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