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