Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_verifier.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 21 | #include "class_linker.h" |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 22 | #include "compiler.h" |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 23 | #include "dex_cache.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 24 | #include "dex_file.h" |
| 25 | #include "dex_instruction.h" |
| 26 | #include "dex_instruction_visitor.h" |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 27 | #include "dex_verifier.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 28 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 29 | #include "leb128.h" |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 30 | #include "logging.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 31 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 33 | #include "stringpiece.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 34 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 35 | #if defined(ART_USE_LLVM_COMPILER) |
| 36 | #include "compiler_llvm/backend_types.h" |
| 37 | #include "compiler_llvm/inferred_reg_category_map.h" |
| 38 | using namespace art::compiler_llvm; |
| 39 | #endif |
| 40 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 41 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 42 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 43 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 44 | static const bool gDebugVerify = false; |
| 45 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 46 | static const char* type_strings[] = { |
| 47 | "Unknown", |
| 48 | "Conflict", |
| 49 | "Boolean", |
| 50 | "Byte", |
| 51 | "Short", |
| 52 | "Char", |
| 53 | "Integer", |
| 54 | "Float", |
| 55 | "Long (Low Half)", |
| 56 | "Long (High Half)", |
| 57 | "Double (Low Half)", |
| 58 | "Double (High Half)", |
| 59 | "64-bit Constant (Low Half)", |
| 60 | "64-bit Constant (High Half)", |
| 61 | "32-bit Constant", |
| 62 | "Unresolved Reference", |
| 63 | "Uninitialized Reference", |
| 64 | "Uninitialized This Reference", |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 65 | "Unresolved And Uninitialized Reference", |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 66 | "Reference", |
| 67 | }; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 68 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 69 | std::string RegType::Dump() const { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 70 | DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference); |
| 71 | std::string result; |
| 72 | if (IsConstant()) { |
| 73 | uint32_t val = ConstantValue(); |
| 74 | if (val == 0) { |
| 75 | result = "Zero"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 76 | } else { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 77 | if (IsConstantShort()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 78 | result = StringPrintf("32-bit Constant: %d", val); |
| 79 | } else { |
| 80 | result = StringPrintf("32-bit Constant: 0x%x", val); |
| 81 | } |
| 82 | } |
| 83 | } else { |
| 84 | result = type_strings[type_]; |
| 85 | if (IsReferenceTypes()) { |
| 86 | result += ": "; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 87 | if (IsUnresolvedTypes()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 88 | result += PrettyDescriptor(GetDescriptor()); |
| 89 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 90 | result += PrettyDescriptor(GetClass()); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 91 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 94 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | const RegType& RegType::HighHalf(RegTypeCache* cache) const { |
| 98 | CHECK(IsLowHalf()); |
| 99 | if (type_ == kRegTypeLongLo) { |
| 100 | return cache->FromType(kRegTypeLongHi); |
| 101 | } else if (type_ == kRegTypeDoubleLo) { |
| 102 | return cache->FromType(kRegTypeDoubleHi); |
| 103 | } else { |
| 104 | return cache->FromType(kRegTypeConstHi); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is |
| 110 | * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of |
| 111 | * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J |
| 112 | * is the deepest (lowest upper bound) parent of S and T). |
| 113 | * |
| 114 | * This operation applies for regular classes and arrays, however, for interface types there needn't |
| 115 | * be a partial ordering on the types. We could solve the problem of a lack of a partial order by |
| 116 | * introducing sets of types, however, the only operation permissible on an interface is |
| 117 | * invoke-interface. In the tradition of Java verifiers we defer the verification of interface |
| 118 | * types until an invoke-interface call on the interface typed reference at runtime and allow |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 119 | * the perversion of any Object being assignable to an interface type (note, however, that we don't |
| 120 | * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type |
| 121 | * safe; further the Join on a Object cannot result in a sub-class by definition). |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 122 | */ |
| 123 | Class* RegType::ClassJoin(Class* s, Class* t) { |
| 124 | DCHECK(!s->IsPrimitive()) << PrettyClass(s); |
| 125 | DCHECK(!t->IsPrimitive()) << PrettyClass(t); |
| 126 | if (s == t) { |
| 127 | return s; |
| 128 | } else if (s->IsAssignableFrom(t)) { |
| 129 | return s; |
| 130 | } else if (t->IsAssignableFrom(s)) { |
| 131 | return t; |
| 132 | } else if (s->IsArrayClass() && t->IsArrayClass()) { |
| 133 | Class* s_ct = s->GetComponentType(); |
| 134 | Class* t_ct = t->GetComponentType(); |
| 135 | if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) { |
| 136 | // Given the types aren't the same, if either array is of primitive types then the only |
| 137 | // common parent is java.lang.Object |
| 138 | Class* result = s->GetSuperClass(); // short-cut to java.lang.Object |
| 139 | DCHECK(result->IsObjectClass()); |
| 140 | return result; |
| 141 | } |
| 142 | Class* common_elem = ClassJoin(s_ct, t_ct); |
| 143 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 144 | const ClassLoader* class_loader = s->GetClassLoader(); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 145 | std::string descriptor("["); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 146 | descriptor += ClassHelper(common_elem).GetDescriptor(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 147 | Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader); |
| 148 | DCHECK(array_class != NULL); |
| 149 | return array_class; |
| 150 | } else { |
| 151 | size_t s_depth = s->Depth(); |
| 152 | size_t t_depth = t->Depth(); |
| 153 | // Get s and t to the same depth in the hierarchy |
| 154 | if (s_depth > t_depth) { |
| 155 | while (s_depth > t_depth) { |
| 156 | s = s->GetSuperClass(); |
| 157 | s_depth--; |
| 158 | } |
| 159 | } else { |
| 160 | while (t_depth > s_depth) { |
| 161 | t = t->GetSuperClass(); |
| 162 | t_depth--; |
| 163 | } |
| 164 | } |
| 165 | // Go up the hierarchy until we get to the common parent |
| 166 | while (s != t) { |
| 167 | s = s->GetSuperClass(); |
| 168 | t = t->GetSuperClass(); |
| 169 | } |
| 170 | return s; |
| 171 | } |
| 172 | } |
| 173 | |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 174 | bool RegType::IsAssignableFrom(const RegType& src) const { |
| 175 | if (Equals(src)) { |
| 176 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 177 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 178 | switch (GetType()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 179 | case RegType::kRegTypeBoolean: return src.IsBooleanTypes(); |
| 180 | case RegType::kRegTypeByte: return src.IsByteTypes(); |
| 181 | case RegType::kRegTypeShort: return src.IsShortTypes(); |
| 182 | case RegType::kRegTypeChar: return src.IsCharTypes(); |
| 183 | case RegType::kRegTypeInteger: return src.IsIntegralTypes(); |
| 184 | case RegType::kRegTypeFloat: return src.IsFloatTypes(); |
| 185 | case RegType::kRegTypeLongLo: return src.IsLongTypes(); |
| 186 | case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes(); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 187 | default: |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 188 | if (!IsReferenceTypes()) { |
| 189 | LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'"; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 190 | } |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 191 | if (src.IsZero()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 192 | return true; // all reference types can be assigned null |
| 193 | } else if (!src.IsReferenceTypes()) { |
| 194 | return false; // expect src to be a reference type |
| 195 | } else if (IsJavaLangObject()) { |
| 196 | return true; // all reference types can be assigned to Object |
| 197 | } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) { |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 198 | return true; // We allow assignment to any interface, see comment in ClassJoin |
Ian Rogers | 5d86e52 | 2012-02-01 11:45:24 -0800 | [diff] [blame] | 199 | } else if (IsJavaLangObjectArray()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 200 | return src.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[] |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 201 | } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() && |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 202 | GetClass()->IsAssignableFrom(src.GetClass())) { |
| 203 | // We're assignable from the Class point-of-view |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 204 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 205 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 206 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 207 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 212 | static const RegType& SelectNonConstant(const RegType& a, const RegType& b) { |
| 213 | return a.IsConstant() ? b : a; |
| 214 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 215 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 216 | const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const { |
| 217 | DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 218 | if (IsUnknown() && incoming_type.IsUnknown()) { |
| 219 | return *this; // Unknown MERGE Unknown => Unknown |
| 220 | } else if (IsConflict()) { |
| 221 | return *this; // Conflict MERGE * => Conflict |
| 222 | } else if (incoming_type.IsConflict()) { |
| 223 | return incoming_type; // * MERGE Conflict => Conflict |
| 224 | } else if (IsUnknown() || incoming_type.IsUnknown()) { |
| 225 | return reg_types->Conflict(); // Unknown MERGE * => Conflict |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 226 | } else if (IsConstant() && incoming_type.IsConstant()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 227 | int32_t val1 = ConstantValue(); |
| 228 | int32_t val2 = incoming_type.ConstantValue(); |
| 229 | if (val1 >= 0 && val2 >= 0) { |
| 230 | // +ve1 MERGE +ve2 => MAX(+ve1, +ve2) |
| 231 | if (val1 >= val2) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 232 | return *this; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 233 | } else { |
| 234 | return incoming_type; |
| 235 | } |
| 236 | } else if (val1 < 0 && val2 < 0) { |
| 237 | // -ve1 MERGE -ve2 => MIN(-ve1, -ve2) |
| 238 | if (val1 <= val2) { |
| 239 | return *this; |
| 240 | } else { |
| 241 | return incoming_type; |
| 242 | } |
| 243 | } else { |
| 244 | // Values are +ve and -ve, choose smallest signed type in which they both fit |
| 245 | if (IsConstantByte()) { |
| 246 | if (incoming_type.IsConstantByte()) { |
| 247 | return reg_types->ByteConstant(); |
| 248 | } else if (incoming_type.IsConstantShort()) { |
| 249 | return reg_types->ShortConstant(); |
| 250 | } else { |
| 251 | return reg_types->IntConstant(); |
| 252 | } |
| 253 | } else if (IsConstantShort()) { |
Ian Rogers | 1592bc7 | 2011-10-27 20:08:53 -0700 | [diff] [blame] | 254 | if (incoming_type.IsConstantShort()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 255 | return reg_types->ShortConstant(); |
| 256 | } else { |
| 257 | return reg_types->IntConstant(); |
| 258 | } |
| 259 | } else { |
| 260 | return reg_types->IntConstant(); |
| 261 | } |
| 262 | } |
| 263 | } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) { |
| 264 | if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) { |
| 265 | return reg_types->Boolean(); // boolean MERGE boolean => boolean |
| 266 | } |
| 267 | if (IsByteTypes() && incoming_type.IsByteTypes()) { |
| 268 | return reg_types->Byte(); // byte MERGE byte => byte |
| 269 | } |
| 270 | if (IsShortTypes() && incoming_type.IsShortTypes()) { |
| 271 | return reg_types->Short(); // short MERGE short => short |
| 272 | } |
| 273 | if (IsCharTypes() && incoming_type.IsCharTypes()) { |
| 274 | return reg_types->Char(); // char MERGE char => char |
| 275 | } |
| 276 | return reg_types->Integer(); // int MERGE * => int |
| 277 | } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) || |
| 278 | (IsLongTypes() && incoming_type.IsLongTypes()) || |
| 279 | (IsLongHighTypes() && incoming_type.IsLongHighTypes()) || |
| 280 | (IsDoubleTypes() && incoming_type.IsDoubleTypes()) || |
| 281 | (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) { |
| 282 | // check constant case was handled prior to entry |
| 283 | DCHECK(!IsConstant() || !incoming_type.IsConstant()); |
| 284 | // float/long/double MERGE float/long/double_constant => float/long/double |
| 285 | return SelectNonConstant(*this, incoming_type); |
| 286 | } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 287 | if (IsZero() || incoming_type.IsZero()) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 288 | return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 289 | } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) { |
| 290 | return reg_types->JavaLangObject(); // Object MERGE ref => Object |
| 291 | } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() || |
| 292 | IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) { |
| 293 | // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already |
| 294 | // checked these so => Conflict |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 295 | return reg_types->Conflict(); |
| 296 | } else { // Two reference types, compute Join |
| 297 | Class* c1 = GetClass(); |
| 298 | Class* c2 = incoming_type.GetClass(); |
| 299 | DCHECK(c1 != NULL && !c1->IsPrimitive()); |
| 300 | DCHECK(c2 != NULL && !c2->IsPrimitive()); |
| 301 | Class* join_class = ClassJoin(c1, c2); |
| 302 | if (c1 == join_class) { |
| 303 | return *this; |
| 304 | } else if (c2 == join_class) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 305 | return incoming_type; |
| 306 | } else { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 307 | return reg_types->FromClass(join_class); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 308 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 309 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 310 | } else { |
| 311 | return reg_types->Conflict(); // Unexpected types => Conflict |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 315 | static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 316 | switch (prim_type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 317 | case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean; |
| 318 | case Primitive::kPrimByte: return RegType::kRegTypeByte; |
| 319 | case Primitive::kPrimShort: return RegType::kRegTypeShort; |
| 320 | case Primitive::kPrimChar: return RegType::kRegTypeChar; |
| 321 | case Primitive::kPrimInt: return RegType::kRegTypeInteger; |
| 322 | case Primitive::kPrimLong: return RegType::kRegTypeLongLo; |
| 323 | case Primitive::kPrimFloat: return RegType::kRegTypeFloat; |
| 324 | case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo; |
| 325 | case Primitive::kPrimVoid: |
| 326 | default: return RegType::kRegTypeUnknown; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) { |
| 331 | if (descriptor.length() == 1) { |
| 332 | switch (descriptor[0]) { |
| 333 | case 'Z': return RegType::kRegTypeBoolean; |
| 334 | case 'B': return RegType::kRegTypeByte; |
| 335 | case 'S': return RegType::kRegTypeShort; |
| 336 | case 'C': return RegType::kRegTypeChar; |
| 337 | case 'I': return RegType::kRegTypeInteger; |
| 338 | case 'J': return RegType::kRegTypeLongLo; |
| 339 | case 'F': return RegType::kRegTypeFloat; |
| 340 | case 'D': return RegType::kRegTypeDoubleLo; |
| 341 | case 'V': |
| 342 | default: return RegType::kRegTypeUnknown; |
| 343 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 344 | } else if (descriptor[0] == 'L' || descriptor[0] == '[') { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 345 | return RegType::kRegTypeReference; |
| 346 | } else { |
| 347 | return RegType::kRegTypeUnknown; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | std::ostream& operator<<(std::ostream& os, const RegType& rhs) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 352 | os << rhs.Dump(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 353 | return os; |
| 354 | } |
| 355 | |
| 356 | const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader, |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 357 | const char* descriptor) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 358 | return From(RegTypeFromDescriptor(descriptor), loader, descriptor); |
| 359 | } |
| 360 | |
| 361 | const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader, |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 362 | const char* descriptor) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 363 | if (type <= RegType::kRegTypeLastFixedLocation) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 364 | // entries should be sized greater than primitive types |
| 365 | DCHECK_GT(entries_.size(), static_cast<size_t>(type)); |
| 366 | RegType* entry = entries_[type]; |
| 367 | if (entry == NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 368 | Class* klass = NULL; |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 369 | if (strlen(descriptor) != 0) { |
| 370 | klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 371 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 372 | entry = new RegType(type, klass, 0, type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 373 | entries_[type] = entry; |
| 374 | } |
| 375 | return *entry; |
| 376 | } else { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 377 | DCHECK(type == RegType::kRegTypeReference); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 378 | ClassHelper kh; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 379 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 380 | RegType* cur_entry = entries_[i]; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 381 | // check resolved and unresolved references, ignore uninitialized references |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 382 | if (cur_entry->IsReference()) { |
| 383 | kh.ChangeClass(cur_entry->GetClass()); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 384 | if (strcmp(descriptor, kh.GetDescriptor()) == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 385 | return *cur_entry; |
| 386 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 387 | } else if (cur_entry->IsUnresolvedReference() && |
| 388 | cur_entry->GetDescriptor()->Equals(descriptor)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 389 | return *cur_entry; |
| 390 | } |
| 391 | } |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 392 | Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 393 | if (klass != NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 394 | // Able to resolve so create resolved register type |
| 395 | RegType* entry = new RegType(type, klass, 0, entries_.size()); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 396 | entries_.push_back(entry); |
| 397 | return *entry; |
| 398 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 399 | // TODO: we assume unresolved, but we may be able to do better by validating whether the |
| 400 | // descriptor string is valid |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 401 | // Unable to resolve so create unresolved register type |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 402 | DCHECK(Thread::Current()->IsExceptionPending()); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 403 | Thread::Current()->ClearException(); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 404 | if (IsValidDescriptor(descriptor)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 405 | String* string_descriptor = |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 406 | Runtime::Current()->GetInternTable()->InternStrong(descriptor); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 407 | RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0, |
| 408 | entries_.size()); |
| 409 | entries_.push_back(entry); |
| 410 | return *entry; |
| 411 | } else { |
| 412 | // The descriptor is broken return the unknown type as there's nothing sensible that |
| 413 | // could be done at runtime |
| 414 | return Unknown(); |
| 415 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 416 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | const RegType& RegTypeCache::FromClass(Class* klass) { |
| 421 | if (klass->IsPrimitive()) { |
| 422 | RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType()); |
| 423 | // entries should be sized greater than primitive types |
| 424 | DCHECK_GT(entries_.size(), static_cast<size_t>(type)); |
| 425 | RegType* entry = entries_[type]; |
| 426 | if (entry == NULL) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 427 | entry = new RegType(type, klass, 0, type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 428 | entries_[type] = entry; |
| 429 | } |
| 430 | return *entry; |
| 431 | } else { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 432 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 433 | RegType* cur_entry = entries_[i]; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 434 | if (cur_entry->IsReference() && cur_entry->GetClass() == klass) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 435 | return *cur_entry; |
| 436 | } |
| 437 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 438 | RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 439 | entries_.push_back(entry); |
| 440 | return *entry; |
| 441 | } |
| 442 | } |
| 443 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 444 | const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) { |
| 445 | RegType* entry; |
| 446 | if (type.IsUnresolvedTypes()) { |
| 447 | String* descriptor = type.GetDescriptor(); |
| 448 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 449 | RegType* cur_entry = entries_[i]; |
| 450 | if (cur_entry->IsUnresolvedAndUninitializedReference() && |
| 451 | cur_entry->GetAllocationPc() == allocation_pc && |
| 452 | cur_entry->GetDescriptor() == descriptor) { |
| 453 | return *cur_entry; |
| 454 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 455 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 456 | entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference, |
| 457 | descriptor, allocation_pc, entries_.size()); |
| 458 | } else { |
| 459 | Class* klass = type.GetClass(); |
| 460 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 461 | RegType* cur_entry = entries_[i]; |
| 462 | if (cur_entry->IsUninitializedReference() && |
| 463 | cur_entry->GetAllocationPc() == allocation_pc && |
| 464 | cur_entry->GetClass() == klass) { |
| 465 | return *cur_entry; |
| 466 | } |
| 467 | } |
| 468 | entry = new RegType(RegType::kRegTypeUninitializedReference, |
| 469 | klass, allocation_pc, entries_.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 470 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 471 | entries_.push_back(entry); |
| 472 | return *entry; |
| 473 | } |
| 474 | |
| 475 | const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) { |
| 476 | RegType* entry; |
| 477 | if (uninit_type.IsUnresolvedTypes()) { |
| 478 | String* descriptor = uninit_type.GetDescriptor(); |
| 479 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 480 | RegType* cur_entry = entries_[i]; |
| 481 | if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) { |
| 482 | return *cur_entry; |
| 483 | } |
| 484 | } |
| 485 | entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size()); |
| 486 | } else { |
| 487 | Class* klass = uninit_type.GetClass(); |
| 488 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 489 | RegType* cur_entry = entries_[i]; |
| 490 | if (cur_entry->IsReference() && cur_entry->GetClass() == klass) { |
| 491 | return *cur_entry; |
| 492 | } |
| 493 | } |
| 494 | entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size()); |
| 495 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 496 | entries_.push_back(entry); |
| 497 | return *entry; |
| 498 | } |
| 499 | |
| 500 | const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 501 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 502 | RegType* cur_entry = entries_[i]; |
| 503 | if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) { |
| 504 | return *cur_entry; |
| 505 | } |
| 506 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 507 | RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 508 | entries_.size()); |
| 509 | entries_.push_back(entry); |
| 510 | return *entry; |
| 511 | } |
| 512 | |
| 513 | const RegType& RegTypeCache::FromType(RegType::Type type) { |
| 514 | CHECK(type < RegType::kRegTypeReference); |
| 515 | switch (type) { |
| 516 | case RegType::kRegTypeBoolean: return From(type, NULL, "Z"); |
| 517 | case RegType::kRegTypeByte: return From(type, NULL, "B"); |
| 518 | case RegType::kRegTypeShort: return From(type, NULL, "S"); |
| 519 | case RegType::kRegTypeChar: return From(type, NULL, "C"); |
| 520 | case RegType::kRegTypeInteger: return From(type, NULL, "I"); |
| 521 | case RegType::kRegTypeFloat: return From(type, NULL, "F"); |
| 522 | case RegType::kRegTypeLongLo: |
| 523 | case RegType::kRegTypeLongHi: return From(type, NULL, "J"); |
| 524 | case RegType::kRegTypeDoubleLo: |
| 525 | case RegType::kRegTypeDoubleHi: return From(type, NULL, "D"); |
| 526 | default: return From(type, NULL, ""); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | const RegType& RegTypeCache::FromCat1Const(int32_t value) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 531 | for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) { |
| 532 | RegType* cur_entry = entries_[i]; |
| 533 | if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) { |
| 534 | return *cur_entry; |
| 535 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 536 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 537 | RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size()); |
| 538 | entries_.push_back(entry); |
| 539 | return *entry; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 542 | const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 543 | CHECK(array.IsArrayTypes()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 544 | if (array.IsUnresolvedTypes()) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 545 | std::string descriptor(array.GetDescriptor()->ToModifiedUtf8()); |
| 546 | std::string component(descriptor.substr(1, descriptor.size() - 1)); |
Ian Rogers | 672297c | 2012-01-10 14:50:55 -0800 | [diff] [blame] | 547 | return FromDescriptor(loader, component.c_str()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 548 | } else { |
| 549 | return FromClass(array.GetClass()->GetComponentType()); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 554 | bool RegisterLine::CheckConstructorReturn() const { |
| 555 | for (size_t i = 0; i < num_regs_; i++) { |
| 556 | if (GetRegisterType(i).IsUninitializedThisReference()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 557 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 558 | << "Constructor returning without calling superclass constructor"; |
| 559 | return false; |
| 560 | } |
| 561 | } |
| 562 | return true; |
| 563 | } |
| 564 | |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 565 | bool RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 566 | DCHECK(vdst < num_regs_); |
| 567 | if (new_type.IsLowHalf()) { |
| 568 | line_[vdst] = new_type.GetId(); |
| 569 | line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(); |
| 570 | } else if (new_type.IsHighHalf()) { |
| 571 | /* should never set these explicitly */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 572 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Explicit set of high register type"; |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 573 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 574 | } else if (new_type.IsConflict()) { // should only be set during a merge |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 575 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Set register to unknown type " << new_type; |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 576 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 577 | } else { |
| 578 | line_[vdst] = new_type.GetId(); |
| 579 | } |
| 580 | // Clear the monitor entry bits for this register. |
| 581 | ClearAllRegToLockDepths(vdst); |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 582 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void RegisterLine::SetResultTypeToUnknown() { |
| 586 | uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 587 | result_[0] = unknown_id; |
| 588 | result_[1] = unknown_id; |
| 589 | } |
| 590 | |
| 591 | void RegisterLine::SetResultRegisterType(const RegType& new_type) { |
| 592 | result_[0] = new_type.GetId(); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 593 | if (new_type.IsLowHalf()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 594 | DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1); |
| 595 | result_[1] = new_type.GetId() + 1; |
| 596 | } else { |
| 597 | result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const { |
| 602 | // The register index was validated during the static pass, so we don't need to check it here. |
| 603 | DCHECK_LT(vsrc, num_regs_); |
| 604 | return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]); |
| 605 | } |
| 606 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 607 | const RegType& RegisterLine::GetInvocationThis(const DecodedInstruction& dec_insn) { |
| 608 | if (dec_insn.vA < 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 609 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke lacks 'this'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 610 | return verifier_->GetRegTypeCache()->Unknown(); |
| 611 | } |
| 612 | /* get the element type of the array held in vsrc */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 613 | const RegType& this_type = GetRegisterType(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 614 | if (!this_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 615 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "tried to get class from non-reference register v" |
| 616 | << dec_insn.vC << " (type=" << this_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 617 | return verifier_->GetRegTypeCache()->Unknown(); |
| 618 | } |
| 619 | return this_type; |
| 620 | } |
| 621 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 622 | bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) { |
| 623 | // Verify the src register type against the check type refining the type of the register |
| 624 | const RegType& src_type = GetRegisterType(vsrc); |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 625 | if (!check_type.IsAssignableFrom(src_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 626 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "register v" << vsrc << " has type " << src_type |
| 627 | << " but expected " << check_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 628 | return false; |
| 629 | } |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 630 | if (check_type.IsLowHalf()) { |
| 631 | const RegType& src_type_h = GetRegisterType(vsrc + 1); |
| 632 | if (!src_type.CheckWidePair(src_type_h)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 633 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "wide register v" << vsrc << " has type " |
| 634 | << src_type << "/" << src_type_h; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 638 | // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less |
| 639 | // precise than the subtype in vsrc so leave it for reference types. For primitive types |
| 640 | // if they are a defined type then they are as precise as we can get, however, for constant |
| 641 | // types we may wish to refine them. Unfortunately constant propagation has rendered this useless. |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 646 | DCHECK(uninit_type.IsUninitializedTypes()); |
| 647 | const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type); |
| 648 | size_t changed = 0; |
| 649 | for (size_t i = 0; i < num_regs_; i++) { |
| 650 | if (GetRegisterType(i).Equals(uninit_type)) { |
| 651 | line_[i] = init_type.GetId(); |
| 652 | changed++; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 653 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 654 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 655 | DCHECK_GT(changed, 0u); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) { |
| 659 | for (size_t i = 0; i < num_regs_; i++) { |
| 660 | if (GetRegisterType(i).Equals(uninit_type)) { |
| 661 | line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId(); |
| 662 | ClearAllRegToLockDepths(i); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) { |
| 668 | DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef); |
| 669 | const RegType& type = GetRegisterType(vsrc); |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 670 | if (!SetRegisterType(vdst, type)) { |
| 671 | return; |
| 672 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 673 | if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) || |
| 674 | (cat == kTypeCategoryRef && !type.IsReferenceTypes())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 675 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type |
| 676 | << " cat=" << static_cast<int>(cat); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 677 | } else if (cat == kTypeCategoryRef) { |
| 678 | CopyRegToLockDepth(vdst, vsrc); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) { |
| 683 | const RegType& type_l = GetRegisterType(vsrc); |
| 684 | const RegType& type_h = GetRegisterType(vsrc + 1); |
| 685 | |
| 686 | if (!type_l.CheckWidePair(type_h)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 687 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy2 v" << vdst << "<-v" << vsrc |
| 688 | << " type=" << type_l << "/" << type_h; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 689 | } else { |
| 690 | SetRegisterType(vdst, type_l); // implicitly sets the second half |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) { |
| 695 | const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]); |
| 696 | if ((!is_reference && !type.IsCategory1Types()) || |
| 697 | (is_reference && !type.IsReferenceTypes())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 698 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 699 | << "copyRes1 v" << vdst << "<- result0" << " type=" << type; |
| 700 | } else { |
| 701 | DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown()); |
| 702 | SetRegisterType(vdst, type); |
| 703 | result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Implement "move-result-wide". Copy the category-2 value from the result |
| 709 | * register to another register, and reset the result register. |
| 710 | */ |
| 711 | void RegisterLine::CopyResultRegister2(uint32_t vdst) { |
| 712 | const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]); |
| 713 | const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]); |
| 714 | if (!type_l.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 715 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 716 | << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l; |
| 717 | } else { |
| 718 | DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case |
| 719 | SetRegisterType(vdst, type_l); // also sets the high |
| 720 | result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 721 | result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId(); |
| 722 | } |
| 723 | } |
| 724 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 725 | void RegisterLine::CheckUnaryOp(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 726 | const RegType& dst_type, const RegType& src_type) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 727 | if (VerifyRegisterType(dec_insn.vB, src_type)) { |
| 728 | SetRegisterType(dec_insn.vA, dst_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 732 | void RegisterLine::CheckBinaryOp(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 733 | const RegType& dst_type, |
| 734 | const RegType& src_type1, const RegType& src_type2, |
| 735 | bool check_boolean_op) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 736 | if (VerifyRegisterType(dec_insn.vB, src_type1) && |
| 737 | VerifyRegisterType(dec_insn.vC, src_type2)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 738 | if (check_boolean_op) { |
| 739 | DCHECK(dst_type.IsInteger()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 740 | if (GetRegisterType(dec_insn.vB).IsBooleanTypes() && |
| 741 | GetRegisterType(dec_insn.vC).IsBooleanTypes()) { |
| 742 | SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 746 | SetRegisterType(dec_insn.vA, dst_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 750 | void RegisterLine::CheckBinaryOp2addr(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 751 | const RegType& dst_type, const RegType& src_type1, |
| 752 | const RegType& src_type2, bool check_boolean_op) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 753 | if (VerifyRegisterType(dec_insn.vA, src_type1) && |
| 754 | VerifyRegisterType(dec_insn.vB, src_type2)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 755 | if (check_boolean_op) { |
| 756 | DCHECK(dst_type.IsInteger()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 757 | if (GetRegisterType(dec_insn.vA).IsBooleanTypes() && |
| 758 | GetRegisterType(dec_insn.vB).IsBooleanTypes()) { |
| 759 | SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 763 | SetRegisterType(dec_insn.vA, dst_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 767 | void RegisterLine::CheckLiteralOp(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 768 | const RegType& dst_type, const RegType& src_type, |
| 769 | bool check_boolean_op) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 770 | if (VerifyRegisterType(dec_insn.vB, src_type)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 771 | if (check_boolean_op) { |
| 772 | DCHECK(dst_type.IsInteger()); |
| 773 | /* check vB with the call, then check the constant manually */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 774 | if (GetRegisterType(dec_insn.vB).IsBooleanTypes() && |
| 775 | (dec_insn.vC == 0 || dec_insn.vC == 1)) { |
| 776 | SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 777 | return; |
| 778 | } |
| 779 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 780 | SetRegisterType(dec_insn.vA, dst_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) { |
| 785 | const RegType& reg_type = GetRegisterType(reg_idx); |
| 786 | if (!reg_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 787 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter on non-object (" << reg_type << ")"; |
Elliott Hughes | fbef946 | 2011-12-14 14:24:40 -0800 | [diff] [blame] | 788 | } else if (monitors_.size() >= 32) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 789 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter stack overflow: " << monitors_.size(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 790 | } else { |
| 791 | SetRegToLockDepth(reg_idx, monitors_.size()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 792 | monitors_.push_back(insn_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | void RegisterLine::PopMonitor(uint32_t reg_idx) { |
| 797 | const RegType& reg_type = GetRegisterType(reg_idx); |
| 798 | if (!reg_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 799 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit on non-object (" << reg_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 800 | } else if (monitors_.empty()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 801 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit stack underflow"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 802 | } else { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 803 | monitors_.pop_back(); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 804 | if (!IsSetLockDepth(reg_idx, monitors_.size())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 805 | // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before |
| 806 | // format "036" the constant collector may create unlocks on the same object but referenced |
| 807 | // via different registers. |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 808 | ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 809 | : verifier_->LogVerifyInfo()) |
| 810 | << "monitor-exit not unlocking the top of the monitor stack"; |
| 811 | } else { |
| 812 | // Record the register was unlocked |
| 813 | ClearRegToLockDepth(reg_idx, monitors_.size()); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | bool RegisterLine::VerifyMonitorStackEmpty() { |
| 819 | if (MonitorStackDepth() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 820 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected empty monitor stack"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 821 | return false; |
| 822 | } else { |
| 823 | return true; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) { |
| 828 | bool changed = false; |
| 829 | for (size_t idx = 0; idx < num_regs_; idx++) { |
| 830 | if (line_[idx] != incoming_line->line_[idx]) { |
| 831 | const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx); |
| 832 | const RegType& cur_type = GetRegisterType(idx); |
| 833 | const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache()); |
| 834 | changed = changed || !cur_type.Equals(new_type); |
| 835 | line_[idx] = new_type.GetId(); |
| 836 | } |
| 837 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 838 | if (monitors_.size() != incoming_line->monitors_.size()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 839 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths (depth=" |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 840 | << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")"; |
| 841 | } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) { |
| 842 | for (uint32_t idx = 0; idx < num_regs_; idx++) { |
| 843 | size_t depths = reg_to_lock_depths_.count(idx); |
| 844 | size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx); |
| 845 | if (depths != incoming_depths) { |
| 846 | if (depths == 0 || incoming_depths == 0) { |
| 847 | reg_to_lock_depths_.erase(idx); |
| 848 | } else { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 849 | verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths for register v" << idx |
| 850 | << ": " << depths << " != " << incoming_depths; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | return changed; |
| 857 | } |
| 858 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 859 | void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 860 | for (size_t i = 0; i < num_regs_; i += 8) { |
| 861 | uint8_t val = 0; |
| 862 | for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) { |
| 863 | // Note: we write 1 for a Reference but not for Null |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 864 | if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 865 | val |= 1 << j; |
| 866 | } |
| 867 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 868 | if ((i / 8) >= max_bytes) { |
| 869 | DCHECK_EQ(0, val); |
| 870 | continue; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 871 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 872 | DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val); |
| 873 | data.push_back(val); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
| 877 | std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 878 | os << rhs.Dump(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 879 | return os; |
| 880 | } |
| 881 | |
| 882 | |
| 883 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags, |
| 884 | uint32_t insns_size, uint16_t registers_size, |
| 885 | DexVerifier* verifier) { |
| 886 | DCHECK_GT(insns_size, 0U); |
| 887 | |
| 888 | for (uint32_t i = 0; i < insns_size; i++) { |
| 889 | bool interesting = false; |
| 890 | switch (mode) { |
| 891 | case kTrackRegsAll: |
| 892 | interesting = flags[i].IsOpcode(); |
| 893 | break; |
| 894 | case kTrackRegsGcPoints: |
| 895 | interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget(); |
| 896 | break; |
| 897 | case kTrackRegsBranches: |
| 898 | interesting = flags[i].IsBranchTarget(); |
| 899 | break; |
| 900 | default: |
| 901 | break; |
| 902 | } |
| 903 | if (interesting) { |
| 904 | pc_to_register_line_[i] = new RegisterLine(registers_size, verifier); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 909 | bool DexVerifier::VerifyClass(const Class* klass, std::string& error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 910 | if (klass->IsVerified()) { |
| 911 | return true; |
| 912 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 913 | Class* super = klass->GetSuperClass(); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 914 | if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 915 | error = "Verifier rejected class "; |
| 916 | error += PrettyDescriptor(klass); |
| 917 | error += " that has no super class"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 918 | return false; |
| 919 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 920 | if (super != NULL && super->IsFinal()) { |
| 921 | error = "Verifier rejected class "; |
| 922 | error += PrettyDescriptor(klass); |
| 923 | error += " that attempts to sub-class final class "; |
| 924 | error += PrettyDescriptor(super); |
| 925 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 926 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 927 | for (size_t i = 0; i < klass->NumDirectMethods(); ++i) { |
| 928 | Method* method = klass->GetDirectMethod(i); |
| 929 | if (!VerifyMethod(method)) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 930 | error = "Verifier rejected class "; |
| 931 | error += PrettyDescriptor(klass); |
| 932 | error += " due to bad method "; |
| 933 | error += PrettyMethod(method, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 934 | return false; |
| 935 | } |
| 936 | } |
| 937 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 938 | Method* method = klass->GetVirtualMethod(i); |
| 939 | if (!VerifyMethod(method)) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 940 | error = "Verifier rejected class "; |
| 941 | error += PrettyDescriptor(klass); |
| 942 | error += " due to bad method "; |
| 943 | error += PrettyMethod(method, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | } |
| 947 | return true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 948 | } |
| 949 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 950 | bool DexVerifier::VerifyMethod(Method* method) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 951 | DexVerifier verifier(method); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 952 | bool success = verifier.VerifyAll(); |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 953 | CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE); |
| 954 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 955 | // We expect either success and no verification error, or failure and a generic failure to |
| 956 | // reject the class. |
| 957 | if (success) { |
| 958 | if (verifier.failure_ != VERIFY_ERROR_NONE) { |
| 959 | LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl |
| 960 | << verifier.fail_messages_; |
| 961 | } |
| 962 | } else { |
| 963 | LOG(INFO) << "Verification error in " << PrettyMethod(method) << " " |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 964 | << verifier.fail_messages_.str(); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 965 | if (gDebugVerify) { |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 966 | std::cout << std::endl << verifier.info_messages_.str(); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 967 | verifier.Dump(std::cout); |
| 968 | } |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 969 | DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) || |
| 970 | (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 971 | } |
| 972 | return success; |
| 973 | } |
| 974 | |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 975 | void DexVerifier::VerifyMethodAndDump(Method* method) { |
| 976 | DexVerifier verifier(method); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 977 | verifier.VerifyAll(); |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 978 | |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 979 | LOG(INFO) << "Dump of method " << PrettyMethod(method) << " " |
| 980 | << verifier.fail_messages_.str() << std::endl |
| 981 | << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier); |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 982 | } |
| 983 | |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 984 | bool DexVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache, |
| 985 | const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) { |
| 986 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx); |
| 987 | const byte* class_data = dex_file->GetClassData(class_def); |
| 988 | ClassDataItemIterator it(*dex_file, class_data); |
| 989 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 990 | it.Next(); |
| 991 | } |
| 992 | while (it.HasNextDirectMethod()) { |
| 993 | uint32_t method_idx = it.GetMemberIndex(); |
| 994 | if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx, |
| 995 | it.GetMethodCodeItem())) { |
| 996 | error = "Verifier rejected class"; |
| 997 | error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| 998 | error += " due to bad method "; |
| 999 | error += PrettyMethod(method_idx, *dex_file); |
| 1000 | return false; |
| 1001 | } |
| 1002 | it.Next(); |
| 1003 | } |
| 1004 | while (it.HasNextVirtualMethod()) { |
| 1005 | uint32_t method_idx = it.GetMemberIndex(); |
| 1006 | if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx, |
| 1007 | it.GetMethodCodeItem())) { |
| 1008 | error = "Verifier rejected class"; |
| 1009 | error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| 1010 | error += " due to bad method "; |
| 1011 | error += PrettyMethod(method_idx, *dex_file); |
| 1012 | return false; |
| 1013 | } |
| 1014 | it.Next(); |
| 1015 | } |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
| 1019 | bool DexVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache, |
| 1020 | const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) { |
| 1021 | DexVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item); |
| 1022 | |
| 1023 | // Without a method*, we can only verify the struture. |
| 1024 | bool success = verifier.VerifyStructure(); |
| 1025 | CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE); |
| 1026 | |
| 1027 | // We expect either success and no verification error, or failure and a generic failure to |
| 1028 | // reject the class. |
| 1029 | if (success) { |
| 1030 | if (verifier.failure_ != VERIFY_ERROR_NONE) { |
| 1031 | LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method_idx, *dex_file) |
| 1032 | << std::endl << verifier.fail_messages_; |
| 1033 | } |
| 1034 | } else { |
| 1035 | LOG(INFO) << "Verification error in " << PrettyMethod(method_idx, *dex_file) << " " |
| 1036 | << verifier.fail_messages_.str(); |
| 1037 | if (gDebugVerify) { |
| 1038 | std::cout << std::endl << verifier.info_messages_.str(); |
| 1039 | verifier.Dump(std::cout); |
| 1040 | } |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1041 | DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) || |
| 1042 | (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1043 | } |
| 1044 | return success; |
| 1045 | } |
| 1046 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1047 | DexVerifier::DexVerifier(Method* method) |
| 1048 | : work_insn_idx_(-1), |
| 1049 | method_(method), |
| 1050 | failure_(VERIFY_ERROR_NONE), |
| 1051 | new_instance_count_(0), |
| 1052 | monitor_enter_count_(0) { |
| 1053 | CHECK(method != NULL); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1054 | dex_cache_ = method->GetDeclaringClass()->GetDexCache(); |
| 1055 | class_loader_ = method->GetDeclaringClass()->GetClassLoader(); |
Brian Carlstrom | c12a17a | 2012-01-17 18:02:32 -0800 | [diff] [blame] | 1056 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1057 | dex_file_ = &class_linker->FindDexFile(dex_cache_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1058 | code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset()); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1059 | const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef(); |
| 1060 | class_def_idx_ = dex_file_->GetIndexForClassDef(*class_def); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1063 | DexVerifier::DexVerifier(const DexFile* dex_file, DexCache* dex_cache, |
| 1064 | const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) |
| 1065 | : work_insn_idx_(-1), |
| 1066 | method_(NULL), |
| 1067 | dex_file_(dex_file), |
| 1068 | dex_cache_(dex_cache), |
| 1069 | class_loader_(class_loader), |
| 1070 | class_def_idx_(class_def_idx), |
| 1071 | code_item_(code_item), |
| 1072 | failure_(VERIFY_ERROR_NONE), |
| 1073 | new_instance_count_(0), |
| 1074 | monitor_enter_count_(0) { |
| 1075 | } |
| 1076 | |
| 1077 | bool DexVerifier::VerifyAll() { |
| 1078 | CHECK(method_ != NULL); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1079 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 1080 | if (code_item_ == NULL) { |
| 1081 | if (!method_->IsNative() && !method_->IsAbstract()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1082 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1083 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1084 | } else { |
| 1085 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1086 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1087 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1088 | return VerifyStructure() && VerifyCodeFlow(); |
| 1089 | } |
| 1090 | |
| 1091 | bool DexVerifier::VerifyStructure() { |
| 1092 | if (code_item_ == NULL) { |
| 1093 | return true; |
| 1094 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1095 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 1096 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1097 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 1098 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1099 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1100 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1101 | // Allocate and initialize an array to hold instruction data. |
| 1102 | insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]()); |
| 1103 | // Run through the instructions and see if the width checks out. |
| 1104 | bool result = ComputeWidthsAndCountOps(); |
| 1105 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 1106 | result = result && ScanTryCatchBlocks(); |
| 1107 | // Perform static instruction verification. |
| 1108 | result = result && VerifyInstructions(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1109 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 1112 | std::ostream& DexVerifier::Fail(VerifyError error) { |
| 1113 | CHECK_EQ(failure_, VERIFY_ERROR_NONE); |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 1114 | if (Runtime::Current()->IsCompiler()) { |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 1115 | switch (error) { |
| 1116 | // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1117 | // errors into soft verification errors so that we re-verify at runtime. We may fail to find |
| 1118 | // or to agree on access because of not yet available class loaders, or class loaders that |
| 1119 | // will differ at runtime. |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 1120 | case VERIFY_ERROR_NO_CLASS: |
| 1121 | case VERIFY_ERROR_NO_FIELD: |
| 1122 | case VERIFY_ERROR_NO_METHOD: |
Ian Rogers | 9ada79c | 2012-02-03 14:29:52 -0800 | [diff] [blame] | 1123 | case VERIFY_ERROR_ACCESS_CLASS: |
| 1124 | case VERIFY_ERROR_ACCESS_FIELD: |
| 1125 | case VERIFY_ERROR_ACCESS_METHOD: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1126 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 1127 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1128 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 1129 | // marked as rejected to prevent it from being compiled. |
| 1130 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1131 | Compiler::ClassReference ref(dex_file_, class_def_idx_); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 1132 | AddRejectedClass(ref); |
| 1133 | break; |
| 1134 | } |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 1135 | default: |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | failure_ = error; |
| 1140 | return fail_messages_ << "VFY: " << PrettyMethod(method_) |
| 1141 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 1142 | } |
| 1143 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1144 | bool DexVerifier::ComputeWidthsAndCountOps() { |
| 1145 | const uint16_t* insns = code_item_->insns_; |
| 1146 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 1147 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1148 | size_t new_instance_count = 0; |
| 1149 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1150 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1151 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1152 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1153 | Instruction::Code opcode = inst->Opcode(); |
| 1154 | if (opcode == Instruction::NEW_INSTANCE) { |
| 1155 | new_instance_count++; |
| 1156 | } else if (opcode == Instruction::MONITOR_ENTER) { |
| 1157 | monitor_enter_count++; |
| 1158 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1159 | size_t inst_size = inst->SizeInCodeUnits(); |
| 1160 | insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size); |
| 1161 | dex_pc += inst_size; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1162 | inst = inst->Next(); |
| 1163 | } |
| 1164 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1165 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1166 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 1167 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1168 | return false; |
| 1169 | } |
| 1170 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1171 | new_instance_count_ = new_instance_count; |
| 1172 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1173 | return true; |
| 1174 | } |
| 1175 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1176 | bool DexVerifier::ScanTryCatchBlocks() { |
| 1177 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1178 | if (tries_size == 0) { |
| 1179 | return true; |
| 1180 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1181 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1182 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1183 | |
| 1184 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 1185 | const DexFile::TryItem* try_item = &tries[idx]; |
| 1186 | uint32_t start = try_item->start_addr_; |
| 1187 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1188 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1189 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1190 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1193 | if (!insn_flags_[start].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1194 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1195 | return false; |
| 1196 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1197 | for (uint32_t dex_pc = start; dex_pc < end; |
| 1198 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
| 1199 | insn_flags_[dex_pc].SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1200 | } |
| 1201 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1202 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1203 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1204 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1205 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1206 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1207 | CatchHandlerIterator iterator(handlers_ptr); |
| 1208 | for (; iterator.HasNext(); iterator.Next()) { |
| 1209 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1210 | if (!insn_flags_[dex_pc].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1211 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1212 | return false; |
| 1213 | } |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1214 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 1215 | if (inst->Opcode() != Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1216 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception (" |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1217 | << dex_pc << ")"; |
| 1218 | return false; |
| 1219 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1220 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1221 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1222 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1223 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1224 | Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(), |
| 1225 | dex_cache_, class_loader_); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1226 | if (exception_type == NULL) { |
| 1227 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 1228 | Thread::Current()->ClearException(); |
| 1229 | } |
| 1230 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1231 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1232 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1233 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1237 | bool DexVerifier::VerifyInstructions() { |
| 1238 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1239 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1240 | /* Flag the start of the method as a branch target. */ |
| 1241 | insn_flags_[0].SetBranchTarget(); |
| 1242 | |
| 1243 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1244 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1245 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1246 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 1247 | fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
| 1250 | /* Flag instructions that are garbage collection points */ |
| 1251 | if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) { |
| 1252 | insn_flags_[dex_pc].SetGcPoint(); |
| 1253 | } |
| 1254 | dex_pc += inst->SizeInCodeUnits(); |
| 1255 | inst = inst->Next(); |
| 1256 | } |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
| 1260 | bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1261 | DecodedInstruction dec_insn(inst); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1262 | bool result = true; |
| 1263 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1264 | case Instruction::kVerifyRegA: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1265 | result = result && CheckRegisterIndex(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1266 | break; |
| 1267 | case Instruction::kVerifyRegAWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1268 | result = result && CheckWideRegisterIndex(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1269 | break; |
| 1270 | } |
| 1271 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1272 | case Instruction::kVerifyRegB: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1273 | result = result && CheckRegisterIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1274 | break; |
| 1275 | case Instruction::kVerifyRegBField: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1276 | result = result && CheckFieldIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1277 | break; |
| 1278 | case Instruction::kVerifyRegBMethod: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1279 | result = result && CheckMethodIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | case Instruction::kVerifyRegBNewInstance: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1282 | result = result && CheckNewInstance(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1283 | break; |
| 1284 | case Instruction::kVerifyRegBString: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1285 | result = result && CheckStringIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1286 | break; |
| 1287 | case Instruction::kVerifyRegBType: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1288 | result = result && CheckTypeIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1289 | break; |
| 1290 | case Instruction::kVerifyRegBWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1291 | result = result && CheckWideRegisterIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1292 | break; |
| 1293 | } |
| 1294 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1295 | case Instruction::kVerifyRegC: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1296 | result = result && CheckRegisterIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1297 | break; |
| 1298 | case Instruction::kVerifyRegCField: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1299 | result = result && CheckFieldIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1300 | break; |
| 1301 | case Instruction::kVerifyRegCNewArray: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1302 | result = result && CheckNewArray(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1303 | break; |
| 1304 | case Instruction::kVerifyRegCType: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1305 | result = result && CheckTypeIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1306 | break; |
| 1307 | case Instruction::kVerifyRegCWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1308 | result = result && CheckWideRegisterIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1309 | break; |
| 1310 | } |
| 1311 | switch (inst->GetVerifyExtraFlags()) { |
| 1312 | case Instruction::kVerifyArrayData: |
| 1313 | result = result && CheckArrayData(code_offset); |
| 1314 | break; |
| 1315 | case Instruction::kVerifyBranchTarget: |
| 1316 | result = result && CheckBranchTarget(code_offset); |
| 1317 | break; |
| 1318 | case Instruction::kVerifySwitchTargets: |
| 1319 | result = result && CheckSwitchTargets(code_offset); |
| 1320 | break; |
| 1321 | case Instruction::kVerifyVarArg: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1322 | result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1323 | break; |
| 1324 | case Instruction::kVerifyVarArgRange: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1325 | result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1326 | break; |
| 1327 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1328 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1329 | result = false; |
| 1330 | break; |
| 1331 | } |
| 1332 | return result; |
| 1333 | } |
| 1334 | |
| 1335 | bool DexVerifier::CheckRegisterIndex(uint32_t idx) { |
| 1336 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1337 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 1338 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
| 1344 | bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) { |
| 1345 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1346 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 1347 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1348 | return false; |
| 1349 | } |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
| 1353 | bool DexVerifier::CheckFieldIndex(uint32_t idx) { |
| 1354 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1355 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 1356 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1357 | return false; |
| 1358 | } |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
| 1362 | bool DexVerifier::CheckMethodIndex(uint32_t idx) { |
| 1363 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1364 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 1365 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1366 | return false; |
| 1367 | } |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
| 1371 | bool DexVerifier::CheckNewInstance(uint32_t idx) { |
| 1372 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1373 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1374 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1375 | return false; |
| 1376 | } |
| 1377 | // We don't need the actual class, just a pointer to the class name. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1378 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1379 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1380 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
| 1383 | return true; |
| 1384 | } |
| 1385 | |
| 1386 | bool DexVerifier::CheckStringIndex(uint32_t idx) { |
| 1387 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1388 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 1389 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1390 | return false; |
| 1391 | } |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | bool DexVerifier::CheckTypeIndex(uint32_t idx) { |
| 1396 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1397 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1398 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
| 1401 | return true; |
| 1402 | } |
| 1403 | |
| 1404 | bool DexVerifier::CheckNewArray(uint32_t idx) { |
| 1405 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1406 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1407 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1408 | return false; |
| 1409 | } |
| 1410 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1411 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1412 | const char* cp = descriptor; |
| 1413 | while (*cp++ == '[') { |
| 1414 | bracket_count++; |
| 1415 | } |
| 1416 | if (bracket_count == 0) { |
| 1417 | /* The given class must be an array type. */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1418 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1419 | return false; |
| 1420 | } else if (bracket_count > 255) { |
| 1421 | /* It is illegal to create an array of more than 255 dimensions. */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1422 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1423 | return false; |
| 1424 | } |
| 1425 | return true; |
| 1426 | } |
| 1427 | |
| 1428 | bool DexVerifier::CheckArrayData(uint32_t cur_offset) { |
| 1429 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1430 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1431 | const uint16_t* array_data; |
| 1432 | int32_t array_data_offset; |
| 1433 | |
| 1434 | DCHECK_LT(cur_offset, insn_count); |
| 1435 | /* make sure the start of the array data table is in range */ |
| 1436 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 1437 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 1438 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1439 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset |
| 1440 | << ", data offset " << array_data_offset << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1441 | return false; |
| 1442 | } |
| 1443 | /* offset to array data table is a relative branch-style offset */ |
| 1444 | array_data = insns + array_data_offset; |
| 1445 | /* make sure the table is 32-bit aligned */ |
| 1446 | if ((((uint32_t) array_data) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1447 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1448 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1452 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1453 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1454 | /* make sure the end of the switch is in range */ |
| 1455 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1456 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1457 | << ", data offset " << array_data_offset << ", end " |
| 1458 | << cur_offset + array_data_offset + table_size |
| 1459 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1460 | return false; |
| 1461 | } |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
| 1465 | bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) { |
| 1466 | int32_t offset; |
| 1467 | bool isConditional, selfOkay; |
| 1468 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1469 | return false; |
| 1470 | } |
| 1471 | if (!selfOkay && offset == 0) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1472 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1473 | return false; |
| 1474 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1475 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1476 | // to have identical "wrap-around" behavior, but it's unwise to depend on that. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1477 | if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1478 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1479 | return false; |
| 1480 | } |
| 1481 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1482 | int32_t abs_offset = cur_offset + offset; |
| 1483 | if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1484 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1485 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1486 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1487 | return false; |
| 1488 | } |
| 1489 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 1494 | bool* selfOkay) { |
| 1495 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1496 | *pConditional = false; |
| 1497 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1498 | switch (*insns & 0xff) { |
| 1499 | case Instruction::GOTO: |
| 1500 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1501 | break; |
| 1502 | case Instruction::GOTO_32: |
| 1503 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1504 | *selfOkay = true; |
| 1505 | break; |
| 1506 | case Instruction::GOTO_16: |
| 1507 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1508 | break; |
| 1509 | case Instruction::IF_EQ: |
| 1510 | case Instruction::IF_NE: |
| 1511 | case Instruction::IF_LT: |
| 1512 | case Instruction::IF_GE: |
| 1513 | case Instruction::IF_GT: |
| 1514 | case Instruction::IF_LE: |
| 1515 | case Instruction::IF_EQZ: |
| 1516 | case Instruction::IF_NEZ: |
| 1517 | case Instruction::IF_LTZ: |
| 1518 | case Instruction::IF_GEZ: |
| 1519 | case Instruction::IF_GTZ: |
| 1520 | case Instruction::IF_LEZ: |
| 1521 | *pOffset = (int16_t) insns[1]; |
| 1522 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1523 | break; |
| 1524 | default: |
| 1525 | return false; |
| 1526 | break; |
| 1527 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1528 | return true; |
| 1529 | } |
| 1530 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1531 | bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
| 1532 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1533 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1534 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1535 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1536 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
| 1537 | if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1538 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
| 1539 | << ", switch offset " << switch_offset << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1540 | return false; |
| 1541 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1542 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1543 | const uint16_t* switch_insns = insns + switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1544 | /* make sure the table is 32-bit aligned */ |
| 1545 | if ((((uint32_t) switch_insns) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1546 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1547 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1548 | return false; |
| 1549 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1550 | uint32_t switch_count = switch_insns[1]; |
| 1551 | int32_t keys_offset, targets_offset; |
| 1552 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1553 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1554 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1555 | targets_offset = 4; |
| 1556 | keys_offset = -1; |
| 1557 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1558 | } else { |
| 1559 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1560 | keys_offset = 2; |
| 1561 | targets_offset = 2 + 2 * switch_count; |
| 1562 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1563 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1564 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1565 | if (switch_insns[0] != expected_signature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1566 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1567 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1568 | return false; |
| 1569 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1570 | /* make sure the end of the switch is in range */ |
| 1571 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1572 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset " |
| 1573 | << switch_offset << ", end " |
| 1574 | << (cur_offset + switch_offset + table_size) |
| 1575 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1576 | return false; |
| 1577 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1578 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1579 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1580 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1581 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1582 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 1583 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 1584 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1585 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 1586 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1589 | last_key = key; |
| 1590 | } |
| 1591 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1592 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1593 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 1594 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 1595 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 1596 | int32_t abs_offset = cur_offset + offset; |
| 1597 | if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1598 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1599 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1600 | << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1603 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1604 | } |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
| 1608 | bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
| 1609 | if (vA > 5) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1610 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1611 | return false; |
| 1612 | } |
| 1613 | uint16_t registers_size = code_item_->registers_size_; |
| 1614 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1615 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1616 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1617 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1618 | return false; |
| 1619 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | return true; |
| 1623 | } |
| 1624 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1625 | bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
| 1626 | uint16_t registers_size = code_item_->registers_size_; |
| 1627 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1628 | // integer overflow when adding them here. |
| 1629 | if (vA + vC > registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1630 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> " |
| 1631 | << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1632 | return false; |
| 1633 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
| 1636 | |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1637 | const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) { |
| 1638 | std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>; |
| 1639 | length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24); |
| 1640 | length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16); |
| 1641 | length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8); |
| 1642 | length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0); |
| 1643 | length_prefixed_gc_map->insert(length_prefixed_gc_map->end(), |
| 1644 | gc_map.begin(), |
| 1645 | gc_map.end()); |
| 1646 | DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size()); |
| 1647 | DCHECK_EQ(gc_map.size(), |
| 1648 | static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) | |
| 1649 | (length_prefixed_gc_map->at(1) << 16) | |
| 1650 | (length_prefixed_gc_map->at(2) << 8) | |
| 1651 | (length_prefixed_gc_map->at(3) << 0))); |
| 1652 | return length_prefixed_gc_map; |
| 1653 | } |
| 1654 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1655 | bool DexVerifier::VerifyCodeFlow() { |
| 1656 | uint16_t registers_size = code_item_->registers_size_; |
| 1657 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1658 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1659 | if (registers_size * insns_size > 4*1024*1024) { |
buzbee | 4922ef9 | 2012-02-24 14:32:20 -0800 | [diff] [blame] | 1660 | LOG(WARNING) << "warning: method is huge (regs=" << registers_size |
| 1661 | << " insns_size=" << insns_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1662 | } |
| 1663 | /* Create and initialize table holding register status */ |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 1664 | reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1665 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1666 | work_line_.reset(new RegisterLine(registers_size, this)); |
| 1667 | saved_line_.reset(new RegisterLine(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1668 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1669 | /* Initialize register types of method arguments. */ |
| 1670 | if (!SetTypesFromSignature()) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1671 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 1672 | fail_messages_ << "Bad signature in " << PrettyMethod(method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
| 1675 | /* Perform code flow verification. */ |
| 1676 | if (!CodeFlowVerifyMethod()) { |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1677 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1678 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1681 | /* Generate a register map and add it to the method. */ |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1682 | UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap()); |
| 1683 | if (map.get() == NULL) { |
| 1684 | DCHECK_NE(failure_, VERIFY_ERROR_NONE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1685 | return false; // Not a real failure, but a failure to encode |
| 1686 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1687 | #ifndef NDEBUG |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1688 | VerifyGcMap(*map); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1689 | #endif |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1690 | const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get())); |
| 1691 | Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex()); |
| 1692 | verifier::DexVerifier::SetGcMap(ref, *gc_map); |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 1693 | |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1694 | method_->SetGcMap(&gc_map->at(0)); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame^] | 1695 | |
| 1696 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 1697 | /* Generate Inferred Register Category for LLVM-based Code Generator */ |
| 1698 | const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap(); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame^] | 1699 | verifier::DexVerifier::SetInferredRegCategoryMap(ref, *table); |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 1700 | #endif |
| 1701 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1702 | return true; |
| 1703 | } |
| 1704 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1705 | void DexVerifier::Dump(std::ostream& os) { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1706 | if (code_item_ == NULL) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1707 | os << "Native method" << std::endl; |
| 1708 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1709 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1710 | DCHECK(code_item_ != NULL); |
| 1711 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1712 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
| 1713 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
Elliott Hughes | aa6e1cd | 2012-01-18 19:26:06 -0800 | [diff] [blame] | 1714 | os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump() |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1715 | << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1716 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1717 | if (reg_line != NULL) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1718 | os << reg_line->Dump() << std::endl; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1719 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1720 | inst = inst->Next(); |
| 1721 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1724 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1725 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1726 | case 'I': |
| 1727 | case 'C': |
| 1728 | case 'S': |
| 1729 | case 'B': |
| 1730 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1731 | case 'F': |
| 1732 | case 'D': |
| 1733 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1734 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1735 | default: |
| 1736 | return false; |
| 1737 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1740 | bool DexVerifier::SetTypesFromSignature() { |
| 1741 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1742 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1743 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1744 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1745 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
| 1746 | //Include the "this" pointer. |
| 1747 | size_t cur_arg = 0; |
| 1748 | if (!method_->IsStatic()) { |
| 1749 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1750 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1751 | // called. |
| 1752 | Class* declaring_class = method_->GetDeclaringClass(); |
| 1753 | if (method_->IsConstructor() && !declaring_class->IsObjectClass()) { |
| 1754 | reg_line->SetRegisterType(arg_start + cur_arg, |
| 1755 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1756 | } else { |
| 1757 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1758 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1759 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1762 | const DexFile::ProtoId& proto_id = |
| 1763 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1764 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1765 | |
| 1766 | for (; iterator.HasNext(); iterator.Next()) { |
| 1767 | const char* descriptor = iterator.GetDescriptor(); |
| 1768 | if (descriptor == NULL) { |
| 1769 | LOG(FATAL) << "Null descriptor"; |
| 1770 | } |
| 1771 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1772 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1773 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1774 | return false; |
| 1775 | } |
| 1776 | switch (descriptor[0]) { |
| 1777 | case 'L': |
| 1778 | case '[': |
| 1779 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1780 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1781 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1782 | // can return without doing anything or call virtual methods). |
| 1783 | { |
| 1784 | const RegType& reg_type = |
| 1785 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1786 | reg_line->SetRegisterType(arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1787 | } |
| 1788 | break; |
| 1789 | case 'Z': |
| 1790 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); |
| 1791 | break; |
| 1792 | case 'C': |
| 1793 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); |
| 1794 | break; |
| 1795 | case 'B': |
| 1796 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); |
| 1797 | break; |
| 1798 | case 'I': |
| 1799 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); |
| 1800 | break; |
| 1801 | case 'S': |
| 1802 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); |
| 1803 | break; |
| 1804 | case 'F': |
| 1805 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); |
| 1806 | break; |
| 1807 | case 'J': |
| 1808 | case 'D': { |
| 1809 | const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double(); |
| 1810 | reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register |
| 1811 | cur_arg++; |
| 1812 | break; |
| 1813 | } |
| 1814 | default: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1815 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1816 | return false; |
| 1817 | } |
| 1818 | cur_arg++; |
| 1819 | } |
| 1820 | if (cur_arg != expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1821 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1822 | return false; |
| 1823 | } |
| 1824 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1825 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1826 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1827 | bool result; |
| 1828 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1829 | result = descriptor[1] == '\0'; |
| 1830 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
| 1831 | size_t i = 0; |
| 1832 | do { |
| 1833 | i++; |
| 1834 | } while (descriptor[i] == '['); // process leading [ |
| 1835 | if (descriptor[i] == 'L') { // object array |
| 1836 | do { |
| 1837 | i++; // find closing ; |
| 1838 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1839 | result = descriptor[i] == ';'; |
| 1840 | } else { // primitive array |
| 1841 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1842 | } |
| 1843 | } else if (descriptor[0] == 'L') { |
| 1844 | // could be more thorough here, but shouldn't be required |
| 1845 | size_t i = 0; |
| 1846 | do { |
| 1847 | i++; |
| 1848 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1849 | result = descriptor[i] == ';'; |
| 1850 | } else { |
| 1851 | result = false; |
| 1852 | } |
| 1853 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1854 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1855 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1856 | } |
| 1857 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1860 | bool DexVerifier::CodeFlowVerifyMethod() { |
| 1861 | const uint16_t* insns = code_item_->insns_; |
| 1862 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1863 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1864 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1865 | insn_flags_[0].SetChanged(); |
| 1866 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1867 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1868 | /* Continue until no instructions are marked "changed". */ |
| 1869 | while (true) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1870 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1871 | uint32_t insn_idx = start_guess; |
| 1872 | for (; insn_idx < insns_size; insn_idx++) { |
| 1873 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1874 | break; |
| 1875 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1876 | if (insn_idx == insns_size) { |
| 1877 | if (start_guess != 0) { |
| 1878 | /* try again, starting from the top */ |
| 1879 | start_guess = 0; |
| 1880 | continue; |
| 1881 | } else { |
| 1882 | /* all flags are clear */ |
| 1883 | break; |
| 1884 | } |
| 1885 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1886 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1887 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1888 | // "changed" flags, we need to load the set of registers from the table. |
| 1889 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1890 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1891 | // target. |
| 1892 | work_insn_idx_ = insn_idx; |
| 1893 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1894 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1895 | } else { |
| 1896 | #ifndef NDEBUG |
| 1897 | /* |
| 1898 | * Sanity check: retrieve the stored register line (assuming |
| 1899 | * a full table) and make sure it actually matches. |
| 1900 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1901 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| 1902 | if (register_line != NULL) { |
| 1903 | if (work_line_->CompareLine(register_line) != 0) { |
| 1904 | Dump(std::cout); |
| 1905 | std::cout << info_messages_.str(); |
| 1906 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1907 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << std::endl |
| 1908 | << " work_line=" << *work_line_ << std::endl |
| 1909 | << " expected=" << *register_line; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1910 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1911 | } |
| 1912 | #endif |
| 1913 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1914 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
| 1915 | fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1916 | return false; |
| 1917 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1918 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1919 | insn_flags_[insn_idx].SetVisited(); |
| 1920 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1921 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1922 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1923 | if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1924 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1925 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1926 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1927 | * down the line, possibly in the verifier. |
| 1928 | * |
| 1929 | * If we've substituted "always throw" instructions into the stream, |
| 1930 | * we are almost certainly going to have some dead code. |
| 1931 | */ |
| 1932 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1933 | uint32_t insn_idx = 0; |
| 1934 | for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1935 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1936 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1937 | * may or may not be preceded by a padding NOP (for alignment). |
| 1938 | */ |
| 1939 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1940 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1941 | insns[insn_idx] == Instruction::kArrayDataSignature || |
| 1942 | (insns[insn_idx] == Instruction::NOP && |
| 1943 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1944 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1945 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1946 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1949 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1950 | if (dead_start < 0) |
| 1951 | dead_start = insn_idx; |
| 1952 | } else if (dead_start >= 0) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1953 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1954 | dead_start = -1; |
| 1955 | } |
| 1956 | } |
| 1957 | if (dead_start >= 0) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1958 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1959 | } |
| 1960 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1961 | return true; |
| 1962 | } |
| 1963 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1964 | bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1965 | #ifdef VERIFIER_STATS |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1966 | if (CurrentInsnFlags().IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1967 | gDvm.verifierStats.instrsReexamined++; |
| 1968 | } else { |
| 1969 | gDvm.verifierStats.instrsExamined++; |
| 1970 | } |
| 1971 | #endif |
| 1972 | |
| 1973 | /* |
| 1974 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1975 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1976 | * control to another statement: |
| 1977 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1978 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1979 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1980 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1981 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1982 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1983 | * throw an exception that is handled by an encompassing "try" |
| 1984 | * block. |
| 1985 | * |
| 1986 | * We can also return, in which case there is no successor instruction |
| 1987 | * from this point. |
| 1988 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1989 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1990 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1991 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1992 | const Instruction* inst = Instruction::At(insns); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1993 | DecodedInstruction dec_insn(inst); |
| 1994 | int opcode_flags = Instruction::Flags(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1995 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1996 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1997 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1998 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1999 | // Generate processing back trace to debug verifier |
Ian Rogers | 5ed29bf | 2011-10-26 12:22:21 -0700 | [diff] [blame] | 2000 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl |
| 2001 | << *work_line_.get() << std::endl; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2002 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2003 | |
| 2004 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2005 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2006 | * can throw an exception, we will copy/merge this into the "catch" |
| 2007 | * address rather than work_line, because we don't want the result |
| 2008 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2009 | * a type) to be visible to the exception handler. |
| 2010 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2011 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2012 | saved_line_->CopyFromLine(work_line_.get()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2013 | } else { |
| 2014 | #ifndef NDEBUG |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2015 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2016 | #endif |
| 2017 | } |
| 2018 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2019 | switch (dec_insn.opcode) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2020 | case Instruction::NOP: |
| 2021 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2022 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2023 | * a signature that looks like a NOP; if we see one of these in |
| 2024 | * the course of executing code then we have a problem. |
| 2025 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2026 | if (dec_insn.vA != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2027 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | } |
| 2029 | break; |
| 2030 | |
| 2031 | case Instruction::MOVE: |
| 2032 | case Instruction::MOVE_FROM16: |
| 2033 | case Instruction::MOVE_16: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2034 | work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2035 | break; |
| 2036 | case Instruction::MOVE_WIDE: |
| 2037 | case Instruction::MOVE_WIDE_FROM16: |
| 2038 | case Instruction::MOVE_WIDE_16: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2039 | work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2040 | break; |
| 2041 | case Instruction::MOVE_OBJECT: |
| 2042 | case Instruction::MOVE_OBJECT_FROM16: |
| 2043 | case Instruction::MOVE_OBJECT_16: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2044 | work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | break; |
| 2046 | |
| 2047 | /* |
| 2048 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2049 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2050 | * might want to hold the result in an actual CPU register, so the |
| 2051 | * Dalvik spec requires that these only appear immediately after an |
| 2052 | * invoke or filled-new-array. |
| 2053 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2054 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2055 | * redundant with the reset done below, but it can make the debug info |
| 2056 | * easier to read in some cases.) |
| 2057 | */ |
| 2058 | case Instruction::MOVE_RESULT: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2059 | work_line_->CopyResultRegister1(dec_insn.vA, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2060 | break; |
| 2061 | case Instruction::MOVE_RESULT_WIDE: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2062 | work_line_->CopyResultRegister2(dec_insn.vA); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | case Instruction::MOVE_RESULT_OBJECT: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2065 | work_line_->CopyResultRegister1(dec_insn.vA, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2066 | break; |
| 2067 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2068 | case Instruction::MOVE_EXCEPTION: { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2069 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2070 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2071 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2072 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2073 | const RegType& res_type = GetCaughtExceptionType(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2074 | work_line_->SetRegisterType(dec_insn.vA, res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2075 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2076 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2077 | case Instruction::RETURN_VOID: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2078 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 2079 | if (!GetMethodReturnType().IsUnknown()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2080 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2081 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2082 | } |
| 2083 | break; |
| 2084 | case Instruction::RETURN: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2085 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2086 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2087 | const RegType& return_type = GetMethodReturnType(); |
| 2088 | if (!return_type.IsCategory1Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2089 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2090 | } else { |
| 2091 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2092 | // Also, it may use integer values for boolean, byte, short, and character return types. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2093 | const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2094 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2095 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2096 | return_type.IsShort() || return_type.IsChar()) && |
| 2097 | src_type.IsInteger())); |
| 2098 | /* check the register contents */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2099 | work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2100 | if (failure_ != VERIFY_ERROR_NONE) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2101 | fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2102 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } |
| 2105 | break; |
| 2106 | case Instruction::RETURN_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2107 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2108 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2109 | const RegType& return_type = GetMethodReturnType(); |
| 2110 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2111 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2112 | } else { |
| 2113 | /* check the register contents */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2114 | work_line_->VerifyRegisterType(dec_insn.vA, return_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2115 | if (failure_ != VERIFY_ERROR_NONE) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2116 | fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2117 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | break; |
| 2121 | case Instruction::RETURN_OBJECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2122 | if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 2123 | const RegType& return_type = GetMethodReturnType(); |
| 2124 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2125 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2126 | } else { |
| 2127 | /* return_type is the *expected* return type, not register value */ |
| 2128 | DCHECK(!return_type.IsZero()); |
| 2129 | DCHECK(!return_type.IsUninitializedReference()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2130 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2131 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 2132 | // instance of the "return_type" |
| 2133 | if (reg_type.IsUninitializedTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2134 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2135 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2136 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2137 | << "', but expected from declaration '" << return_type << "'"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | break; |
| 2142 | |
| 2143 | case Instruction::CONST_4: |
| 2144 | case Instruction::CONST_16: |
| 2145 | case Instruction::CONST: |
| 2146 | /* could be boolean, int, float, or a null reference */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2147 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2148 | break; |
| 2149 | case Instruction::CONST_HIGH16: |
| 2150 | /* could be boolean, int, float, or a null reference */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2151 | work_line_->SetRegisterType(dec_insn.vA, |
| 2152 | reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2153 | break; |
| 2154 | case Instruction::CONST_WIDE_16: |
| 2155 | case Instruction::CONST_WIDE_32: |
| 2156 | case Instruction::CONST_WIDE: |
| 2157 | case Instruction::CONST_WIDE_HIGH16: |
| 2158 | /* could be long or double; resolved upon use */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2159 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2160 | break; |
| 2161 | case Instruction::CONST_STRING: |
| 2162 | case Instruction::CONST_STRING_JUMBO: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2163 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2164 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2165 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2166 | // Get type from instruction if unresolved then we need an access check |
| 2167 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2168 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2169 | // Register holds class, ie its type is class, but on error we keep it Unknown |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2170 | work_line_->SetRegisterType(dec_insn.vA, |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2171 | res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2172 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2173 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2174 | case Instruction::MONITOR_ENTER: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2175 | work_line_->PushMonitor(dec_insn.vA, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2176 | break; |
| 2177 | case Instruction::MONITOR_EXIT: |
| 2178 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2179 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2180 | * but when they do they act as if they succeeded and the PC is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2181 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2182 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2183 | * feature that Dalvik doesn't support.) |
| 2184 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2185 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2186 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2187 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2188 | * structured locking checks are working, the former would have |
| 2189 | * failed on the -enter instruction, and the latter is impossible. |
| 2190 | * |
| 2191 | * This is fortunate, because issue 3221411 prevents us from |
| 2192 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2193 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2194 | * some catch blocks (which will show up as "dead" code when |
| 2195 | * we skip them here); if we can't, then the code path could be |
| 2196 | * "live" so we still need to check it. |
| 2197 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2198 | opcode_flags &= ~Instruction::kThrow; |
| 2199 | work_line_->PopMonitor(dec_insn.vA); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2200 | break; |
| 2201 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2202 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2203 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2204 | /* |
| 2205 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2206 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2207 | * |
| 2208 | * If it fails, an exception is thrown, which we deal with later by ignoring the update to |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2209 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2210 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2211 | bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2212 | const RegType& res_type = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2213 | ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC); |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2214 | if (res_type.IsUnknown()) { |
| 2215 | CHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 2216 | break; // couldn't resolve class |
| 2217 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2218 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2219 | const RegType& orig_type = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2220 | work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2221 | if (!res_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2222 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2223 | } else if (!orig_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2224 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2225 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2226 | if (is_checkcast) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2227 | work_line_->SetRegisterType(dec_insn.vA, res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2228 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2229 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2230 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2231 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2232 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2233 | } |
| 2234 | case Instruction::ARRAY_LENGTH: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2235 | const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2236 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2237 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2238 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2239 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2240 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | break; |
| 2244 | } |
| 2245 | case Instruction::NEW_INSTANCE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2246 | const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2247 | if (res_type.IsUnknown()) { |
| 2248 | CHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 2249 | break; // couldn't resolve class |
| 2250 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2251 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2252 | // can't create an instance of an interface or abstract class */ |
| 2253 | if (!res_type.IsInstantiableTypes()) { |
| 2254 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2255 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2256 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2257 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| 2258 | // Any registers holding previous allocations from this address that have not yet been |
| 2259 | // initialized must be marked invalid. |
| 2260 | work_line_->MarkUninitRefsAsInvalid(uninit_type); |
| 2261 | // add the new uninitialized reference to the register state |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2262 | work_line_->SetRegisterType(dec_insn.vA, uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2263 | } |
| 2264 | break; |
| 2265 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2266 | case Instruction::NEW_ARRAY: |
| 2267 | VerifyNewArray(dec_insn, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2268 | break; |
| 2269 | case Instruction::FILLED_NEW_ARRAY: |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2270 | VerifyNewArray(dec_insn, true, false); |
| 2271 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2272 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2273 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 2274 | VerifyNewArray(dec_insn, true, true); |
| 2275 | just_set_result = true; // Filled new array range sets result register |
| 2276 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2277 | case Instruction::CMPL_FLOAT: |
| 2278 | case Instruction::CMPG_FLOAT: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2279 | if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2280 | break; |
| 2281 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2282 | if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2283 | break; |
| 2284 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2285 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2286 | break; |
| 2287 | case Instruction::CMPL_DOUBLE: |
| 2288 | case Instruction::CMPG_DOUBLE: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2289 | if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2290 | break; |
| 2291 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2292 | if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2293 | break; |
| 2294 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2295 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2296 | break; |
| 2297 | case Instruction::CMP_LONG: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2298 | if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2299 | break; |
| 2300 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2301 | if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2302 | break; |
| 2303 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2304 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2305 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2306 | case Instruction::THROW: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2307 | const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2308 | if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2309 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2310 | } |
| 2311 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2312 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2313 | case Instruction::GOTO: |
| 2314 | case Instruction::GOTO_16: |
| 2315 | case Instruction::GOTO_32: |
| 2316 | /* no effect on or use of registers */ |
| 2317 | break; |
| 2318 | |
| 2319 | case Instruction::PACKED_SWITCH: |
| 2320 | case Instruction::SPARSE_SWITCH: |
| 2321 | /* verify that vAA is an integer, or can be converted to one */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2322 | work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | break; |
| 2324 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2325 | case Instruction::FILL_ARRAY_DATA: { |
| 2326 | /* Similar to the verification done for APUT */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2327 | const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2328 | /* array_type can be null if the reg type is Zero */ |
| 2329 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2330 | if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2331 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2332 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2333 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 2334 | method_->GetDeclaringClass()->GetClassLoader()); |
| 2335 | DCHECK(!component_type.IsUnknown()); |
| 2336 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2337 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2338 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2339 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2340 | // Now verify if the element width in the table matches the element width declared in |
| 2341 | // the array |
| 2342 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 2343 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2344 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2345 | } else { |
| 2346 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2347 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2348 | // in the table and expected from the array class. |
| 2349 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2350 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2351 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2352 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2358 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2359 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2360 | case Instruction::IF_NE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2361 | const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA); |
| 2362 | const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2363 | bool mismatch = false; |
| 2364 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2365 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2366 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2367 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2368 | } else { // both integral? |
| 2369 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2370 | } |
| 2371 | if (mismatch) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2372 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2 |
| 2373 | << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2374 | } |
| 2375 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2376 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2377 | case Instruction::IF_LT: |
| 2378 | case Instruction::IF_GE: |
| 2379 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2380 | case Instruction::IF_LE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2381 | const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA); |
| 2382 | const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2383 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2384 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2385 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2386 | } |
| 2387 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2388 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2389 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2390 | case Instruction::IF_NEZ: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2391 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2392 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2393 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2394 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2395 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2396 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2397 | case Instruction::IF_LTZ: |
| 2398 | case Instruction::IF_GEZ: |
| 2399 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2400 | case Instruction::IF_LEZ: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2401 | const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2402 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2403 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2404 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2405 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2406 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2407 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2408 | case Instruction::AGET_BOOLEAN: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2409 | VerifyAGet(dec_insn, reg_types_.Boolean(), true); |
| 2410 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2411 | case Instruction::AGET_BYTE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2412 | VerifyAGet(dec_insn, reg_types_.Byte(), true); |
| 2413 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2414 | case Instruction::AGET_CHAR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2415 | VerifyAGet(dec_insn, reg_types_.Char(), true); |
| 2416 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2417 | case Instruction::AGET_SHORT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2418 | VerifyAGet(dec_insn, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2419 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2420 | case Instruction::AGET: |
| 2421 | VerifyAGet(dec_insn, reg_types_.Integer(), true); |
| 2422 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2423 | case Instruction::AGET_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2424 | VerifyAGet(dec_insn, reg_types_.Long(), true); |
| 2425 | break; |
| 2426 | case Instruction::AGET_OBJECT: |
| 2427 | VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2428 | break; |
| 2429 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2430 | case Instruction::APUT_BOOLEAN: |
| 2431 | VerifyAPut(dec_insn, reg_types_.Boolean(), true); |
| 2432 | break; |
| 2433 | case Instruction::APUT_BYTE: |
| 2434 | VerifyAPut(dec_insn, reg_types_.Byte(), true); |
| 2435 | break; |
| 2436 | case Instruction::APUT_CHAR: |
| 2437 | VerifyAPut(dec_insn, reg_types_.Char(), true); |
| 2438 | break; |
| 2439 | case Instruction::APUT_SHORT: |
| 2440 | VerifyAPut(dec_insn, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2441 | break; |
| 2442 | case Instruction::APUT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2443 | VerifyAPut(dec_insn, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2444 | break; |
| 2445 | case Instruction::APUT_WIDE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2446 | VerifyAPut(dec_insn, reg_types_.Long(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2447 | break; |
| 2448 | case Instruction::APUT_OBJECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2449 | VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2450 | break; |
| 2451 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2452 | case Instruction::IGET_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2453 | VerifyISGet(dec_insn, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2454 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2455 | case Instruction::IGET_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2456 | VerifyISGet(dec_insn, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2457 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2458 | case Instruction::IGET_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2459 | VerifyISGet(dec_insn, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2460 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2461 | case Instruction::IGET_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2462 | VerifyISGet(dec_insn, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2463 | break; |
| 2464 | case Instruction::IGET: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2465 | VerifyISGet(dec_insn, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2466 | break; |
| 2467 | case Instruction::IGET_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2468 | VerifyISGet(dec_insn, reg_types_.Long(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2469 | break; |
| 2470 | case Instruction::IGET_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2471 | VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2472 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2473 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2474 | case Instruction::IPUT_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2475 | VerifyISPut(dec_insn, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2476 | break; |
| 2477 | case Instruction::IPUT_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2478 | VerifyISPut(dec_insn, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2479 | break; |
| 2480 | case Instruction::IPUT_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2481 | VerifyISPut(dec_insn, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2482 | break; |
| 2483 | case Instruction::IPUT_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2484 | VerifyISPut(dec_insn, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2485 | break; |
| 2486 | case Instruction::IPUT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2487 | VerifyISPut(dec_insn, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2488 | break; |
| 2489 | case Instruction::IPUT_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2490 | VerifyISPut(dec_insn, reg_types_.Long(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2491 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2492 | case Instruction::IPUT_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2493 | VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2494 | break; |
| 2495 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2496 | case Instruction::SGET_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2497 | VerifyISGet(dec_insn, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2498 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2499 | case Instruction::SGET_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2500 | VerifyISGet(dec_insn, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2501 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2502 | case Instruction::SGET_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2503 | VerifyISGet(dec_insn, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2504 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2505 | case Instruction::SGET_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2506 | VerifyISGet(dec_insn, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2507 | break; |
| 2508 | case Instruction::SGET: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2509 | VerifyISGet(dec_insn, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2510 | break; |
| 2511 | case Instruction::SGET_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2512 | VerifyISGet(dec_insn, reg_types_.Long(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2513 | break; |
| 2514 | case Instruction::SGET_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2515 | VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2516 | break; |
| 2517 | |
| 2518 | case Instruction::SPUT_BOOLEAN: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2519 | VerifyISPut(dec_insn, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2520 | break; |
| 2521 | case Instruction::SPUT_BYTE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2522 | VerifyISPut(dec_insn, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2523 | break; |
| 2524 | case Instruction::SPUT_CHAR: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2525 | VerifyISPut(dec_insn, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2526 | break; |
| 2527 | case Instruction::SPUT_SHORT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2528 | VerifyISPut(dec_insn, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2529 | break; |
| 2530 | case Instruction::SPUT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2531 | VerifyISPut(dec_insn, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2532 | break; |
| 2533 | case Instruction::SPUT_WIDE: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2534 | VerifyISPut(dec_insn, reg_types_.Long(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2535 | break; |
| 2536 | case Instruction::SPUT_OBJECT: |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 2537 | VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2538 | break; |
| 2539 | |
| 2540 | case Instruction::INVOKE_VIRTUAL: |
| 2541 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2542 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2543 | case Instruction::INVOKE_SUPER_RANGE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2544 | bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2545 | dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE); |
| 2546 | bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER || |
| 2547 | dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2548 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super); |
| 2549 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2550 | const char* descriptor; |
| 2551 | if (called_method == NULL) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2552 | uint32_t method_idx = dec_insn.vB; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2553 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2554 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2555 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2556 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2557 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2558 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2559 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2560 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2561 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2562 | just_set_result = true; |
| 2563 | } |
| 2564 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2565 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2566 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2567 | case Instruction::INVOKE_DIRECT_RANGE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2568 | bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2569 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false); |
| 2570 | if (failure_ == VERIFY_ERROR_NONE) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2571 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2572 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2573 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2574 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2575 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2576 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2577 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2578 | bool is_constructor; |
| 2579 | if (called_method != NULL) { |
| 2580 | is_constructor = called_method->IsConstructor(); |
| 2581 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2582 | uint32_t method_idx = dec_insn.vB; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2583 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2584 | const char* name = dex_file_->GetMethodName(method_id); |
| 2585 | is_constructor = strcmp(name, "<init>") == 0; |
| 2586 | } |
| 2587 | if (is_constructor) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2588 | const RegType& this_type = work_line_->GetInvocationThis(dec_insn); |
| 2589 | if (failure_ != VERIFY_ERROR_NONE) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2590 | break; |
| 2591 | |
| 2592 | /* no null refs allowed (?) */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2593 | if (this_type.IsZero()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2594 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2595 | break; |
| 2596 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2597 | if (called_method != NULL) { |
| 2598 | Class* this_class = this_type.GetClass(); |
| 2599 | DCHECK(this_class != NULL); |
| 2600 | /* must be in same class or in superclass */ |
| 2601 | if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) { |
| 2602 | if (this_class != method_->GetDeclaringClass()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2603 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2604 | << "invoke-direct <init> on super only allowed for 'this' in <init>"; |
| 2605 | break; |
| 2606 | } |
| 2607 | } else if (called_method->GetDeclaringClass() != this_class) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2608 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-direct <init> must be on current class or super"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2609 | break; |
| 2610 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* arg must be an uninitialized reference */ |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 2614 | if (!this_type.IsUninitializedTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2615 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2616 | << this_type; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2617 | break; |
| 2618 | } |
| 2619 | |
| 2620 | /* |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 2621 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2622 | * registers that have the same object instance in them, not just the "this" register. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2623 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2624 | work_line_->MarkRefsAsInitialized(this_type); |
| 2625 | if (failure_ != VERIFY_ERROR_NONE) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2626 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2627 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2628 | const char* descriptor; |
| 2629 | if (called_method == NULL) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2630 | uint32_t method_idx = dec_insn.vB; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2631 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2632 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2633 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2634 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2635 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2636 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2637 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2638 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2639 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2640 | just_set_result = true; |
| 2641 | } |
| 2642 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2643 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2644 | case Instruction::INVOKE_STATIC: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2645 | case Instruction::INVOKE_STATIC_RANGE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2646 | bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2647 | Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false); |
| 2648 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2649 | const char* descriptor; |
| 2650 | if (called_method == NULL) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2651 | uint32_t method_idx = dec_insn.vB; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2652 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2653 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2654 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2655 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2656 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2657 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2658 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2659 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2660 | work_line_->SetResultRegisterType(return_type); |
| 2661 | just_set_result = true; |
| 2662 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2663 | } |
| 2664 | break; |
| 2665 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2666 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2667 | bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2668 | Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false); |
| 2669 | if (failure_ == VERIFY_ERROR_NONE) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2670 | if (abs_method != NULL) { |
| 2671 | Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | f3c1f78 | 2011-11-02 14:12:15 -0700 | [diff] [blame] | 2672 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2673 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2674 | << PrettyMethod(abs_method) << "'"; |
| 2675 | break; |
| 2676 | } |
| 2677 | } |
| 2678 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2679 | * interface or Object (see comments in RegType::JoinClass). |
| 2680 | */ |
| 2681 | const RegType& this_type = work_line_->GetInvocationThis(dec_insn); |
| 2682 | if (failure_ == VERIFY_ERROR_NONE) { |
| 2683 | if (this_type.IsZero()) { |
| 2684 | /* null pointer always passes (and always fails at runtime) */ |
| 2685 | } else { |
| 2686 | if (this_type.IsUninitializedTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2687 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2688 | << this_type; |
| 2689 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2690 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2691 | // In the past we have tried to assert that "called_interface" is assignable |
| 2692 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2693 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2694 | // implemented by "this_type". For example, two classes may implement the same |
| 2695 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2696 | // join will set "this_type" to the parent class and a test that this implements |
| 2697 | // the interface will incorrectly fail. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2698 | } |
| 2699 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2700 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2701 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2702 | * the type information is in the abstract method, so we're good. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2703 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2704 | const char* descriptor; |
| 2705 | if (abs_method == NULL) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2706 | uint32_t method_idx = dec_insn.vB; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2707 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2708 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2709 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2710 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2711 | descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2712 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2713 | const RegType& return_type = |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2714 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
| 2715 | work_line_->SetResultRegisterType(return_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2716 | work_line_->SetResultRegisterType(return_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2717 | just_set_result = true; |
| 2718 | } |
| 2719 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2720 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2721 | case Instruction::NEG_INT: |
| 2722 | case Instruction::NOT_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2723 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | break; |
| 2725 | case Instruction::NEG_LONG: |
| 2726 | case Instruction::NOT_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2727 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2728 | break; |
| 2729 | case Instruction::NEG_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2730 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2731 | break; |
| 2732 | case Instruction::NEG_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2733 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2734 | break; |
| 2735 | case Instruction::INT_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2736 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2739 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2740 | break; |
| 2741 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2742 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2743 | break; |
| 2744 | case Instruction::LONG_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2745 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2746 | break; |
| 2747 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2748 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2749 | break; |
| 2750 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2751 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2752 | break; |
| 2753 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2754 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2755 | break; |
| 2756 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2757 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2758 | break; |
| 2759 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2760 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2761 | break; |
| 2762 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2763 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2764 | break; |
| 2765 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2766 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2767 | break; |
| 2768 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2769 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2770 | break; |
| 2771 | case Instruction::INT_TO_BYTE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2772 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2773 | break; |
| 2774 | case Instruction::INT_TO_CHAR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2775 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2776 | break; |
| 2777 | case Instruction::INT_TO_SHORT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2778 | work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2779 | break; |
| 2780 | |
| 2781 | case Instruction::ADD_INT: |
| 2782 | case Instruction::SUB_INT: |
| 2783 | case Instruction::MUL_INT: |
| 2784 | case Instruction::REM_INT: |
| 2785 | case Instruction::DIV_INT: |
| 2786 | case Instruction::SHL_INT: |
| 2787 | case Instruction::SHR_INT: |
| 2788 | case Instruction::USHR_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2789 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2790 | break; |
| 2791 | case Instruction::AND_INT: |
| 2792 | case Instruction::OR_INT: |
| 2793 | case Instruction::XOR_INT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2794 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2795 | break; |
| 2796 | case Instruction::ADD_LONG: |
| 2797 | case Instruction::SUB_LONG: |
| 2798 | case Instruction::MUL_LONG: |
| 2799 | case Instruction::DIV_LONG: |
| 2800 | case Instruction::REM_LONG: |
| 2801 | case Instruction::AND_LONG: |
| 2802 | case Instruction::OR_LONG: |
| 2803 | case Instruction::XOR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2804 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2805 | break; |
| 2806 | case Instruction::SHL_LONG: |
| 2807 | case Instruction::SHR_LONG: |
| 2808 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2809 | /* shift distance is Int, making these different from other binary operations */ |
| 2810 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2811 | break; |
| 2812 | case Instruction::ADD_FLOAT: |
| 2813 | case Instruction::SUB_FLOAT: |
| 2814 | case Instruction::MUL_FLOAT: |
| 2815 | case Instruction::DIV_FLOAT: |
| 2816 | case Instruction::REM_FLOAT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2818 | break; |
| 2819 | case Instruction::ADD_DOUBLE: |
| 2820 | case Instruction::SUB_DOUBLE: |
| 2821 | case Instruction::MUL_DOUBLE: |
| 2822 | case Instruction::DIV_DOUBLE: |
| 2823 | case Instruction::REM_DOUBLE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2824 | work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2825 | break; |
| 2826 | case Instruction::ADD_INT_2ADDR: |
| 2827 | case Instruction::SUB_INT_2ADDR: |
| 2828 | case Instruction::MUL_INT_2ADDR: |
| 2829 | case Instruction::REM_INT_2ADDR: |
| 2830 | case Instruction::SHL_INT_2ADDR: |
| 2831 | case Instruction::SHR_INT_2ADDR: |
| 2832 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2833 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2834 | break; |
| 2835 | case Instruction::AND_INT_2ADDR: |
| 2836 | case Instruction::OR_INT_2ADDR: |
| 2837 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2838 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2839 | break; |
| 2840 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2841 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2842 | break; |
| 2843 | case Instruction::ADD_LONG_2ADDR: |
| 2844 | case Instruction::SUB_LONG_2ADDR: |
| 2845 | case Instruction::MUL_LONG_2ADDR: |
| 2846 | case Instruction::DIV_LONG_2ADDR: |
| 2847 | case Instruction::REM_LONG_2ADDR: |
| 2848 | case Instruction::AND_LONG_2ADDR: |
| 2849 | case Instruction::OR_LONG_2ADDR: |
| 2850 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2851 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2852 | break; |
| 2853 | case Instruction::SHL_LONG_2ADDR: |
| 2854 | case Instruction::SHR_LONG_2ADDR: |
| 2855 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2856 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2857 | break; |
| 2858 | case Instruction::ADD_FLOAT_2ADDR: |
| 2859 | case Instruction::SUB_FLOAT_2ADDR: |
| 2860 | case Instruction::MUL_FLOAT_2ADDR: |
| 2861 | case Instruction::DIV_FLOAT_2ADDR: |
| 2862 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2863 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2864 | break; |
| 2865 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2866 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2867 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2868 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2869 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2870 | work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2871 | break; |
| 2872 | case Instruction::ADD_INT_LIT16: |
| 2873 | case Instruction::RSUB_INT: |
| 2874 | case Instruction::MUL_INT_LIT16: |
| 2875 | case Instruction::DIV_INT_LIT16: |
| 2876 | case Instruction::REM_INT_LIT16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2877 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2878 | break; |
| 2879 | case Instruction::AND_INT_LIT16: |
| 2880 | case Instruction::OR_INT_LIT16: |
| 2881 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2882 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2883 | break; |
| 2884 | case Instruction::ADD_INT_LIT8: |
| 2885 | case Instruction::RSUB_INT_LIT8: |
| 2886 | case Instruction::MUL_INT_LIT8: |
| 2887 | case Instruction::DIV_INT_LIT8: |
| 2888 | case Instruction::REM_INT_LIT8: |
| 2889 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2890 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2891 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2892 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2893 | break; |
| 2894 | case Instruction::AND_INT_LIT8: |
| 2895 | case Instruction::OR_INT_LIT8: |
| 2896 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2897 | work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2898 | break; |
| 2899 | |
| 2900 | /* |
| 2901 | * This falls into the general category of "optimized" instructions, |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2902 | * which don't generally appear during verification. Because it's |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2903 | * inserted in the course of verification, we can expect to see it here. |
| 2904 | */ |
jeffhao | b4df514 | 2011-09-19 20:25:32 -0700 | [diff] [blame] | 2905 | case Instruction::THROW_VERIFICATION_ERROR: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2906 | break; |
| 2907 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2908 | /* These should never appear during verification. */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2909 | case Instruction::UNUSED_EE: |
| 2910 | case Instruction::UNUSED_EF: |
| 2911 | case Instruction::UNUSED_F2: |
| 2912 | case Instruction::UNUSED_F3: |
| 2913 | case Instruction::UNUSED_F4: |
| 2914 | case Instruction::UNUSED_F5: |
| 2915 | case Instruction::UNUSED_F6: |
| 2916 | case Instruction::UNUSED_F7: |
| 2917 | case Instruction::UNUSED_F8: |
| 2918 | case Instruction::UNUSED_F9: |
| 2919 | case Instruction::UNUSED_FA: |
| 2920 | case Instruction::UNUSED_FB: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2921 | case Instruction::UNUSED_F0: |
| 2922 | case Instruction::UNUSED_F1: |
| 2923 | case Instruction::UNUSED_E3: |
| 2924 | case Instruction::UNUSED_E8: |
| 2925 | case Instruction::UNUSED_E7: |
| 2926 | case Instruction::UNUSED_E4: |
| 2927 | case Instruction::UNUSED_E9: |
| 2928 | case Instruction::UNUSED_FC: |
| 2929 | case Instruction::UNUSED_E5: |
| 2930 | case Instruction::UNUSED_EA: |
| 2931 | case Instruction::UNUSED_FD: |
| 2932 | case Instruction::UNUSED_E6: |
| 2933 | case Instruction::UNUSED_EB: |
| 2934 | case Instruction::UNUSED_FE: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2935 | case Instruction::UNUSED_3E: |
| 2936 | case Instruction::UNUSED_3F: |
| 2937 | case Instruction::UNUSED_40: |
| 2938 | case Instruction::UNUSED_41: |
| 2939 | case Instruction::UNUSED_42: |
| 2940 | case Instruction::UNUSED_43: |
| 2941 | case Instruction::UNUSED_73: |
| 2942 | case Instruction::UNUSED_79: |
| 2943 | case Instruction::UNUSED_7A: |
| 2944 | case Instruction::UNUSED_EC: |
| 2945 | case Instruction::UNUSED_FF: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2946 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2947 | break; |
| 2948 | |
| 2949 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2950 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2951 | * complain if an instruction is missing (which is desirable). |
| 2952 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2953 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2954 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2955 | if (failure_ != VERIFY_ERROR_NONE) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2956 | if (failure_ == VERIFY_ERROR_BAD_CLASS_HARD || failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2957 | /* immediate failure, reject class */ |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 2958 | fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2959 | return false; |
| 2960 | } else { |
| 2961 | /* replace opcode and continue on */ |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 2962 | fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2963 | ReplaceFailingInstruction(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2964 | /* IMPORTANT: method->insns may have been changed */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2965 | insns = code_item_->insns_ + work_insn_idx_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2966 | /* continue on as if we just handled a throw-verification-error */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2967 | failure_ = VERIFY_ERROR_NONE; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2968 | opcode_flags = Instruction::kThrow; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2969 | } |
| 2970 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2971 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2972 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2973 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2974 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2975 | */ |
| 2976 | if (!just_set_result) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2977 | work_line_->SetResultTypeToUnknown(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 2980 | /* Handle "continue". Tag the next consecutive instruction. */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2981 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2982 | uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits(); |
| 2983 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2984 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2985 | return false; |
| 2986 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2987 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 2988 | // next instruction isn't one. |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2989 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2990 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2991 | } |
| 2992 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| 2993 | if (next_line != NULL) { |
| 2994 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 2995 | // needed. |
| 2996 | if (!UpdateRegisters(next_insn_idx, work_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2997 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2998 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2999 | } else { |
| 3000 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3001 | * We're not recording register data for the next instruction, so we don't know what the prior |
| 3002 | * state was. We have to assume that something has changed and re-evaluate it. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3003 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3004 | insn_flags_[next_insn_idx].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3009 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3010 | * |
| 3011 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3012 | * state of the register when the branch is taken or not taken. For example, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3013 | * somebody could get a reference field, check it for zero, and if the |
| 3014 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3015 | * since the value is known to be zero. We do not currently account for |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3016 | * that, and will reject the code. |
| 3017 | * |
| 3018 | * TODO: avoid re-fetching the branch target |
| 3019 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3020 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3021 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3022 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3023 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3024 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3025 | return false; |
| 3026 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3027 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3028 | if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3029 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3030 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3031 | /* update branch target, set "changed" if appropriate */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3032 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3033 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3034 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3038 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3039 | * |
| 3040 | * We've already verified that the table is structurally sound, so we |
| 3041 | * just need to walk through and tag the targets. |
| 3042 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3043 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3044 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 3045 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3046 | int switch_count = switch_insns[1]; |
| 3047 | int offset_to_targets, targ; |
| 3048 | |
| 3049 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3050 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3051 | offset_to_targets = 4; |
| 3052 | } else { |
| 3053 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3054 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3055 | offset_to_targets = 2 + 2 * switch_count; |
| 3056 | } |
| 3057 | |
| 3058 | /* verify each switch target */ |
| 3059 | for (targ = 0; targ < switch_count; targ++) { |
| 3060 | int offset; |
| 3061 | uint32_t abs_offset; |
| 3062 | |
| 3063 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3064 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 3065 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3066 | abs_offset = work_insn_idx_ + offset; |
| 3067 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3068 | if (!CheckNotMoveException(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3069 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3070 | } |
| 3071 | if (!UpdateRegisters(abs_offset, work_line_.get())) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3072 | return false; |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3077 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3078 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3079 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3080 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3081 | bool within_catch_all = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3082 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3083 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3084 | for (; iterator.HasNext(); iterator.Next()) { |
| 3085 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3086 | within_catch_all = true; |
| 3087 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3088 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3089 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3090 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3091 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3092 | */ |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3093 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3094 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3095 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3099 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3100 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3101 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3102 | if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3103 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3104 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3105 | * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3106 | * it will do so before grabbing the lock). |
| 3107 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3108 | if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3109 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3110 | << "expected to be within a catch-all for an instruction where a monitor is held"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3111 | return false; |
| 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3116 | /* If we're returning from the method, make sure monitor stack is empty. */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3117 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3118 | if (!work_line_->VerifyMonitorStackEmpty()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3119 | return false; |
| 3120 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3124 | * Update start_guess. Advance to the next instruction of that's |
| 3125 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3126 | * neither of those exists we're in a return or throw; leave start_guess |
| 3127 | * alone and let the caller sort it out. |
| 3128 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3129 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3130 | *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3131 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3132 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3133 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3134 | } |
| 3135 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3136 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 3137 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3138 | |
| 3139 | return true; |
| 3140 | } |
| 3141 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3142 | const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3143 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3144 | Class* referrer = method_->GetDeclaringClass(); |
| 3145 | Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx); |
| 3146 | const RegType& result = |
| 3147 | klass != NULL ? reg_types_.FromClass(klass) |
| 3148 | : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor); |
| 3149 | if (klass == NULL && !result.IsUnresolvedTypes()) { |
| 3150 | method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3151 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 3152 | if (result.IsUnknown()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3153 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing unknown class in " << PrettyDescriptor(referrer); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 3154 | return result; |
| 3155 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3156 | // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to |
| 3157 | // check at runtime if access is allowed and so pass here. |
| 3158 | if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) { |
| 3159 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3160 | << PrettyDescriptor(referrer) << "' -> '" |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3161 | << result << "'"; |
| 3162 | return reg_types_.Unknown(); |
| 3163 | } else { |
| 3164 | return result; |
| 3165 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3166 | } |
| 3167 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3168 | const RegType& DexVerifier::GetCaughtExceptionType() { |
| 3169 | const RegType* common_super = NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3170 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3171 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3172 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3173 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3174 | CatchHandlerIterator iterator(handlers_ptr); |
| 3175 | for (; iterator.HasNext(); iterator.Next()) { |
| 3176 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3177 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3178 | common_super = ®_types_.JavaLangThrowable(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3179 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3180 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3181 | if (common_super == NULL) { |
| 3182 | // Unconditionally assign for the first handler. We don't assert this is a Throwable |
| 3183 | // as that is caught at runtime |
| 3184 | common_super = &exception; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3185 | } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3186 | // We don't know enough about the type and the common path merge will result in |
| 3187 | // Conflict. Fail here knowing the correct thing can be done at runtime. |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3188 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3189 | return reg_types_.Unknown(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3190 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3191 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3192 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3193 | common_super = &common_super->Merge(exception, ®_types_); |
| 3194 | CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3199 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | if (common_super == NULL) { |
| 3203 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3204 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3205 | return reg_types_.Unknown(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3206 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3207 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3210 | Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3211 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 3212 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
| 3213 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3214 | fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id); |
| 3215 | return NULL; |
| 3216 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3217 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3218 | return NULL; // Can't resolve Class so no more to do here |
| 3219 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3220 | Class* klass = klass_type.GetClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3221 | Class* referrer = method_->GetDeclaringClass(); |
| 3222 | DexCache* dex_cache = referrer->GetDexCache(); |
| 3223 | Method* res_method = dex_cache->GetResolvedMethod(method_idx); |
| 3224 | if (res_method == NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3225 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3226 | std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL)); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3227 | |
| 3228 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3229 | res_method = klass->FindDirectMethod(name, signature); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3230 | } else if (method_type == METHOD_INTERFACE) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3231 | res_method = klass->FindInterfaceMethod(name, signature); |
| 3232 | } else { |
| 3233 | res_method = klass->FindVirtualMethod(name, signature); |
| 3234 | } |
| 3235 | if (res_method != NULL) { |
| 3236 | dex_cache->SetResolvedMethod(method_idx, res_method); |
| 3237 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3238 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3239 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3240 | // a class has changed, and will be flagged as an error in later checks. |
| 3241 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
| 3242 | res_method = klass->FindDirectMethod(name, signature); |
| 3243 | } |
| 3244 | if (res_method == NULL) { |
| 3245 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 3246 | << PrettyDescriptor(klass) << "." << name |
| 3247 | << " " << signature; |
| 3248 | return NULL; |
| 3249 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3250 | } |
| 3251 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3252 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3253 | // enforce them here. |
| 3254 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3255 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 3256 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3257 | return NULL; |
| 3258 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3259 | // Disallow any calls to class initializers. |
| 3260 | if (MethodHelper(res_method).IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3261 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 3262 | << PrettyMethod(res_method); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3263 | return NULL; |
| 3264 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3265 | // Check if access is allowed. |
| 3266 | if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
| 3267 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
| 3268 | << " from " << PrettyDescriptor(referrer) << ")"; |
| 3269 | return NULL; |
| 3270 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3271 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 3272 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3273 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3274 | << PrettyMethod(res_method); |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3275 | return NULL; |
| 3276 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3277 | // Check that interface methods match interface classes. |
| 3278 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 3279 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 3280 | << " is in an interface class " << PrettyClass(klass); |
| 3281 | return NULL; |
| 3282 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 3283 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 3284 | << " is in a non-interface class " << PrettyClass(klass); |
| 3285 | return NULL; |
| 3286 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3287 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3288 | // target method. |
| 3289 | if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || |
| 3290 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3291 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3292 | ) { |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 3293 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of " |
| 3294 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3295 | return NULL; |
| 3296 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3297 | return res_method; |
| 3298 | } |
| 3299 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3300 | Method* DexVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn, |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3301 | MethodType method_type, bool is_range, bool is_super) { |
| 3302 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3303 | // we're making. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3304 | Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3305 | if (res_method == NULL) { // error or class is unresolved |
| 3306 | return NULL; |
| 3307 | } |
| 3308 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3309 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3310 | // has a vtable entry for the target method. |
| 3311 | if (is_super) { |
| 3312 | DCHECK(method_type == METHOD_VIRTUAL); |
| 3313 | Class* super = method_->GetDeclaringClass()->GetSuperClass(); |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 3314 | if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3315 | if (super == NULL) { // Only Object has no super class |
| 3316 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_) |
| 3317 | << " to super " << PrettyMethod(res_method); |
| 3318 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3319 | MethodHelper mh(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3320 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_) |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3321 | << " to super " << PrettyDescriptor(super) |
| 3322 | << "." << mh.GetName() |
| 3323 | << mh.GetSignature(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3324 | } |
| 3325 | return NULL; |
| 3326 | } |
| 3327 | } |
| 3328 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3329 | // match the call to the signature. Also, we might might be calling through an abstract method |
| 3330 | // definition (which doesn't have register count values). |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3331 | size_t expected_args = dec_insn.vA; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3332 | /* caught by static verifier */ |
| 3333 | DCHECK(is_range || expected_args <= 5); |
| 3334 | if (expected_args > code_item_->outs_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3335 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3336 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3337 | return NULL; |
| 3338 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3339 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3340 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3341 | * Check the "this" argument, which must be an instance of the class |
| 3342 | * that declared the method. For an interface class, we don't do the |
| 3343 | * full interface merge, so we can't do a rigorous check here (which |
| 3344 | * is okay since we have to do it at runtime). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3345 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3346 | size_t actual_args = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3347 | if (!res_method->IsStatic()) { |
| 3348 | const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn); |
| 3349 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3350 | return NULL; |
| 3351 | } |
| 3352 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3353 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3354 | return NULL; |
| 3355 | } |
| 3356 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3357 | const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass()); |
| 3358 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3359 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3360 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3361 | return NULL; |
| 3362 | } |
| 3363 | } |
| 3364 | actual_args++; |
| 3365 | } |
| 3366 | /* |
| 3367 | * Process the target method's signature. This signature may or may not |
| 3368 | * have been verified, so we can't assume it's properly formed. |
| 3369 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3370 | MethodHelper mh(res_method); |
| 3371 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3372 | size_t params_size = params == NULL ? 0 : params->Size(); |
| 3373 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3374 | if (actual_args >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3375 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3376 | << "'. Expected " << expected_args << " arguments, processing argument " << actual_args |
| 3377 | << " (where longs/doubles count twice)."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3378 | return NULL; |
| 3379 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3380 | const char* descriptor = |
| 3381 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3382 | if (descriptor == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3383 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3384 | << " missing signature component"; |
| 3385 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3386 | } |
| 3387 | const RegType& reg_type = |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3388 | reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3389 | uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args]; |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3390 | if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
| 3391 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3392 | } |
| 3393 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3394 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3395 | if (actual_args != expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3396 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3397 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3398 | return NULL; |
| 3399 | } else { |
| 3400 | return res_method; |
| 3401 | } |
| 3402 | } |
| 3403 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3404 | const RegType& DexVerifier::GetMethodReturnType() { |
| 3405 | return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), |
| 3406 | MethodHelper(method_).GetReturnTypeDescriptor()); |
| 3407 | } |
| 3408 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3409 | void DexVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled, |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3410 | bool is_range) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3411 | const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3412 | if (res_type.IsUnknown()) { |
| 3413 | CHECK_NE(failure_, VERIFY_ERROR_NONE); |
| 3414 | } else { |
| 3415 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3416 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3417 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3418 | } else if (!is_filled) { |
| 3419 | /* make sure "size" register is valid type */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3420 | work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3421 | /* set register type to array class */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3422 | work_line_->SetRegisterType(dec_insn.vA, res_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3423 | } else { |
| 3424 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3425 | // the list and fail. It's legal, if silly, for arg_count to be zero. |
| 3426 | const RegType& expected_type = reg_types_.GetComponentType(res_type, |
| 3427 | method_->GetDeclaringClass()->GetClassLoader()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3428 | uint32_t arg_count = dec_insn.vA; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3429 | for (size_t ui = 0; ui < arg_count; ui++) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3430 | uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui]; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3431 | if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { |
| 3432 | work_line_->SetResultRegisterType(reg_types_.Unknown()); |
| 3433 | return; |
| 3434 | } |
| 3435 | } |
| 3436 | // filled-array result goes into "result" register |
| 3437 | work_line_->SetResultRegisterType(res_type); |
| 3438 | } |
| 3439 | } |
| 3440 | } |
| 3441 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3442 | void DexVerifier::VerifyAGet(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3443 | const RegType& insn_type, bool is_primitive) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3444 | const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3445 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3446 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3447 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3448 | const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3449 | if (array_type.IsZero()) { |
| 3450 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3451 | // instruction type. TODO: have a proper notion of bottom here. |
| 3452 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3453 | // Reference or category 1 |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3454 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3455 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3456 | // Category 2 |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3457 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3458 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3459 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3460 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget"; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3461 | } else { |
| 3462 | /* verify the class */ |
| 3463 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 3464 | method_->GetDeclaringClass()->GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3465 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3467 | << " source for aget-object"; |
| 3468 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3469 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3470 | << " source for category 1 aget"; |
| 3471 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3472 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
| 3473 | (insn_type.IsLong() && component_type.IsDouble()))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3474 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3475 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3476 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3477 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3478 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3479 | // doubles. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3480 | work_line_->SetRegisterType(dec_insn.vA, component_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3486 | void DexVerifier::VerifyAPut(const DecodedInstruction& dec_insn, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3487 | const RegType& insn_type, bool is_primitive) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3488 | const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3489 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3490 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3491 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3492 | const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3493 | if (array_type.IsZero()) { |
| 3494 | // Null array type; this code path will fail at runtime. Infer a merge-able type from the |
| 3495 | // instruction type. |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3496 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3497 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput"; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3498 | } else { |
| 3499 | /* verify the class */ |
| 3500 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 3501 | method_->GetDeclaringClass()->GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3502 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3503 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3504 | << " source for aput-object"; |
| 3505 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3506 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3507 | << " source for category 1 aput"; |
| 3508 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3509 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
| 3510 | (insn_type.IsLong() && component_type.IsDouble()))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3511 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3512 | << " incompatible with aput of type " << insn_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3513 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3514 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3515 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3516 | // incompatible classes will be caught at runtime as an array store exception |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3517 | work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | } |
| 3522 | |
| 3523 | Field* DexVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3524 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3525 | // Check access to class |
| 3526 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
| 3527 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3528 | fail_messages_ << " in attempt to access static field " << field_idx << " (" |
| 3529 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3530 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 3531 | return NULL; |
| 3532 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3533 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3534 | return NULL; // Can't resolve Class so no more to do here |
| 3535 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 3536 | Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3537 | if (field == NULL) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3538 | LOG(INFO) << "unable to resolve static field " << field_idx << " (" |
| 3539 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3540 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3541 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3542 | Thread::Current()->ClearException(); |
| 3543 | return NULL; |
| 3544 | } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(), |
| 3545 | field->GetAccessFlags())) { |
| 3546 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
| 3547 | << " from " << PrettyClass(method_->GetDeclaringClass()); |
| 3548 | return NULL; |
| 3549 | } else if (!field->IsStatic()) { |
| 3550 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
| 3551 | return NULL; |
| 3552 | } else { |
| 3553 | return field; |
| 3554 | } |
| 3555 | } |
| 3556 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3557 | Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3558 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3559 | // Check access to class |
| 3560 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
| 3561 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3562 | fail_messages_ << " in attempt to access instance field " << field_idx << " (" |
| 3563 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3564 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 3565 | return NULL; |
| 3566 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3567 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3568 | return NULL; // Can't resolve Class so no more to do here |
| 3569 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 3570 | Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3571 | if (field == NULL) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3572 | LOG(INFO) << "unable to resolve instance field " << field_idx << " (" |
| 3573 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3574 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3575 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3576 | Thread::Current()->ClearException(); |
| 3577 | return NULL; |
| 3578 | } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(), |
| 3579 | field->GetAccessFlags())) { |
| 3580 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3581 | << " from " << PrettyClass(method_->GetDeclaringClass()); |
| 3582 | return NULL; |
| 3583 | } else if (field->IsStatic()) { |
| 3584 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3585 | << " to not be static"; |
| 3586 | return NULL; |
| 3587 | } else if (obj_type.IsZero()) { |
| 3588 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3589 | return field; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3590 | } else if (obj_type.IsUninitializedTypes() && |
| 3591 | (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() || |
| 3592 | field->GetDeclaringClass() != method_->GetDeclaringClass())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3593 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3594 | // the field is declared in this class |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3595 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
| 3596 | << " of a not fully initialized object within the context of " |
| 3597 | << PrettyMethod(method_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3598 | return NULL; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3599 | } else if (!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3600 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3601 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3602 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3603 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3604 | << " from object of type " << PrettyClass(obj_type.GetClass()); |
| 3605 | return NULL; |
| 3606 | } else { |
| 3607 | return field; |
| 3608 | } |
| 3609 | } |
| 3610 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3611 | void DexVerifier::VerifyISGet(const DecodedInstruction& dec_insn, |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3612 | const RegType& insn_type, bool is_primitive, bool is_static) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3613 | uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3614 | Field* field; |
| 3615 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3616 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3617 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3618 | const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3619 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3620 | } |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3621 | if (failure_ != VERIFY_ERROR_NONE) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3622 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3623 | } else { |
| 3624 | const char* descriptor; |
| 3625 | const ClassLoader* loader; |
| 3626 | if (field != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3627 | descriptor = FieldHelper(field).GetTypeDescriptor(); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3628 | loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3629 | } else { |
| 3630 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3631 | descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
| 3632 | loader = method_->GetDeclaringClass()->GetClassLoader(); |
| 3633 | } |
| 3634 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3635 | if (is_primitive) { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3636 | if (field_type.Equals(insn_type) || |
| 3637 | (field_type.IsFloat() && insn_type.IsIntegralTypes()) || |
| 3638 | (field_type.IsDouble() && insn_type.IsLongTypes())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3639 | // expected that read is of the correct primitive type or that int reads are reading |
| 3640 | // floats or long reads are reading doubles |
| 3641 | } else { |
| 3642 | // This is a global failure rather than a class change failure as the instructions and |
| 3643 | // the descriptors for the type should have been consistent within the same file at |
| 3644 | // compile time |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3645 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3646 | << " to be of type '" << insn_type |
| 3647 | << "' but found type '" << field_type << "' in get"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3648 | return; |
| 3649 | } |
| 3650 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3651 | if (!insn_type.IsAssignableFrom(field_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3652 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3653 | << " to be compatible with type '" << insn_type |
| 3654 | << "' but found type '" << field_type |
| 3655 | << "' in get-object"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3656 | return; |
| 3657 | } |
| 3658 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3659 | work_line_->SetRegisterType(dec_insn.vA, field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3663 | void DexVerifier::VerifyISPut(const DecodedInstruction& dec_insn, |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3664 | const RegType& insn_type, bool is_primitive, bool is_static) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3665 | uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3666 | Field* field; |
| 3667 | if (is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3668 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3669 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3670 | const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3671 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3672 | } |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3673 | if (failure_ != VERIFY_ERROR_NONE) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3674 | work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3675 | } else { |
| 3676 | const char* descriptor; |
| 3677 | const ClassLoader* loader; |
| 3678 | if (field != NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3679 | descriptor = FieldHelper(field).GetTypeDescriptor(); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3680 | loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3681 | } else { |
| 3682 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3683 | descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
| 3684 | loader = method_->GetDeclaringClass()->GetClassLoader(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3685 | } |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3686 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor); |
| 3687 | if (field != NULL) { |
| 3688 | if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) { |
| 3689 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3690 | << " from other class " << PrettyClass(method_->GetDeclaringClass()); |
| 3691 | return; |
| 3692 | } |
| 3693 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3694 | if (is_primitive) { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3695 | // Primitive field assignability rules are weaker than regular assignability rules |
| 3696 | bool instruction_compatible; |
| 3697 | bool value_compatible; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3698 | const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA); |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3699 | if (field_type.IsIntegralTypes()) { |
| 3700 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 3701 | value_compatible = value_type.IsIntegralTypes(); |
| 3702 | } else if (field_type.IsFloat()) { |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3703 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3704 | value_compatible = value_type.IsFloatTypes(); |
| 3705 | } else if (field_type.IsLong()) { |
| 3706 | instruction_compatible = insn_type.IsLong(); |
| 3707 | value_compatible = value_type.IsLongTypes(); |
| 3708 | } else if (field_type.IsDouble()) { |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3709 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3710 | value_compatible = value_type.IsDoubleTypes(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3711 | } else { |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3712 | instruction_compatible = false; // reference field with primitive store |
| 3713 | value_compatible = false; // unused |
| 3714 | } |
| 3715 | if (!instruction_compatible) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3716 | // This is a global failure rather than a class change failure as the instructions and |
| 3717 | // the descriptors for the type should have been consistent within the same file at |
| 3718 | // compile time |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3719 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3720 | << " to be of type '" << insn_type |
| 3721 | << "' but found type '" << field_type |
| 3722 | << "' in put"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3723 | return; |
| 3724 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3725 | if (!value_compatible) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3726 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA |
| 3727 | << " of type " << value_type |
| 3728 | << " but expected " << field_type |
| 3729 | << " for store to " << PrettyField(field) << " in put"; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3730 | return; |
| 3731 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3732 | } else { |
Ian Rogers | b5e95b9 | 2011-10-25 23:28:55 -0700 | [diff] [blame] | 3733 | if (!insn_type.IsAssignableFrom(field_type)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3734 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3735 | << " to be compatible with type '" << insn_type |
| 3736 | << "' but found type '" << field_type |
| 3737 | << "' in put-object"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3738 | return; |
| 3739 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3740 | work_line_->VerifyRegisterType(dec_insn.vA, field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3741 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3745 | bool DexVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3746 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3747 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3748 | return false; |
| 3749 | } |
| 3750 | return true; |
| 3751 | } |
| 3752 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3753 | void DexVerifier::ReplaceFailingInstruction() { |
Ian Rogers | f1864ef | 2011-12-09 12:39:48 -0800 | [diff] [blame] | 3754 | if (Runtime::Current()->IsStarted()) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3755 | LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_) |
Ian Rogers | f1864ef | 2011-12-09 12:39:48 -0800 | [diff] [blame] | 3756 | << " " << fail_messages_.str(); |
| 3757 | return; |
| 3758 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3759 | const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_); |
| 3760 | DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name(); |
| 3761 | VerifyErrorRefType ref_type; |
| 3762 | switch (inst->Opcode()) { |
| 3763 | case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3764 | case Instruction::CHECK_CAST: |
| 3765 | case Instruction::INSTANCE_OF: |
| 3766 | case Instruction::NEW_INSTANCE: |
| 3767 | case Instruction::NEW_ARRAY: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3768 | case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3769 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 3770 | ref_type = VERIFY_ERROR_REF_CLASS; |
| 3771 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3772 | case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3773 | case Instruction::IGET_BOOLEAN: |
| 3774 | case Instruction::IGET_BYTE: |
| 3775 | case Instruction::IGET_CHAR: |
| 3776 | case Instruction::IGET_SHORT: |
| 3777 | case Instruction::IGET_WIDE: |
| 3778 | case Instruction::IGET_OBJECT: |
| 3779 | case Instruction::IPUT: |
| 3780 | case Instruction::IPUT_BOOLEAN: |
| 3781 | case Instruction::IPUT_BYTE: |
| 3782 | case Instruction::IPUT_CHAR: |
| 3783 | case Instruction::IPUT_SHORT: |
| 3784 | case Instruction::IPUT_WIDE: |
| 3785 | case Instruction::IPUT_OBJECT: |
| 3786 | case Instruction::SGET: |
| 3787 | case Instruction::SGET_BOOLEAN: |
| 3788 | case Instruction::SGET_BYTE: |
| 3789 | case Instruction::SGET_CHAR: |
| 3790 | case Instruction::SGET_SHORT: |
| 3791 | case Instruction::SGET_WIDE: |
| 3792 | case Instruction::SGET_OBJECT: |
| 3793 | case Instruction::SPUT: |
| 3794 | case Instruction::SPUT_BOOLEAN: |
| 3795 | case Instruction::SPUT_BYTE: |
| 3796 | case Instruction::SPUT_CHAR: |
| 3797 | case Instruction::SPUT_SHORT: |
| 3798 | case Instruction::SPUT_WIDE: |
| 3799 | case Instruction::SPUT_OBJECT: |
| 3800 | ref_type = VERIFY_ERROR_REF_FIELD; |
| 3801 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3802 | case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3803 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 3804 | case Instruction::INVOKE_SUPER: |
| 3805 | case Instruction::INVOKE_SUPER_RANGE: |
| 3806 | case Instruction::INVOKE_DIRECT: |
| 3807 | case Instruction::INVOKE_DIRECT_RANGE: |
| 3808 | case Instruction::INVOKE_STATIC: |
| 3809 | case Instruction::INVOKE_STATIC_RANGE: |
| 3810 | case Instruction::INVOKE_INTERFACE: |
| 3811 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 3812 | ref_type = VERIFY_ERROR_REF_METHOD; |
| 3813 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3814 | default: |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3815 | LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3816 | return; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 3817 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3818 | uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_); |
| 3819 | // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit |
| 3820 | // instruction, so assert it. |
| 3821 | size_t width = inst->SizeInCodeUnits(); |
| 3822 | CHECK_GT(width, 1u); |
Ian Rogers | f1864ef | 2011-12-09 12:39:48 -0800 | [diff] [blame] | 3823 | // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3824 | // NOPs |
| 3825 | for (size_t i = 2; i < width; i++) { |
| 3826 | insns[work_insn_idx_ + i] = Instruction::NOP; |
| 3827 | } |
| 3828 | // Encode the opcode, with the failure code in the high byte |
| 3829 | uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR | |
| 3830 | (failure_ << 8) | // AA - component |
| 3831 | (ref_type << (8 + kVerifyErrorRefTypeShift)); |
| 3832 | insns[work_insn_idx_] = new_instruction; |
| 3833 | // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we |
| 3834 | // rewrote, so nothing to do here. |
Ian Rogers | 9fdfc18 | 2011-10-26 23:12:52 -0700 | [diff] [blame] | 3835 | LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " " |
| 3836 | << fail_messages_.str(); |
| 3837 | if (gDebugVerify) { |
| 3838 | std::cout << std::endl << info_messages_.str(); |
| 3839 | Dump(std::cout); |
| 3840 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3841 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 3842 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3843 | bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3844 | bool changed = true; |
| 3845 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 3846 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3847 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3848 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 3849 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 3850 | * only way a register can transition out of "unknown", so this is not just an optimization.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3851 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3852 | target_line->CopyFromLine(merge_line); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3853 | } else { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3854 | UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL); |
| 3855 | if (gDebugVerify) { |
| 3856 | copy->CopyFromLine(target_line); |
| 3857 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3858 | changed = target_line->MergeRegisters(merge_line); |
| 3859 | if (failure_ != VERIFY_ERROR_NONE) { |
| 3860 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3861 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3862 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 3863 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
| 3864 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3865 | << *copy.get() << " MERGE" << std::endl |
| 3866 | << *merge_line << " ==" << std::endl |
| 3867 | << *target_line << std::endl; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3868 | } |
| 3869 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3870 | if (changed) { |
| 3871 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3872 | } |
| 3873 | return true; |
| 3874 | } |
| 3875 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3876 | void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, |
| 3877 | size_t* log2_max_gc_pc) { |
| 3878 | size_t local_gc_points = 0; |
| 3879 | size_t max_insn = 0; |
| 3880 | size_t max_ref_reg = -1; |
| 3881 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
| 3882 | if (insn_flags_[i].IsGcPoint()) { |
| 3883 | local_gc_points++; |
| 3884 | max_insn = i; |
| 3885 | RegisterLine* line = reg_table_.GetLine(i); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3886 | max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3887 | } |
| 3888 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3889 | *gc_points = local_gc_points; |
| 3890 | *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1) |
| 3891 | size_t i = 0; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3892 | while ((1U << i) <= max_insn) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3893 | i++; |
| 3894 | } |
| 3895 | *log2_max_gc_pc = i; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3896 | } |
| 3897 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3898 | const std::vector<uint8_t>* DexVerifier::GenerateGcMap() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3899 | size_t num_entries, ref_bitmap_bits, pc_bits; |
| 3900 | ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits); |
| 3901 | // There's a single byte to encode the size of each bitmap |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 3902 | if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3903 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3904 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3905 | << ref_bitmap_bits << " registers"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3906 | return NULL; |
| 3907 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3908 | size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8; |
| 3909 | // There are 2 bytes to encode the number of entries |
| 3910 | if (num_entries >= 65536) { |
| 3911 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3912 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3913 | << num_entries << " entries"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3914 | return NULL; |
| 3915 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3916 | size_t pc_bytes; |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3917 | RegisterMapFormat format; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3918 | if (pc_bits <= 8) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3919 | format = kRegMapFormatCompact8; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3920 | pc_bytes = 1; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3921 | } else if (pc_bits <= 16) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3922 | format = kRegMapFormatCompact16; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3923 | pc_bytes = 2; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3924 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3925 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3926 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3927 | << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)"; |
| 3928 | return NULL; |
| 3929 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3930 | size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3931 | std::vector<uint8_t>* table = new std::vector<uint8_t>; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3932 | if (table == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3933 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3934 | return NULL; |
| 3935 | } |
| 3936 | // Write table header |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 3937 | table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask)); |
| 3938 | table->push_back(ref_bitmap_bytes & 0xFF); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3939 | table->push_back(num_entries & 0xFF); |
| 3940 | table->push_back((num_entries >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3941 | // Write table data |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3942 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
| 3943 | if (insn_flags_[i].IsGcPoint()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3944 | table->push_back(i & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3945 | if (pc_bytes == 2) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3946 | table->push_back((i >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3947 | } |
| 3948 | RegisterLine* line = reg_table_.GetLine(i); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3949 | line->WriteReferenceBitMap(*table, ref_bitmap_bytes); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3950 | } |
| 3951 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3952 | DCHECK_EQ(table->size(), table_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3953 | return table; |
| 3954 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3955 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3956 | void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3957 | // Check that for every GC point there is a map entry, there aren't entries for non-GC points, |
| 3958 | // that the table data is well formed and all references are marked (or not) in the bitmap |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3959 | PcToReferenceMap map(&data[0], data.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3960 | size_t map_index = 0; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3961 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3962 | const uint8_t* reg_bitmap = map.FindBitMap(i, false); |
| 3963 | if (insn_flags_[i].IsGcPoint()) { |
| 3964 | CHECK_LT(map_index, map.NumEntries()); |
| 3965 | CHECK_EQ(map.GetPC(map_index), i); |
| 3966 | CHECK_EQ(map.GetBitMap(map_index), reg_bitmap); |
| 3967 | map_index++; |
| 3968 | RegisterLine* line = reg_table_.GetLine(i); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3969 | for (size_t j = 0; j < code_item_->registers_size_; j++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3970 | if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3971 | CHECK_LT(j / 8, map.RegWidth()); |
| 3972 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1); |
| 3973 | } else if ((j / 8) < map.RegWidth()) { |
| 3974 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0); |
| 3975 | } else { |
| 3976 | // If a register doesn't contain a reference then the bitmap may be shorter than the line |
| 3977 | } |
| 3978 | } |
| 3979 | } else { |
| 3980 | CHECK(reg_bitmap == NULL); |
| 3981 | } |
| 3982 | } |
| 3983 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3984 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3985 | const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const { |
| 3986 | size_t num_entries = NumEntries(); |
| 3987 | // Do linear or binary search? |
| 3988 | static const size_t kSearchThreshold = 8; |
| 3989 | if (num_entries < kSearchThreshold) { |
| 3990 | for (size_t i = 0; i < num_entries; i++) { |
| 3991 | if (GetPC(i) == dex_pc) { |
| 3992 | return GetBitMap(i); |
| 3993 | } |
| 3994 | } |
| 3995 | } else { |
| 3996 | int lo = 0; |
| 3997 | int hi = num_entries -1; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 3998 | while (hi >= lo) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3999 | int mid = (hi + lo) / 2; |
| 4000 | int mid_pc = GetPC(mid); |
| 4001 | if (dex_pc > mid_pc) { |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4002 | lo = mid + 1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4003 | } else if (dex_pc < mid_pc) { |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4004 | hi = mid - 1; |
| 4005 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4006 | return GetBitMap(mid); |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4010 | if (error_if_not_present) { |
| 4011 | LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc; |
| 4012 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4013 | return NULL; |
| 4014 | } |
| 4015 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4016 | Mutex* DexVerifier::gc_maps_lock_ = NULL; |
| 4017 | DexVerifier::GcMapTable* DexVerifier::gc_maps_ = NULL; |
| 4018 | |
| 4019 | void DexVerifier::InitGcMaps() { |
| 4020 | gc_maps_lock_ = new Mutex("verifier GC maps lock"); |
| 4021 | MutexLock mu(*gc_maps_lock_); |
| 4022 | gc_maps_ = new DexVerifier::GcMapTable; |
| 4023 | } |
| 4024 | |
| 4025 | void DexVerifier::DeleteGcMaps() { |
Elliott Hughes | f34f174 | 2012-03-16 18:56:00 -0700 | [diff] [blame] | 4026 | { |
| 4027 | MutexLock mu(*gc_maps_lock_); |
| 4028 | STLDeleteValues(gc_maps_); |
| 4029 | delete gc_maps_; |
| 4030 | gc_maps_ = NULL; |
| 4031 | } |
| 4032 | delete gc_maps_lock_; |
| 4033 | gc_maps_lock_ = NULL; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4034 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4035 | |
| 4036 | void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4037 | MutexLock mu(*gc_maps_lock_); |
Brian Carlstrom | 73a15f4 | 2012-01-17 18:14:39 -0800 | [diff] [blame] | 4038 | const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref); |
| 4039 | if (existing_gc_map != NULL) { |
| 4040 | CHECK(*existing_gc_map == gc_map); |
| 4041 | delete existing_gc_map; |
| 4042 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4043 | (*gc_maps_)[ref] = &gc_map; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4044 | CHECK(GetGcMap(ref) != NULL); |
| 4045 | } |
| 4046 | |
| 4047 | const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4048 | MutexLock mu(*gc_maps_lock_); |
| 4049 | GcMapTable::const_iterator it = gc_maps_->find(ref); |
| 4050 | if (it == gc_maps_->end()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4051 | return NULL; |
| 4052 | } |
| 4053 | CHECK(it->second != NULL); |
| 4054 | return it->second; |
| 4055 | } |
| 4056 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4057 | static Mutex& GetRejectedClassesLock() { |
| 4058 | static Mutex rejected_classes_lock("verifier rejected classes lock"); |
| 4059 | return rejected_classes_lock; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4060 | } |
| 4061 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4062 | static std::set<Compiler::ClassReference>& GetRejectedClasses() { |
| 4063 | static std::set<Compiler::ClassReference> rejected_classes; |
| 4064 | return rejected_classes; |
| 4065 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4066 | |
| 4067 | void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4068 | MutexLock mu(GetRejectedClassesLock()); |
| 4069 | GetRejectedClasses().insert(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4070 | CHECK(IsClassRejected(ref)); |
| 4071 | } |
| 4072 | |
| 4073 | bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4074 | MutexLock mu(GetRejectedClassesLock()); |
| 4075 | std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses()); |
| 4076 | return (rejected_classes.find(ref) != rejected_classes.end()); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4077 | } |
| 4078 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 4079 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame^] | 4080 | const InferredRegCategoryMap* DexVerifier::GenerateInferredRegCategoryMap() { |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 4081 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
| 4082 | uint16_t regs_size = code_item_->registers_size_; |
| 4083 | |
| 4084 | UniquePtr<InferredRegCategoryMap> table( |
| 4085 | new InferredRegCategoryMap(insns_size, regs_size)); |
| 4086 | |
| 4087 | for (size_t i = 0; i < insns_size; ++i) { |
| 4088 | if (RegisterLine* line = reg_table_.GetLine(i)) { |
| 4089 | for (size_t r = 0; r < regs_size; ++r) { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame^] | 4090 | const RegType &rt = line->GetRegisterType(r); |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 4091 | |
| 4092 | if (rt.IsZero()) { |
| 4093 | table->SetRegCategory(i, r, kRegZero); |
| 4094 | } else if (rt.IsCategory1Types()) { |
| 4095 | table->SetRegCategory(i, r, kRegCat1nr); |
| 4096 | } else if (rt.IsCategory2Types()) { |
| 4097 | table->SetRegCategory(i, r, kRegCat2); |
| 4098 | } else if (rt.IsReferenceTypes()) { |
| 4099 | table->SetRegCategory(i, r, kRegObject); |
| 4100 | } else { |
| 4101 | table->SetRegCategory(i, r, kRegUnknown); |
| 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | return table.release(); |
| 4108 | } |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame^] | 4109 | |
| 4110 | Mutex* DexVerifier::inferred_reg_category_maps_lock_ = NULL; |
| 4111 | DexVerifier::InferredRegCategoryMapTable* DexVerifier::inferred_reg_category_maps_ = NULL; |
| 4112 | |
| 4113 | void DexVerifier::InitInferredRegCategoryMaps() { |
| 4114 | inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock"); |
| 4115 | MutexLock mu(*inferred_reg_category_maps_lock_); |
| 4116 | inferred_reg_category_maps_ = new DexVerifier::InferredRegCategoryMapTable; |
| 4117 | } |
| 4118 | |
| 4119 | void DexVerifier::DeleteInferredRegCategoryMaps() { |
| 4120 | { |
| 4121 | MutexLock mu(*inferred_reg_category_maps_lock_); |
| 4122 | STLDeleteValues(inferred_reg_category_maps_); |
| 4123 | delete inferred_reg_category_maps_; |
| 4124 | inferred_reg_category_maps_ = NULL; |
| 4125 | } |
| 4126 | delete inferred_reg_category_maps_lock_; |
| 4127 | inferred_reg_category_maps_lock_ = NULL; |
| 4128 | } |
| 4129 | |
| 4130 | |
| 4131 | void DexVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref, |
| 4132 | const InferredRegCategoryMap& inferred_reg_category_map) { |
| 4133 | MutexLock mu(*inferred_reg_category_maps_lock_); |
| 4134 | const InferredRegCategoryMap* existing_inferred_reg_category_map = |
| 4135 | GetInferredRegCategoryMap(ref); |
| 4136 | |
| 4137 | if (existing_inferred_reg_category_map != NULL) { |
| 4138 | CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map); |
| 4139 | delete existing_inferred_reg_category_map; |
| 4140 | } |
| 4141 | |
| 4142 | (*inferred_reg_category_maps_)[ref] = &inferred_reg_category_map; |
| 4143 | CHECK(GetInferredRegCategoryMap(ref) != NULL); |
| 4144 | } |
| 4145 | |
| 4146 | const InferredRegCategoryMap* |
| 4147 | DexVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) { |
| 4148 | MutexLock mu(*inferred_reg_category_maps_lock_); |
| 4149 | |
| 4150 | InferredRegCategoryMapTable::const_iterator it = |
| 4151 | inferred_reg_category_maps_->find(ref); |
| 4152 | |
| 4153 | if (it == inferred_reg_category_maps_->end()) { |
| 4154 | return NULL; |
| 4155 | } |
| 4156 | CHECK(it->second != NULL); |
| 4157 | return it->second; |
| 4158 | } |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 4159 | #endif |
| 4160 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4161 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4162 | } // namespace art |