blob: 117218657b249191743a2c19479679bde1a5c279 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
Elliott Hughes1f359b02011-07-17 14:27:17 -07005#include <iostream>
6
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_linker.h"
jeffhaob4df5142011-09-19 20:25:32 -07008#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
10#include "dex_instruction.h"
11#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070012#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070013#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070014#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070015#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080016#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070018#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
20namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070021namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022
Ian Rogers2c8a8572011-10-24 17:11:36 -070023static const bool gDebugVerify = false;
24
Ian Rogersd81871c2011-10-03 13:57:23 -070025std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
26 return os << (int)rhs;
27}
jeffhaobdb76512011-09-07 11:43:16 -070028
Ian Rogers84fa0742011-10-25 18:13:30 -070029static const char* type_strings[] = {
30 "Unknown",
31 "Conflict",
32 "Boolean",
33 "Byte",
34 "Short",
35 "Char",
36 "Integer",
37 "Float",
38 "Long (Low Half)",
39 "Long (High Half)",
40 "Double (Low Half)",
41 "Double (High Half)",
42 "64-bit Constant (Low Half)",
43 "64-bit Constant (High Half)",
44 "32-bit Constant",
45 "Unresolved Reference",
46 "Uninitialized Reference",
47 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070048 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070049 "Reference",
50};
Ian Rogersd81871c2011-10-03 13:57:23 -070051
Ian Rogers2c8a8572011-10-24 17:11:36 -070052std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070053 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
54 std::string result;
55 if (IsConstant()) {
56 uint32_t val = ConstantValue();
57 if (val == 0) {
58 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070059 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070060 if(IsConstantShort()) {
61 result = StringPrintf("32-bit Constant: %d", val);
62 } else {
63 result = StringPrintf("32-bit Constant: 0x%x", val);
64 }
65 }
66 } else {
67 result = type_strings[type_];
68 if (IsReferenceTypes()) {
69 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -070070 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070071 result += PrettyDescriptor(GetDescriptor());
72 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -070074 }
Ian Rogersd81871c2011-10-03 13:57:23 -070075 }
76 }
Ian Rogers84fa0742011-10-25 18:13:30 -070077 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070078}
79
80const RegType& RegType::HighHalf(RegTypeCache* cache) const {
81 CHECK(IsLowHalf());
82 if (type_ == kRegTypeLongLo) {
83 return cache->FromType(kRegTypeLongHi);
84 } else if (type_ == kRegTypeDoubleLo) {
85 return cache->FromType(kRegTypeDoubleHi);
86 } else {
87 return cache->FromType(kRegTypeConstHi);
88 }
89}
90
91/*
92 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
93 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
94 * 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
95 * is the deepest (lowest upper bound) parent of S and T).
96 *
97 * This operation applies for regular classes and arrays, however, for interface types there needn't
98 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
99 * introducing sets of types, however, the only operation permissible on an interface is
100 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
101 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700102 * the perversion of any Object being assignable to an interface type (note, however, that we don't
103 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
104 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700105 */
106Class* RegType::ClassJoin(Class* s, Class* t) {
107 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
108 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
109 if (s == t) {
110 return s;
111 } else if (s->IsAssignableFrom(t)) {
112 return s;
113 } else if (t->IsAssignableFrom(s)) {
114 return t;
115 } else if (s->IsArrayClass() && t->IsArrayClass()) {
116 Class* s_ct = s->GetComponentType();
117 Class* t_ct = t->GetComponentType();
118 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
119 // Given the types aren't the same, if either array is of primitive types then the only
120 // common parent is java.lang.Object
121 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
122 DCHECK(result->IsObjectClass());
123 return result;
124 }
125 Class* common_elem = ClassJoin(s_ct, t_ct);
126 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
127 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800128 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700130 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
131 DCHECK(array_class != NULL);
132 return array_class;
133 } else {
134 size_t s_depth = s->Depth();
135 size_t t_depth = t->Depth();
136 // Get s and t to the same depth in the hierarchy
137 if (s_depth > t_depth) {
138 while (s_depth > t_depth) {
139 s = s->GetSuperClass();
140 s_depth--;
141 }
142 } else {
143 while (t_depth > s_depth) {
144 t = t->GetSuperClass();
145 t_depth--;
146 }
147 }
148 // Go up the hierarchy until we get to the common parent
149 while (s != t) {
150 s = s->GetSuperClass();
151 t = t->GetSuperClass();
152 }
153 return s;
154 }
155}
156
Ian Rogersb5e95b92011-10-25 23:28:55 -0700157bool RegType::IsAssignableFrom(const RegType& src) const {
158 if (Equals(src)) {
159 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700160 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700161 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700162 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
163 case RegType::kRegTypeByte: return src.IsByteTypes();
164 case RegType::kRegTypeShort: return src.IsShortTypes();
165 case RegType::kRegTypeChar: return src.IsCharTypes();
166 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
167 case RegType::kRegTypeFloat: return src.IsFloatTypes();
168 case RegType::kRegTypeLongLo: return src.IsLongTypes();
169 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700170 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700171 if (!IsReferenceTypes()) {
172 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700173 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700174 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700175 return true; // all reference types can be assigned null
176 } else if (!src.IsReferenceTypes()) {
177 return false; // expect src to be a reference type
178 } else if (IsJavaLangObject()) {
179 return true; // all reference types can be assigned to Object
180 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700181 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700182 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700183 GetClass()->IsAssignableFrom(src.GetClass())) {
184 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700185 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700187 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700188 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700189 }
190 }
191}
192
Ian Rogers84fa0742011-10-25 18:13:30 -0700193static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
194 return a.IsConstant() ? b : a;
195}
jeffhaobdb76512011-09-07 11:43:16 -0700196
Ian Rogersd81871c2011-10-03 13:57:23 -0700197const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
198 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700199 if (IsUnknown() && incoming_type.IsUnknown()) {
200 return *this; // Unknown MERGE Unknown => Unknown
201 } else if (IsConflict()) {
202 return *this; // Conflict MERGE * => Conflict
203 } else if (incoming_type.IsConflict()) {
204 return incoming_type; // * MERGE Conflict => Conflict
205 } else if (IsUnknown() || incoming_type.IsUnknown()) {
206 return reg_types->Conflict(); // Unknown MERGE * => Conflict
207 } else if(IsConstant() && incoming_type.IsConstant()) {
208 int32_t val1 = ConstantValue();
209 int32_t val2 = incoming_type.ConstantValue();
210 if (val1 >= 0 && val2 >= 0) {
211 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
212 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700213 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700214 } else {
215 return incoming_type;
216 }
217 } else if (val1 < 0 && val2 < 0) {
218 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
219 if (val1 <= val2) {
220 return *this;
221 } else {
222 return incoming_type;
223 }
224 } else {
225 // Values are +ve and -ve, choose smallest signed type in which they both fit
226 if (IsConstantByte()) {
227 if (incoming_type.IsConstantByte()) {
228 return reg_types->ByteConstant();
229 } else if (incoming_type.IsConstantShort()) {
230 return reg_types->ShortConstant();
231 } else {
232 return reg_types->IntConstant();
233 }
234 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700235 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700236 return reg_types->ShortConstant();
237 } else {
238 return reg_types->IntConstant();
239 }
240 } else {
241 return reg_types->IntConstant();
242 }
243 }
244 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
245 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
246 return reg_types->Boolean(); // boolean MERGE boolean => boolean
247 }
248 if (IsByteTypes() && incoming_type.IsByteTypes()) {
249 return reg_types->Byte(); // byte MERGE byte => byte
250 }
251 if (IsShortTypes() && incoming_type.IsShortTypes()) {
252 return reg_types->Short(); // short MERGE short => short
253 }
254 if (IsCharTypes() && incoming_type.IsCharTypes()) {
255 return reg_types->Char(); // char MERGE char => char
256 }
257 return reg_types->Integer(); // int MERGE * => int
258 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
259 (IsLongTypes() && incoming_type.IsLongTypes()) ||
260 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
261 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
262 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
263 // check constant case was handled prior to entry
264 DCHECK(!IsConstant() || !incoming_type.IsConstant());
265 // float/long/double MERGE float/long/double_constant => float/long/double
266 return SelectNonConstant(*this, incoming_type);
267 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700268 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700269 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700270 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
271 return reg_types->JavaLangObject(); // Object MERGE ref => Object
272 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
273 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
274 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
275 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700276 return reg_types->Conflict();
277 } else { // Two reference types, compute Join
278 Class* c1 = GetClass();
279 Class* c2 = incoming_type.GetClass();
280 DCHECK(c1 != NULL && !c1->IsPrimitive());
281 DCHECK(c2 != NULL && !c2->IsPrimitive());
282 Class* join_class = ClassJoin(c1, c2);
283 if (c1 == join_class) {
284 return *this;
285 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 return incoming_type;
287 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700288 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700289 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700290 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700291 } else {
292 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700293 }
294}
295
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700296static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700297 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700298 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
299 case Primitive::kPrimByte: return RegType::kRegTypeByte;
300 case Primitive::kPrimShort: return RegType::kRegTypeShort;
301 case Primitive::kPrimChar: return RegType::kRegTypeChar;
302 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
303 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
304 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
305 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
306 case Primitive::kPrimVoid:
307 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700308 }
309}
310
311static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
312 if (descriptor.length() == 1) {
313 switch (descriptor[0]) {
314 case 'Z': return RegType::kRegTypeBoolean;
315 case 'B': return RegType::kRegTypeByte;
316 case 'S': return RegType::kRegTypeShort;
317 case 'C': return RegType::kRegTypeChar;
318 case 'I': return RegType::kRegTypeInteger;
319 case 'J': return RegType::kRegTypeLongLo;
320 case 'F': return RegType::kRegTypeFloat;
321 case 'D': return RegType::kRegTypeDoubleLo;
322 case 'V':
323 default: return RegType::kRegTypeUnknown;
324 }
325 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
326 return RegType::kRegTypeReference;
327 } else {
328 return RegType::kRegTypeUnknown;
329 }
330}
331
332std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700333 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700334 return os;
335}
336
337const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800338 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700339 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
340}
341
342const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800343 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700344 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 // entries should be sized greater than primitive types
346 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
347 RegType* entry = entries_[type];
348 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700349 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800350 if (strlen(descriptor) != 0) {
351 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700353 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700354 entries_[type] = entry;
355 }
356 return *entry;
357 } else {
358 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800359 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700360 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700361 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700362 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 if (cur_entry->IsReference()) {
364 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800365 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 return *cur_entry;
367 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700368 } else if (cur_entry->IsUnresolvedReference() &&
369 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 return *cur_entry;
371 }
372 }
Ian Rogers672297c2012-01-10 14:50:55 -0800373 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700374 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700375 // Able to resolve so create resolved register type
376 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700377 entries_.push_back(entry);
378 return *entry;
379 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700380 // TODO: we assume unresolved, but we may be able to do better by validating whether the
381 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700382 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700383 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700384 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800385 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700386 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800387 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700388 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
389 entries_.size());
390 entries_.push_back(entry);
391 return *entry;
392 } else {
393 // The descriptor is broken return the unknown type as there's nothing sensible that
394 // could be done at runtime
395 return Unknown();
396 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700397 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700398 }
399}
400
401const RegType& RegTypeCache::FromClass(Class* klass) {
402 if (klass->IsPrimitive()) {
403 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
404 // entries should be sized greater than primitive types
405 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
406 RegType* entry = entries_[type];
407 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700408 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 entries_[type] = entry;
410 }
411 return *entry;
412 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700413 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700414 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700415 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700416 return *cur_entry;
417 }
418 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700419 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700420 entries_.push_back(entry);
421 return *entry;
422 }
423}
424
Ian Rogers28ad40d2011-10-27 15:19:26 -0700425const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
426 RegType* entry;
427 if (type.IsUnresolvedTypes()) {
428 String* descriptor = type.GetDescriptor();
429 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
430 RegType* cur_entry = entries_[i];
431 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
432 cur_entry->GetAllocationPc() == allocation_pc &&
433 cur_entry->GetDescriptor() == descriptor) {
434 return *cur_entry;
435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700437 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
438 descriptor, allocation_pc, entries_.size());
439 } else {
440 Class* klass = type.GetClass();
441 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
442 RegType* cur_entry = entries_[i];
443 if (cur_entry->IsUninitializedReference() &&
444 cur_entry->GetAllocationPc() == allocation_pc &&
445 cur_entry->GetClass() == klass) {
446 return *cur_entry;
447 }
448 }
449 entry = new RegType(RegType::kRegTypeUninitializedReference,
450 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700452 entries_.push_back(entry);
453 return *entry;
454}
455
456const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
457 RegType* entry;
458 if (uninit_type.IsUnresolvedTypes()) {
459 String* descriptor = uninit_type.GetDescriptor();
460 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
461 RegType* cur_entry = entries_[i];
462 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
463 return *cur_entry;
464 }
465 }
466 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
467 } else {
468 Class* klass = uninit_type.GetClass();
469 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
470 RegType* cur_entry = entries_[i];
471 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
472 return *cur_entry;
473 }
474 }
475 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
476 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 entries_.push_back(entry);
478 return *entry;
479}
480
481const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700482 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 RegType* cur_entry = entries_[i];
484 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
485 return *cur_entry;
486 }
487 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700488 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 entries_.size());
490 entries_.push_back(entry);
491 return *entry;
492}
493
494const RegType& RegTypeCache::FromType(RegType::Type type) {
495 CHECK(type < RegType::kRegTypeReference);
496 switch (type) {
497 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
498 case RegType::kRegTypeByte: return From(type, NULL, "B");
499 case RegType::kRegTypeShort: return From(type, NULL, "S");
500 case RegType::kRegTypeChar: return From(type, NULL, "C");
501 case RegType::kRegTypeInteger: return From(type, NULL, "I");
502 case RegType::kRegTypeFloat: return From(type, NULL, "F");
503 case RegType::kRegTypeLongLo:
504 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
505 case RegType::kRegTypeDoubleLo:
506 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
507 default: return From(type, NULL, "");
508 }
509}
510
511const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700512 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
513 RegType* cur_entry = entries_[i];
514 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
515 return *cur_entry;
516 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700517 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700518 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
519 entries_.push_back(entry);
520 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700521}
522
Ian Rogers28ad40d2011-10-27 15:19:26 -0700523const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
524 CHECK(array.IsArrayClass());
525 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800526 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
527 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800528 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700529 } else {
530 return FromClass(array.GetClass()->GetComponentType());
531 }
532}
533
534
Ian Rogersd81871c2011-10-03 13:57:23 -0700535bool RegisterLine::CheckConstructorReturn() const {
536 for (size_t i = 0; i < num_regs_; i++) {
537 if (GetRegisterType(i).IsUninitializedThisReference()) {
538 verifier_->Fail(VERIFY_ERROR_GENERIC)
539 << "Constructor returning without calling superclass constructor";
540 return false;
541 }
542 }
543 return true;
544}
545
546void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
547 DCHECK(vdst < num_regs_);
548 if (new_type.IsLowHalf()) {
549 line_[vdst] = new_type.GetId();
550 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
551 } else if (new_type.IsHighHalf()) {
552 /* should never set these explicitly */
553 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
554 } else if (new_type.IsConflict()) { // should only be set during a merge
555 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
556 } else {
557 line_[vdst] = new_type.GetId();
558 }
559 // Clear the monitor entry bits for this register.
560 ClearAllRegToLockDepths(vdst);
561}
562
563void RegisterLine::SetResultTypeToUnknown() {
564 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
565 result_[0] = unknown_id;
566 result_[1] = unknown_id;
567}
568
569void RegisterLine::SetResultRegisterType(const RegType& new_type) {
570 result_[0] = new_type.GetId();
571 if(new_type.IsLowHalf()) {
572 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
573 result_[1] = new_type.GetId() + 1;
574 } else {
575 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
576 }
577}
578
579const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
580 // The register index was validated during the static pass, so we don't need to check it here.
581 DCHECK_LT(vsrc, num_regs_);
582 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
583}
584
585const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
586 if (dec_insn.vA_ < 1) {
587 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
588 return verifier_->GetRegTypeCache()->Unknown();
589 }
590 /* get the element type of the array held in vsrc */
591 const RegType& this_type = GetRegisterType(dec_insn.vC_);
592 if (!this_type.IsReferenceTypes()) {
593 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
594 << dec_insn.vC_ << " (type=" << this_type << ")";
595 return verifier_->GetRegTypeCache()->Unknown();
596 }
597 return this_type;
598}
599
600Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
601 /* get the element type of the array held in vsrc */
602 const RegType& type = GetRegisterType(vsrc);
603 /* if "always zero", we allow it to fail at runtime */
604 if (type.IsZero()) {
605 return NULL;
606 } else if (!type.IsReferenceTypes()) {
607 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
608 << " (type=" << type << ")";
609 return NULL;
610 } else if (type.IsUninitializedReference()) {
611 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
612 return NULL;
613 } else {
614 return type.GetClass();
615 }
616}
617
618bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
619 // Verify the src register type against the check type refining the type of the register
620 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700621 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
623 << " but expected " << check_type;
624 return false;
625 }
626 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
627 // precise than the subtype in vsrc so leave it for reference types. For primitive types
628 // if they are a defined type then they are as precise as we can get, however, for constant
629 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
630 return true;
631}
632
633void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700634 DCHECK(uninit_type.IsUninitializedTypes());
635 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
636 size_t changed = 0;
637 for (size_t i = 0; i < num_regs_; i++) {
638 if (GetRegisterType(i).Equals(uninit_type)) {
639 line_[i] = init_type.GetId();
640 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700643 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644}
645
646void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
647 for (size_t i = 0; i < num_regs_; i++) {
648 if (GetRegisterType(i).Equals(uninit_type)) {
649 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
650 ClearAllRegToLockDepths(i);
651 }
652 }
653}
654
655void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
656 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
657 const RegType& type = GetRegisterType(vsrc);
658 SetRegisterType(vdst, type);
659 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
660 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
661 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
662 << " cat=" << static_cast<int>(cat);
663 } else if (cat == kTypeCategoryRef) {
664 CopyRegToLockDepth(vdst, vsrc);
665 }
666}
667
668void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
669 const RegType& type_l = GetRegisterType(vsrc);
670 const RegType& type_h = GetRegisterType(vsrc + 1);
671
672 if (!type_l.CheckWidePair(type_h)) {
673 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
674 << " type=" << type_l << "/" << type_h;
675 } else {
676 SetRegisterType(vdst, type_l); // implicitly sets the second half
677 }
678}
679
680void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
681 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
682 if ((!is_reference && !type.IsCategory1Types()) ||
683 (is_reference && !type.IsReferenceTypes())) {
684 verifier_->Fail(VERIFY_ERROR_GENERIC)
685 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
686 } else {
687 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
688 SetRegisterType(vdst, type);
689 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
690 }
691}
692
693/*
694 * Implement "move-result-wide". Copy the category-2 value from the result
695 * register to another register, and reset the result register.
696 */
697void RegisterLine::CopyResultRegister2(uint32_t vdst) {
698 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
699 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
700 if (!type_l.IsCategory2Types()) {
701 verifier_->Fail(VERIFY_ERROR_GENERIC)
702 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
703 } else {
704 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
705 SetRegisterType(vdst, type_l); // also sets the high
706 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
707 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
708 }
709}
710
711void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
712 const RegType& dst_type, const RegType& src_type) {
713 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
714 SetRegisterType(dec_insn.vA_, dst_type);
715 }
716}
717
718void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
719 const RegType& dst_type,
720 const RegType& src_type1, const RegType& src_type2,
721 bool check_boolean_op) {
722 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
723 VerifyRegisterType(dec_insn.vC_, src_type2)) {
724 if (check_boolean_op) {
725 DCHECK(dst_type.IsInteger());
726 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
727 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
728 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
729 return;
730 }
731 }
732 SetRegisterType(dec_insn.vA_, dst_type);
733 }
734}
735
736void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
737 const RegType& dst_type, const RegType& src_type1,
738 const RegType& src_type2, bool check_boolean_op) {
739 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
740 VerifyRegisterType(dec_insn.vB_, src_type2)) {
741 if (check_boolean_op) {
742 DCHECK(dst_type.IsInteger());
743 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
744 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
745 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
746 return;
747 }
748 }
749 SetRegisterType(dec_insn.vA_, dst_type);
750 }
751}
752
753void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
754 const RegType& dst_type, const RegType& src_type,
755 bool check_boolean_op) {
756 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
757 if (check_boolean_op) {
758 DCHECK(dst_type.IsInteger());
759 /* check vB with the call, then check the constant manually */
760 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
761 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
762 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
763 return;
764 }
765 }
766 SetRegisterType(dec_insn.vA_, dst_type);
767 }
768}
769
770void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
771 const RegType& reg_type = GetRegisterType(reg_idx);
772 if (!reg_type.IsReferenceTypes()) {
773 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800774 } else if (monitors_.size() >= 32) {
775 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 } else {
777 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700778 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 }
780}
781
782void RegisterLine::PopMonitor(uint32_t reg_idx) {
783 const RegType& reg_type = GetRegisterType(reg_idx);
784 if (!reg_type.IsReferenceTypes()) {
785 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
786 } else if (monitors_.empty()) {
787 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
788 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700789 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
791 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
792 // format "036" the constant collector may create unlocks on the same object but referenced
793 // via different registers.
794 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
795 : verifier_->LogVerifyInfo())
796 << "monitor-exit not unlocking the top of the monitor stack";
797 } else {
798 // Record the register was unlocked
799 ClearRegToLockDepth(reg_idx, monitors_.size());
800 }
801 }
802}
803
804bool RegisterLine::VerifyMonitorStackEmpty() {
805 if (MonitorStackDepth() != 0) {
806 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
807 return false;
808 } else {
809 return true;
810 }
811}
812
813bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
814 bool changed = false;
815 for (size_t idx = 0; idx < num_regs_; idx++) {
816 if (line_[idx] != incoming_line->line_[idx]) {
817 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
818 const RegType& cur_type = GetRegisterType(idx);
819 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
820 changed = changed || !cur_type.Equals(new_type);
821 line_[idx] = new_type.GetId();
822 }
823 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700824 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
826 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
827 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
828 for (uint32_t idx = 0; idx < num_regs_; idx++) {
829 size_t depths = reg_to_lock_depths_.count(idx);
830 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
831 if (depths != incoming_depths) {
832 if (depths == 0 || incoming_depths == 0) {
833 reg_to_lock_depths_.erase(idx);
834 } else {
835 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
836 << ": " << depths << " != " << incoming_depths;
837 break;
838 }
839 }
840 }
841 }
842 return changed;
843}
844
845void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
846 for (size_t i = 0; i < num_regs_; i += 8) {
847 uint8_t val = 0;
848 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
849 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700850 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 val |= 1 << j;
852 }
853 }
854 if (val != 0) {
855 DCHECK_LT(i / 8, max_bytes);
856 data[i / 8] = val;
857 }
858 }
859}
860
861std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700862 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 return os;
864}
865
866
867void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
868 uint32_t insns_size, uint16_t registers_size,
869 DexVerifier* verifier) {
870 DCHECK_GT(insns_size, 0U);
871
872 for (uint32_t i = 0; i < insns_size; i++) {
873 bool interesting = false;
874 switch (mode) {
875 case kTrackRegsAll:
876 interesting = flags[i].IsOpcode();
877 break;
878 case kTrackRegsGcPoints:
879 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
880 break;
881 case kTrackRegsBranches:
882 interesting = flags[i].IsBranchTarget();
883 break;
884 default:
885 break;
886 }
887 if (interesting) {
888 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
889 }
890 }
891}
892
893bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700894 if (klass->IsVerified()) {
895 return true;
896 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800898 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
900 return false;
901 }
902 if (super != NULL) {
Ian Rogers672f5202012-01-12 18:06:40 -0800903 // Acquire lock to prevent races on verifying the super class
904 ObjectLock lock(super);
905
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (!super->IsVerified() && !super->IsErroneous()) {
907 Runtime::Current()->GetClassLinker()->VerifyClass(super);
908 }
909 if (!super->IsVerified()) {
910 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
911 << " that attempts to sub-class corrupt class " << PrettyClass(super);
912 return false;
913 } else if (super->IsFinal()) {
914 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
915 << " that attempts to sub-class final class " << PrettyClass(super);
916 return false;
917 }
918 }
jeffhaobdb76512011-09-07 11:43:16 -0700919 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
920 Method* method = klass->GetDirectMethod(i);
921 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
923 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700924 return false;
925 }
926 }
927 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
928 Method* method = klass->GetVirtualMethod(i);
929 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
931 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700932 return false;
933 }
934 }
935 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936}
937
jeffhaobdb76512011-09-07 11:43:16 -0700938bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 DexVerifier verifier(method);
940 bool success = verifier.Verify();
941 // We expect either success and no verification error, or failure and a generic failure to
942 // reject the class.
943 if (success) {
944 if (verifier.failure_ != VERIFY_ERROR_NONE) {
945 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
946 << verifier.fail_messages_;
947 }
948 } else {
949 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700950 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700951 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700952 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700953 verifier.Dump(std::cout);
954 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
956 }
957 return success;
958}
959
Shih-wei Liao371814f2011-10-27 16:52:10 -0700960void DexVerifier::VerifyMethodAndDump(Method* method) {
961 DexVerifier verifier(method);
962 verifier.Verify();
963
Elliott Hughese0918552011-10-28 17:18:29 -0700964 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
965 << verifier.fail_messages_.str() << std::endl
966 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700967}
968
Ian Rogers28ad40d2011-10-27 15:19:26 -0700969DexVerifier::DexVerifier(Method* method) : work_insn_idx_(-1), method_(method),
970 failure_(VERIFY_ERROR_NONE),
971
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700973 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
974 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 dex_file_ = &class_linker->FindDexFile(dex_cache);
976 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700977}
978
Ian Rogersd81871c2011-10-03 13:57:23 -0700979bool DexVerifier::Verify() {
980 // If there aren't any instructions, make sure that's expected, then exit successfully.
981 if (code_item_ == NULL) {
982 if (!method_->IsNative() && !method_->IsAbstract()) {
983 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700984 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 } else {
986 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700987 }
jeffhaobdb76512011-09-07 11:43:16 -0700988 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
990 if (code_item_->ins_size_ > code_item_->registers_size_) {
991 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
992 << " regs=" << code_item_->registers_size_;
993 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700994 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 // Allocate and initialize an array to hold instruction data.
996 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
997 // Run through the instructions and see if the width checks out.
998 bool result = ComputeWidthsAndCountOps();
999 // Flag instructions guarded by a "try" block and check exception handlers.
1000 result = result && ScanTryCatchBlocks();
1001 // Perform static instruction verification.
1002 result = result && VerifyInstructions();
1003 // Perform code flow analysis.
1004 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001005 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001006}
1007
Ian Rogersd81871c2011-10-03 13:57:23 -07001008bool DexVerifier::ComputeWidthsAndCountOps() {
1009 const uint16_t* insns = code_item_->insns_;
1010 size_t insns_size = code_item_->insns_size_in_code_units_;
1011 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001012 size_t new_instance_count = 0;
1013 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001015
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001017 Instruction::Code opcode = inst->Opcode();
1018 if (opcode == Instruction::NEW_INSTANCE) {
1019 new_instance_count++;
1020 } else if (opcode == Instruction::MONITOR_ENTER) {
1021 monitor_enter_count++;
1022 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 size_t inst_size = inst->SizeInCodeUnits();
1024 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1025 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001026 inst = inst->Next();
1027 }
1028
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 if (dex_pc != insns_size) {
1030 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1031 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001032 return false;
1033 }
1034
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 new_instance_count_ = new_instance_count;
1036 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001037 return true;
1038}
1039
Ian Rogersd81871c2011-10-03 13:57:23 -07001040bool DexVerifier::ScanTryCatchBlocks() {
1041 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001042 if (tries_size == 0) {
1043 return true;
1044 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001046 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001047
1048 for (uint32_t idx = 0; idx < tries_size; idx++) {
1049 const DexFile::TryItem* try_item = &tries[idx];
1050 uint32_t start = try_item->start_addr_;
1051 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001052 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1054 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001055 return false;
1056 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 if (!insn_flags_[start].IsOpcode()) {
1058 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001059 return false;
1060 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 for (uint32_t dex_pc = start; dex_pc < end;
1062 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1063 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001064 }
1065 }
jeffhaobdb76512011-09-07 11:43:16 -07001066 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogers0571d352011-11-03 19:51:38 -07001067 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001068 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001069 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001070 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001071 CatchHandlerIterator iterator(handlers_ptr);
1072 for (; iterator.HasNext(); iterator.Next()) {
1073 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 if (!insn_flags_[dex_pc].IsOpcode()) {
1075 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001076 return false;
1077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001079 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1080 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001081 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1082 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001083 if (exception_type == NULL) {
1084 DCHECK(Thread::Current()->IsExceptionPending());
1085 Thread::Current()->ClearException();
1086 }
1087 }
jeffhaobdb76512011-09-07 11:43:16 -07001088 }
Ian Rogers0571d352011-11-03 19:51:38 -07001089 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001090 }
jeffhaobdb76512011-09-07 11:43:16 -07001091 return true;
1092}
1093
Ian Rogersd81871c2011-10-03 13:57:23 -07001094bool DexVerifier::VerifyInstructions() {
1095 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001096
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 /* Flag the start of the method as a branch target. */
1098 insn_flags_[0].SetBranchTarget();
1099
1100 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1101 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1102 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001103 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1104 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 /* Flag instructions that are garbage collection points */
1108 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1109 insn_flags_[dex_pc].SetGcPoint();
1110 }
1111 dex_pc += inst->SizeInCodeUnits();
1112 inst = inst->Next();
1113 }
1114 return true;
1115}
1116
1117bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1118 Instruction::DecodedInstruction dec_insn(inst);
1119 bool result = true;
1120 switch (inst->GetVerifyTypeArgumentA()) {
1121 case Instruction::kVerifyRegA:
1122 result = result && CheckRegisterIndex(dec_insn.vA_);
1123 break;
1124 case Instruction::kVerifyRegAWide:
1125 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1126 break;
1127 }
1128 switch (inst->GetVerifyTypeArgumentB()) {
1129 case Instruction::kVerifyRegB:
1130 result = result && CheckRegisterIndex(dec_insn.vB_);
1131 break;
1132 case Instruction::kVerifyRegBField:
1133 result = result && CheckFieldIndex(dec_insn.vB_);
1134 break;
1135 case Instruction::kVerifyRegBMethod:
1136 result = result && CheckMethodIndex(dec_insn.vB_);
1137 break;
1138 case Instruction::kVerifyRegBNewInstance:
1139 result = result && CheckNewInstance(dec_insn.vB_);
1140 break;
1141 case Instruction::kVerifyRegBString:
1142 result = result && CheckStringIndex(dec_insn.vB_);
1143 break;
1144 case Instruction::kVerifyRegBType:
1145 result = result && CheckTypeIndex(dec_insn.vB_);
1146 break;
1147 case Instruction::kVerifyRegBWide:
1148 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1149 break;
1150 }
1151 switch (inst->GetVerifyTypeArgumentC()) {
1152 case Instruction::kVerifyRegC:
1153 result = result && CheckRegisterIndex(dec_insn.vC_);
1154 break;
1155 case Instruction::kVerifyRegCField:
1156 result = result && CheckFieldIndex(dec_insn.vC_);
1157 break;
1158 case Instruction::kVerifyRegCNewArray:
1159 result = result && CheckNewArray(dec_insn.vC_);
1160 break;
1161 case Instruction::kVerifyRegCType:
1162 result = result && CheckTypeIndex(dec_insn.vC_);
1163 break;
1164 case Instruction::kVerifyRegCWide:
1165 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1166 break;
1167 }
1168 switch (inst->GetVerifyExtraFlags()) {
1169 case Instruction::kVerifyArrayData:
1170 result = result && CheckArrayData(code_offset);
1171 break;
1172 case Instruction::kVerifyBranchTarget:
1173 result = result && CheckBranchTarget(code_offset);
1174 break;
1175 case Instruction::kVerifySwitchTargets:
1176 result = result && CheckSwitchTargets(code_offset);
1177 break;
1178 case Instruction::kVerifyVarArg:
1179 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1180 break;
1181 case Instruction::kVerifyVarArgRange:
1182 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1183 break;
1184 case Instruction::kVerifyError:
1185 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1186 result = false;
1187 break;
1188 }
1189 return result;
1190}
1191
1192bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1193 if (idx >= code_item_->registers_size_) {
1194 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1195 << code_item_->registers_size_ << ")";
1196 return false;
1197 }
1198 return true;
1199}
1200
1201bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1202 if (idx + 1 >= code_item_->registers_size_) {
1203 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1204 << "+1 >= " << code_item_->registers_size_ << ")";
1205 return false;
1206 }
1207 return true;
1208}
1209
1210bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1211 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1212 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1213 << dex_file_->GetHeader().field_ids_size_ << ")";
1214 return false;
1215 }
1216 return true;
1217}
1218
1219bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1220 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1221 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1222 << dex_file_->GetHeader().method_ids_size_ << ")";
1223 return false;
1224 }
1225 return true;
1226}
1227
1228bool DexVerifier::CheckNewInstance(uint32_t idx) {
1229 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1230 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1231 << dex_file_->GetHeader().type_ids_size_ << ")";
1232 return false;
1233 }
1234 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001235 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 if (descriptor[0] != 'L') {
1237 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1238 return false;
1239 }
1240 return true;
1241}
1242
1243bool DexVerifier::CheckStringIndex(uint32_t idx) {
1244 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1245 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1246 << dex_file_->GetHeader().string_ids_size_ << ")";
1247 return false;
1248 }
1249 return true;
1250}
1251
1252bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1253 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1254 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1255 << dex_file_->GetHeader().type_ids_size_ << ")";
1256 return false;
1257 }
1258 return true;
1259}
1260
1261bool DexVerifier::CheckNewArray(uint32_t idx) {
1262 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1263 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1264 << dex_file_->GetHeader().type_ids_size_ << ")";
1265 return false;
1266 }
1267 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001268 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 const char* cp = descriptor;
1270 while (*cp++ == '[') {
1271 bracket_count++;
1272 }
1273 if (bracket_count == 0) {
1274 /* The given class must be an array type. */
1275 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1276 return false;
1277 } else if (bracket_count > 255) {
1278 /* It is illegal to create an array of more than 255 dimensions. */
1279 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1280 return false;
1281 }
1282 return true;
1283}
1284
1285bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1286 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1287 const uint16_t* insns = code_item_->insns_ + cur_offset;
1288 const uint16_t* array_data;
1289 int32_t array_data_offset;
1290
1291 DCHECK_LT(cur_offset, insn_count);
1292 /* make sure the start of the array data table is in range */
1293 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1294 if ((int32_t) cur_offset + array_data_offset < 0 ||
1295 cur_offset + array_data_offset + 2 >= insn_count) {
1296 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1297 << ", data offset " << array_data_offset << ", count " << insn_count;
1298 return false;
1299 }
1300 /* offset to array data table is a relative branch-style offset */
1301 array_data = insns + array_data_offset;
1302 /* make sure the table is 32-bit aligned */
1303 if ((((uint32_t) array_data) & 0x03) != 0) {
1304 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1305 << ", data offset " << array_data_offset;
1306 return false;
1307 }
1308 uint32_t value_width = array_data[1];
1309 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1310 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1311 /* make sure the end of the switch is in range */
1312 if (cur_offset + array_data_offset + table_size > insn_count) {
1313 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1314 << ", data offset " << array_data_offset << ", end "
1315 << cur_offset + array_data_offset + table_size
1316 << ", count " << insn_count;
1317 return false;
1318 }
1319 return true;
1320}
1321
1322bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1323 int32_t offset;
1324 bool isConditional, selfOkay;
1325 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1326 return false;
1327 }
1328 if (!selfOkay && offset == 0) {
1329 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1330 return false;
1331 }
1332 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1333 // identical "wrap-around" behavior, but it's unwise to depend on that.
1334 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1335 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1336 return false;
1337 }
1338 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1339 int32_t abs_offset = cur_offset + offset;
1340 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1341 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1342 << (void*) abs_offset << ") at " << (void*) cur_offset;
1343 return false;
1344 }
1345 insn_flags_[abs_offset].SetBranchTarget();
1346 return true;
1347}
1348
1349bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1350 bool* selfOkay) {
1351 const uint16_t* insns = code_item_->insns_ + cur_offset;
1352 *pConditional = false;
1353 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 switch (*insns & 0xff) {
1355 case Instruction::GOTO:
1356 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 break;
1358 case Instruction::GOTO_32:
1359 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001360 *selfOkay = true;
1361 break;
1362 case Instruction::GOTO_16:
1363 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 break;
1365 case Instruction::IF_EQ:
1366 case Instruction::IF_NE:
1367 case Instruction::IF_LT:
1368 case Instruction::IF_GE:
1369 case Instruction::IF_GT:
1370 case Instruction::IF_LE:
1371 case Instruction::IF_EQZ:
1372 case Instruction::IF_NEZ:
1373 case Instruction::IF_LTZ:
1374 case Instruction::IF_GEZ:
1375 case Instruction::IF_GTZ:
1376 case Instruction::IF_LEZ:
1377 *pOffset = (int16_t) insns[1];
1378 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001379 break;
1380 default:
1381 return false;
1382 break;
1383 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001384 return true;
1385}
1386
Ian Rogersd81871c2011-10-03 13:57:23 -07001387bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1388 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001389 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1393 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1394 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1395 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001396 return false;
1397 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001398 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 /* make sure the table is 32-bit aligned */
1401 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1403 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001404 return false;
1405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 uint32_t switch_count = switch_insns[1];
1407 int32_t keys_offset, targets_offset;
1408 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1410 /* 0=sig, 1=count, 2/3=firstKey */
1411 targets_offset = 4;
1412 keys_offset = -1;
1413 expected_signature = Instruction::kPackedSwitchSignature;
1414 } else {
1415 /* 0=sig, 1=count, 2..count*2 = keys */
1416 keys_offset = 2;
1417 targets_offset = 2 + 2 * switch_count;
1418 expected_signature = Instruction::kSparseSwitchSignature;
1419 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001422 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1423 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 return false;
1425 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001426 /* make sure the end of the switch is in range */
1427 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1429 << switch_offset << ", end "
1430 << (cur_offset + switch_offset + table_size)
1431 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 return false;
1433 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001434 /* for a sparse switch, verify the keys are in ascending order */
1435 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1437 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1439 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1440 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1442 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 return false;
1444 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 last_key = key;
1446 }
1447 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001448 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 for (uint32_t targ = 0; targ < switch_count; targ++) {
1450 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1451 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1452 int32_t abs_offset = cur_offset + offset;
1453 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1454 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1455 << (void*) abs_offset << ") at "
1456 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 return false;
1458 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 insn_flags_[abs_offset].SetBranchTarget();
1460 }
1461 return true;
1462}
1463
1464bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1465 if (vA > 5) {
1466 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1467 return false;
1468 }
1469 uint16_t registers_size = code_item_->registers_size_;
1470 for (uint32_t idx = 0; idx < vA; idx++) {
1471 if (arg[idx] > registers_size) {
1472 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1473 << ") in non-range invoke (> " << registers_size << ")";
1474 return false;
1475 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 }
1477
1478 return true;
1479}
1480
Ian Rogersd81871c2011-10-03 13:57:23 -07001481bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1482 uint16_t registers_size = code_item_->registers_size_;
1483 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1484 // integer overflow when adding them here.
1485 if (vA + vC > registers_size) {
1486 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1487 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 return false;
1489 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 return true;
1491}
1492
Ian Rogersd81871c2011-10-03 13:57:23 -07001493bool DexVerifier::VerifyCodeFlow() {
1494 uint16_t registers_size = code_item_->registers_size_;
1495 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001496
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 if (registers_size * insns_size > 4*1024*1024) {
1498 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1499 << " insns_size=" << insns_size << ")";
1500 }
1501 /* Create and initialize table holding register status */
1502 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1503 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001504
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 work_line_.reset(new RegisterLine(registers_size, this));
1506 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001507
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 /* Initialize register types of method arguments. */
1509 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001510 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1511 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 return false;
1513 }
1514 /* Perform code flow verification. */
1515 if (!CodeFlowVerifyMethod()) {
1516 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001517 }
1518
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 /* Generate a register map and add it to the method. */
1520 ByteArray* map = GenerateGcMap();
1521 if (map == NULL) {
1522 return false; // Not a real failure, but a failure to encode
1523 }
1524 method_->SetGcMap(map);
1525#ifndef NDEBUG
1526 VerifyGcMap();
1527#endif
jeffhaobdb76512011-09-07 11:43:16 -07001528 return true;
1529}
1530
Ian Rogersd81871c2011-10-03 13:57:23 -07001531void DexVerifier::Dump(std::ostream& os) {
1532 if (method_->IsNative()) {
1533 os << "Native method" << std::endl;
1534 return;
jeffhaobdb76512011-09-07 11:43:16 -07001535 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 DCHECK(code_item_ != NULL);
1537 const Instruction* inst = Instruction::At(code_item_->insns_);
1538 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1539 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001540 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1541 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1543 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001544 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001545 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001546 inst = inst->Next();
1547 }
jeffhaobdb76512011-09-07 11:43:16 -07001548}
1549
Ian Rogersd81871c2011-10-03 13:57:23 -07001550static bool IsPrimitiveDescriptor(char descriptor) {
1551 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001552 case 'I':
1553 case 'C':
1554 case 'S':
1555 case 'B':
1556 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001557 case 'F':
1558 case 'D':
1559 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001561 default:
1562 return false;
1563 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001564}
1565
Ian Rogersd81871c2011-10-03 13:57:23 -07001566bool DexVerifier::SetTypesFromSignature() {
1567 RegisterLine* reg_line = reg_table_.GetLine(0);
1568 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1569 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001570
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1572 //Include the "this" pointer.
1573 size_t cur_arg = 0;
1574 if (!method_->IsStatic()) {
1575 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1576 // argument as uninitialized. This restricts field access until the superclass constructor is
1577 // called.
1578 Class* declaring_class = method_->GetDeclaringClass();
1579 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1580 reg_line->SetRegisterType(arg_start + cur_arg,
1581 reg_types_.UninitializedThisArgument(declaring_class));
1582 } else {
1583 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001586 }
1587
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001588 const DexFile::ProtoId& proto_id =
1589 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001590 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001591
1592 for (; iterator.HasNext(); iterator.Next()) {
1593 const char* descriptor = iterator.GetDescriptor();
1594 if (descriptor == NULL) {
1595 LOG(FATAL) << "Null descriptor";
1596 }
1597 if (cur_arg >= expected_args) {
1598 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1599 << " args, found more (" << descriptor << ")";
1600 return false;
1601 }
1602 switch (descriptor[0]) {
1603 case 'L':
1604 case '[':
1605 // We assume that reference arguments are initialized. The only way it could be otherwise
1606 // (assuming the caller was verified) is if the current method is <init>, but in that case
1607 // it's effectively considered initialized the instant we reach here (in the sense that we
1608 // can return without doing anything or call virtual methods).
1609 {
1610 const RegType& reg_type =
1611 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001612 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 }
1614 break;
1615 case 'Z':
1616 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1617 break;
1618 case 'C':
1619 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1620 break;
1621 case 'B':
1622 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1623 break;
1624 case 'I':
1625 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1626 break;
1627 case 'S':
1628 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1629 break;
1630 case 'F':
1631 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1632 break;
1633 case 'J':
1634 case 'D': {
1635 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1636 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1637 cur_arg++;
1638 break;
1639 }
1640 default:
1641 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1642 return false;
1643 }
1644 cur_arg++;
1645 }
1646 if (cur_arg != expected_args) {
1647 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1648 return false;
1649 }
1650 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1651 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1652 // format. Only major difference from the method argument format is that 'V' is supported.
1653 bool result;
1654 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1655 result = descriptor[1] == '\0';
1656 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1657 size_t i = 0;
1658 do {
1659 i++;
1660 } while (descriptor[i] == '['); // process leading [
1661 if (descriptor[i] == 'L') { // object array
1662 do {
1663 i++; // find closing ;
1664 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1665 result = descriptor[i] == ';';
1666 } else { // primitive array
1667 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1668 }
1669 } else if (descriptor[0] == 'L') {
1670 // could be more thorough here, but shouldn't be required
1671 size_t i = 0;
1672 do {
1673 i++;
1674 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1675 result = descriptor[i] == ';';
1676 } else {
1677 result = false;
1678 }
1679 if (!result) {
1680 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1681 << descriptor << "'";
1682 }
1683 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001684}
1685
Ian Rogersd81871c2011-10-03 13:57:23 -07001686bool DexVerifier::CodeFlowVerifyMethod() {
1687 const uint16_t* insns = code_item_->insns_;
1688 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001689
jeffhaobdb76512011-09-07 11:43:16 -07001690 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 insn_flags_[0].SetChanged();
1692 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001693
jeffhaobdb76512011-09-07 11:43:16 -07001694 /* Continue until no instructions are marked "changed". */
1695 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1697 uint32_t insn_idx = start_guess;
1698 for (; insn_idx < insns_size; insn_idx++) {
1699 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001700 break;
1701 }
jeffhaobdb76512011-09-07 11:43:16 -07001702 if (insn_idx == insns_size) {
1703 if (start_guess != 0) {
1704 /* try again, starting from the top */
1705 start_guess = 0;
1706 continue;
1707 } else {
1708 /* all flags are clear */
1709 break;
1710 }
1711 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 // We carry the working set of registers from instruction to instruction. If this address can
1713 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1714 // "changed" flags, we need to load the set of registers from the table.
1715 // Because we always prefer to continue on to the next instruction, we should never have a
1716 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1717 // target.
1718 work_insn_idx_ = insn_idx;
1719 if (insn_flags_[insn_idx].IsBranchTarget()) {
1720 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001721 } else {
1722#ifndef NDEBUG
1723 /*
1724 * Sanity check: retrieve the stored register line (assuming
1725 * a full table) and make sure it actually matches.
1726 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1728 if (register_line != NULL) {
1729 if (work_line_->CompareLine(register_line) != 0) {
1730 Dump(std::cout);
1731 std::cout << info_messages_.str();
1732 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1733 << "@" << (void*)work_insn_idx_ << std::endl
1734 << " work_line=" << *work_line_ << std::endl
1735 << " expected=" << *register_line;
1736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 }
1738#endif
1739 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 if (!CodeFlowVerifyInstruction(&start_guess)) {
1741 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001742 return false;
1743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 insn_flags_[insn_idx].SetVisited();
1746 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001750 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001751 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001752 * (besides the wasted space), but it indicates a flaw somewhere
1753 * down the line, possibly in the verifier.
1754 *
1755 * If we've substituted "always throw" instructions into the stream,
1756 * we are almost certainly going to have some dead code.
1757 */
1758 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 uint32_t insn_idx = 0;
1760 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001761 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001762 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001763 * may or may not be preceded by a padding NOP (for alignment).
1764 */
1765 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1766 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1767 insns[insn_idx] == Instruction::kArrayDataSignature ||
1768 (insns[insn_idx] == Instruction::NOP &&
1769 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1770 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1771 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001773 }
1774
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001776 if (dead_start < 0)
1777 dead_start = insn_idx;
1778 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001780 dead_start = -1;
1781 }
1782 }
1783 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001785 }
1786 }
jeffhaobdb76512011-09-07 11:43:16 -07001787 return true;
1788}
1789
Ian Rogersd81871c2011-10-03 13:57:23 -07001790bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001791#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001793 gDvm.verifierStats.instrsReexamined++;
1794 } else {
1795 gDvm.verifierStats.instrsExamined++;
1796 }
1797#endif
1798
1799 /*
1800 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001801 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001802 * control to another statement:
1803 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001804 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001805 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001806 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001807 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001808 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001809 * throw an exception that is handled by an encompassing "try"
1810 * block.
1811 *
1812 * We can also return, in which case there is no successor instruction
1813 * from this point.
1814 *
1815 * The behavior can be determined from the OpcodeFlags.
1816 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1818 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001819 Instruction::DecodedInstruction dec_insn(inst);
1820 int opcode_flag = inst->Flag();
1821
jeffhaobdb76512011-09-07 11:43:16 -07001822 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001823 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001824 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001826 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1827 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 }
jeffhaobdb76512011-09-07 11:43:16 -07001829
1830 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001831 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001832 * can throw an exception, we will copy/merge this into the "catch"
1833 * address rather than work_line, because we don't want the result
1834 * from the "successful" code path (e.g. a check-cast that "improves"
1835 * a type) to be visible to the exception handler.
1836 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1838 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001839 } else {
1840#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001842#endif
1843 }
1844
1845 switch (dec_insn.opcode_) {
1846 case Instruction::NOP:
1847 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001848 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001849 * a signature that looks like a NOP; if we see one of these in
1850 * the course of executing code then we have a problem.
1851 */
1852 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
1855 break;
1856
1857 case Instruction::MOVE:
1858 case Instruction::MOVE_FROM16:
1859 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
1862 case Instruction::MOVE_WIDE:
1863 case Instruction::MOVE_WIDE_FROM16:
1864 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867 case Instruction::MOVE_OBJECT:
1868 case Instruction::MOVE_OBJECT_FROM16:
1869 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001871 break;
1872
1873 /*
1874 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001875 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001876 * might want to hold the result in an actual CPU register, so the
1877 * Dalvik spec requires that these only appear immediately after an
1878 * invoke or filled-new-array.
1879 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001880 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001881 * redundant with the reset done below, but it can make the debug info
1882 * easier to read in some cases.)
1883 */
1884 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001886 break;
1887 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001895 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 * This statement can only appear as the first instruction in an exception handler (though not
1897 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001898 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001899 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001900 const RegType& res_type = GetCaughtExceptionType();
1901 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 }
jeffhaobdb76512011-09-07 11:43:16 -07001904 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1906 if (!GetMethodReturnType().IsUnknown()) {
1907 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1908 }
jeffhaobdb76512011-09-07 11:43:16 -07001909 }
1910 break;
1911 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001913 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 const RegType& return_type = GetMethodReturnType();
1915 if (!return_type.IsCategory1Types()) {
1916 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1917 } else {
1918 // Compilers may generate synthetic functions that write byte values into boolean fields.
1919 // Also, it may use integer values for boolean, byte, short, and character return types.
1920 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1921 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1922 ((return_type.IsBoolean() || return_type.IsByte() ||
1923 return_type.IsShort() || return_type.IsChar()) &&
1924 src_type.IsInteger()));
1925 /* check the register contents */
1926 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1927 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001928 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 }
jeffhaobdb76512011-09-07 11:43:16 -07001930 }
1931 }
1932 break;
1933 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001935 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 const RegType& return_type = GetMethodReturnType();
1937 if (!return_type.IsCategory2Types()) {
1938 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1939 } else {
1940 /* check the register contents */
1941 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1942 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001943 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 }
jeffhaobdb76512011-09-07 11:43:16 -07001945 }
1946 }
1947 break;
1948 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1950 const RegType& return_type = GetMethodReturnType();
1951 if (!return_type.IsReferenceTypes()) {
1952 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1953 } else {
1954 /* return_type is the *expected* return type, not register value */
1955 DCHECK(!return_type.IsZero());
1956 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001957 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1958 // Disallow returning uninitialized values and verify that the reference in vAA is an
1959 // instance of the "return_type"
1960 if (reg_type.IsUninitializedTypes()) {
1961 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1962 } else if (!return_type.IsAssignableFrom(reg_type)) {
1963 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1964 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001965 }
1966 }
1967 }
1968 break;
1969
1970 case Instruction::CONST_4:
1971 case Instruction::CONST_16:
1972 case Instruction::CONST:
1973 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
1976 case Instruction::CONST_HIGH16:
1977 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 work_line_->SetRegisterType(dec_insn.vA_,
1979 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
1981 case Instruction::CONST_WIDE_16:
1982 case Instruction::CONST_WIDE_32:
1983 case Instruction::CONST_WIDE:
1984 case Instruction::CONST_WIDE_HIGH16:
1985 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
1988 case Instruction::CONST_STRING:
1989 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001993 // Get type from instruction if unresolved then we need an access check
1994 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1995 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
1996 // Register holds class, ie its type is class, but on error we keep it Unknown
1997 work_line_->SetRegisterType(dec_insn.vA_,
1998 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001999 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
2004 case Instruction::MONITOR_EXIT:
2005 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002006 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002007 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002008 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002009 * to the need to handle asynchronous exceptions, a now-deprecated
2010 * feature that Dalvik doesn't support.)
2011 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002012 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002013 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002014 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002015 * structured locking checks are working, the former would have
2016 * failed on the -enter instruction, and the latter is impossible.
2017 *
2018 * This is fortunate, because issue 3221411 prevents us from
2019 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002020 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002021 * some catch blocks (which will show up as "dead" code when
2022 * we skip them here); if we can't, then the code path could be
2023 * "live" so we still need to check it.
2024 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 opcode_flag &= ~Instruction::kThrow;
2026 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028
Ian Rogers28ad40d2011-10-27 15:19:26 -07002029 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002031 /*
2032 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2033 * could be a "upcast" -- not expected, so we don't try to address it.)
2034 *
2035 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2036 * dec_insn.vA_ when branching to a handler.
2037 */
2038 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2039 const RegType& res_type =
2040 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002041 if (res_type.IsUnknown()) {
2042 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2043 break; // couldn't resolve class
2044 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002045 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2046 const RegType& orig_type =
2047 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2048 if (!res_type.IsNonZeroReferenceTypes()) {
2049 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2050 } else if (!orig_type.IsReferenceTypes()) {
2051 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002052 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002053 if (is_checkcast) {
2054 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002057 }
jeffhaobdb76512011-09-07 11:43:16 -07002058 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002059 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 }
2061 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002062 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2063 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002064 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002065 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 } else {
2067 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2068 }
2069 }
2070 break;
2071 }
2072 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002073 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2074 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2075 // can't create an instance of an interface or abstract class */
2076 if (!res_type.IsInstantiableTypes()) {
2077 Fail(VERIFY_ERROR_INSTANTIATION)
2078 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002080 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2081 // Any registers holding previous allocations from this address that have not yet been
2082 // initialized must be marked invalid.
2083 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2084 // add the new uninitialized reference to the register state
2085 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 }
2087 break;
2088 }
2089 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002090 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2091 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2092 if (!res_type.IsArrayClass()) {
2093 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002094 } else {
2095 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002097 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002098 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002099 }
2100 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 }
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002104 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2105 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2106 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002108 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002109 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002110 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002111 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002112 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002113 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002114 just_set_result = true;
2115 }
2116 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 }
jeffhaobdb76512011-09-07 11:43:16 -07002118 case Instruction::CMPL_FLOAT:
2119 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2121 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2122 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::CMPL_DOUBLE:
2125 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2127 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2128 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2132 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2133 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002136 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2137 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2138 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002139 }
2140 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 }
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::GOTO:
2143 case Instruction::GOTO_16:
2144 case Instruction::GOTO_32:
2145 /* no effect on or use of registers */
2146 break;
2147
2148 case Instruction::PACKED_SWITCH:
2149 case Instruction::SPARSE_SWITCH:
2150 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 case Instruction::FILL_ARRAY_DATA: {
2155 /* Similar to the verification done for APUT */
2156 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2157 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002158 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 if (res_class != NULL) {
2160 Class* component_type = res_class->GetComponentType();
2161 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2162 component_type->IsPrimitiveVoid()) {
2163 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002164 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 } else {
2166 const RegType& value_type = reg_types_.FromClass(component_type);
2167 DCHECK(!value_type.IsUnknown());
2168 // Now verify if the element width in the table matches the element width declared in
2169 // the array
2170 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2171 if (array_data[0] != Instruction::kArrayDataSignature) {
2172 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2173 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002174 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 // Since we don't compress the data in Dex, expect to see equal width of data stored
2176 // in the table and expected from the array class.
2177 if (array_data[1] != elem_width) {
2178 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2179 << " vs " << elem_width << ")";
2180 }
2181 }
2182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 }
2184 }
2185 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 }
jeffhaobdb76512011-09-07 11:43:16 -07002187 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 case Instruction::IF_NE: {
2189 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2190 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2191 bool mismatch = false;
2192 if (reg_type1.IsZero()) { // zero then integral or reference expected
2193 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2194 } else if (reg_type1.IsReferenceTypes()) { // both references?
2195 mismatch = !reg_type2.IsReferenceTypes();
2196 } else { // both integral?
2197 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2198 }
2199 if (mismatch) {
2200 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2201 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002202 }
2203 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 }
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::IF_LT:
2206 case Instruction::IF_GE:
2207 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 case Instruction::IF_LE: {
2209 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2210 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2211 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2212 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2213 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002214 }
2215 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 }
jeffhaobdb76512011-09-07 11:43:16 -07002217 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 case Instruction::IF_NEZ: {
2219 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2220 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2221 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2222 }
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 }
jeffhaobdb76512011-09-07 11:43:16 -07002225 case Instruction::IF_LTZ:
2226 case Instruction::IF_GEZ:
2227 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 case Instruction::IF_LEZ: {
2229 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2230 if (!reg_type.IsIntegralTypes()) {
2231 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2232 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2233 }
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 }
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2238 break;
jeffhaobdb76512011-09-07 11:43:16 -07002239 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2241 break;
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 VerifyAGet(dec_insn, reg_types_.Char(), true);
2244 break;
jeffhaobdb76512011-09-07 11:43:16 -07002245 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 case Instruction::AGET:
2249 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2250 break;
jeffhaobdb76512011-09-07 11:43:16 -07002251 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 VerifyAGet(dec_insn, reg_types_.Long(), true);
2253 break;
2254 case Instruction::AGET_OBJECT:
2255 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 case Instruction::APUT_BOOLEAN:
2259 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2260 break;
2261 case Instruction::APUT_BYTE:
2262 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2263 break;
2264 case Instruction::APUT_CHAR:
2265 VerifyAPut(dec_insn, reg_types_.Char(), true);
2266 break;
2267 case Instruction::APUT_SHORT:
2268 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
2279
jeffhaobdb76512011-09-07 11:43:16 -07002280 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002281 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 break;
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002284 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 break;
jeffhaobdb76512011-09-07 11:43:16 -07002286 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002287 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
jeffhaobdb76512011-09-07 11:43:16 -07002289 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002290 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
2292 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002293 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002296 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002299 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 break;
jeffhaobdb76512011-09-07 11:43:16 -07002301
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002303 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 break;
2305 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002306 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
2308 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002309 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
2311 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002312 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002315 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002318 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 break;
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002321 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323
jeffhaobdb76512011-09-07 11:43:16 -07002324 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002325 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
jeffhaobdb76512011-09-07 11:43:16 -07002327 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002328 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 break;
jeffhaobdb76512011-09-07 11:43:16 -07002330 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002331 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 break;
jeffhaobdb76512011-09-07 11:43:16 -07002333 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002334 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 break;
2336 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002337 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002340 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002343 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 break;
2345
2346 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002347 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
2349 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002350 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 break;
2352 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002353 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 break;
2355 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002356 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002359 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002362 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002365 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367
2368 case Instruction::INVOKE_VIRTUAL:
2369 case Instruction::INVOKE_VIRTUAL_RANGE:
2370 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 case Instruction::INVOKE_SUPER_RANGE: {
2372 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2373 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2374 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2375 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2376 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2377 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002378 const char* descriptor;
2379 if (called_method == NULL) {
2380 uint32_t method_idx = dec_insn.vB_;
2381 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2382 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002383 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002384 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002385 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002386 }
Ian Rogers9074b992011-10-26 17:41:55 -07002387 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002388 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002390 just_set_result = true;
2391 }
2392 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 }
jeffhaobdb76512011-09-07 11:43:16 -07002394 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 case Instruction::INVOKE_DIRECT_RANGE: {
2396 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2397 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2398 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002399 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 * Some additional checks when calling a constructor. We know from the invocation arg check
2401 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2402 * that to require that called_method->klass is the same as this->klass or this->super,
2403 * allowing the latter only if the "this" argument is the same as the "this" argument to
2404 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002405 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002406 bool is_constructor;
2407 if (called_method != NULL) {
2408 is_constructor = called_method->IsConstructor();
2409 } else {
2410 uint32_t method_idx = dec_insn.vB_;
2411 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2412 const char* name = dex_file_->GetMethodName(method_id);
2413 is_constructor = strcmp(name, "<init>") == 0;
2414 }
2415 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2417 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419
2420 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 if (this_type.IsZero()) {
2422 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002425 if (called_method != NULL) {
2426 Class* this_class = this_type.GetClass();
2427 DCHECK(this_class != NULL);
2428 /* must be in same class or in superclass */
2429 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2430 if (this_class != method_->GetDeclaringClass()) {
2431 Fail(VERIFY_ERROR_GENERIC)
2432 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2433 break;
2434 }
2435 } else if (called_method->GetDeclaringClass() != this_class) {
2436 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 }
jeffhaobdb76512011-09-07 11:43:16 -07002439 }
2440
2441 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002442 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2444 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
2446 }
2447
2448 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002449 * Replace the uninitialized reference with an initialized one. We need to do this for all
2450 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002451 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 work_line_->MarkRefsAsInitialized(this_type);
2453 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002455 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002456 const char* descriptor;
2457 if (called_method == NULL) {
2458 uint32_t method_idx = dec_insn.vB_;
2459 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2460 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002461 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002463 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 }
Ian Rogers9074b992011-10-26 17:41:55 -07002465 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002468 just_set_result = true;
2469 }
2470 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 }
jeffhaobdb76512011-09-07 11:43:16 -07002472 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 case Instruction::INVOKE_STATIC_RANGE: {
2474 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2475 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2476 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002477 const char* descriptor;
2478 if (called_method == NULL) {
2479 uint32_t method_idx = dec_insn.vB_;
2480 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2481 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002482 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002484 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002485 }
Ian Rogers9074b992011-10-26 17:41:55 -07002486 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 work_line_->SetResultRegisterType(return_type);
2489 just_set_result = true;
2490 }
jeffhaobdb76512011-09-07 11:43:16 -07002491 }
2492 break;
2493 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 case Instruction::INVOKE_INTERFACE_RANGE: {
2495 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2496 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2497 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002498 if (abs_method != NULL) {
2499 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002500 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002501 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2502 << PrettyMethod(abs_method) << "'";
2503 break;
2504 }
2505 }
2506 /* Get the type of the "this" arg, which should either be a sub-interface of called
2507 * interface or Object (see comments in RegType::JoinClass).
2508 */
2509 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2510 if (failure_ == VERIFY_ERROR_NONE) {
2511 if (this_type.IsZero()) {
2512 /* null pointer always passes (and always fails at runtime) */
2513 } else {
2514 if (this_type.IsUninitializedTypes()) {
2515 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2516 << this_type;
2517 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002519 // In the past we have tried to assert that "called_interface" is assignable
2520 // from "this_type.GetClass()", however, as we do an imprecise Join
2521 // (RegType::JoinClass) we don't have full information on what interfaces are
2522 // implemented by "this_type". For example, two classes may implement the same
2523 // interfaces and have a common parent that doesn't implement the interface. The
2524 // join will set "this_type" to the parent class and a test that this implements
2525 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002526 }
2527 }
jeffhaobdb76512011-09-07 11:43:16 -07002528 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 * We don't have an object instance, so we can't find the concrete method. However, all of
2530 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002531 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002532 const char* descriptor;
2533 if (abs_method == NULL) {
2534 uint32_t method_idx = dec_insn.vB_;
2535 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2536 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002537 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002538 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002539 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002540 }
Ian Rogers9074b992011-10-26 17:41:55 -07002541 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002542 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2543 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002545 just_set_result = true;
2546 }
2547 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 }
jeffhaobdb76512011-09-07 11:43:16 -07002549 case Instruction::NEG_INT:
2550 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::NEG_LONG:
2554 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
2587 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608
2609 case Instruction::ADD_INT:
2610 case Instruction::SUB_INT:
2611 case Instruction::MUL_INT:
2612 case Instruction::REM_INT:
2613 case Instruction::DIV_INT:
2614 case Instruction::SHL_INT:
2615 case Instruction::SHR_INT:
2616 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002618 break;
2619 case Instruction::AND_INT:
2620 case Instruction::OR_INT:
2621 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::ADD_LONG:
2625 case Instruction::SUB_LONG:
2626 case Instruction::MUL_LONG:
2627 case Instruction::DIV_LONG:
2628 case Instruction::REM_LONG:
2629 case Instruction::AND_LONG:
2630 case Instruction::OR_LONG:
2631 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::SHL_LONG:
2635 case Instruction::SHR_LONG:
2636 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 /* shift distance is Int, making these different from other binary operations */
2638 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::ADD_FLOAT:
2641 case Instruction::SUB_FLOAT:
2642 case Instruction::MUL_FLOAT:
2643 case Instruction::DIV_FLOAT:
2644 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::ADD_DOUBLE:
2648 case Instruction::SUB_DOUBLE:
2649 case Instruction::MUL_DOUBLE:
2650 case Instruction::DIV_DOUBLE:
2651 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::ADD_INT_2ADDR:
2655 case Instruction::SUB_INT_2ADDR:
2656 case Instruction::MUL_INT_2ADDR:
2657 case Instruction::REM_INT_2ADDR:
2658 case Instruction::SHL_INT_2ADDR:
2659 case Instruction::SHR_INT_2ADDR:
2660 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::AND_INT_2ADDR:
2664 case Instruction::OR_INT_2ADDR:
2665 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::ADD_LONG_2ADDR:
2672 case Instruction::SUB_LONG_2ADDR:
2673 case Instruction::MUL_LONG_2ADDR:
2674 case Instruction::DIV_LONG_2ADDR:
2675 case Instruction::REM_LONG_2ADDR:
2676 case Instruction::AND_LONG_2ADDR:
2677 case Instruction::OR_LONG_2ADDR:
2678 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::SHL_LONG_2ADDR:
2682 case Instruction::SHR_LONG_2ADDR:
2683 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::ADD_FLOAT_2ADDR:
2687 case Instruction::SUB_FLOAT_2ADDR:
2688 case Instruction::MUL_FLOAT_2ADDR:
2689 case Instruction::DIV_FLOAT_2ADDR:
2690 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
2693 case Instruction::ADD_DOUBLE_2ADDR:
2694 case Instruction::SUB_DOUBLE_2ADDR:
2695 case Instruction::MUL_DOUBLE_2ADDR:
2696 case Instruction::DIV_DOUBLE_2ADDR:
2697 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::ADD_INT_LIT16:
2701 case Instruction::RSUB_INT:
2702 case Instruction::MUL_INT_LIT16:
2703 case Instruction::DIV_INT_LIT16:
2704 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::AND_INT_LIT16:
2708 case Instruction::OR_INT_LIT16:
2709 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::ADD_INT_LIT8:
2713 case Instruction::RSUB_INT_LIT8:
2714 case Instruction::MUL_INT_LIT8:
2715 case Instruction::DIV_INT_LIT8:
2716 case Instruction::REM_INT_LIT8:
2717 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002718 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002719 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
2722 case Instruction::AND_INT_LIT8:
2723 case Instruction::OR_INT_LIT8:
2724 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
2727
2728 /*
2729 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002730 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002731 * inserted in the course of verification, we can expect to see it here.
2732 */
jeffhaob4df5142011-09-19 20:25:32 -07002733 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002737 case Instruction::UNUSED_EE:
2738 case Instruction::UNUSED_EF:
2739 case Instruction::UNUSED_F2:
2740 case Instruction::UNUSED_F3:
2741 case Instruction::UNUSED_F4:
2742 case Instruction::UNUSED_F5:
2743 case Instruction::UNUSED_F6:
2744 case Instruction::UNUSED_F7:
2745 case Instruction::UNUSED_F8:
2746 case Instruction::UNUSED_F9:
2747 case Instruction::UNUSED_FA:
2748 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002749 case Instruction::UNUSED_F0:
2750 case Instruction::UNUSED_F1:
2751 case Instruction::UNUSED_E3:
2752 case Instruction::UNUSED_E8:
2753 case Instruction::UNUSED_E7:
2754 case Instruction::UNUSED_E4:
2755 case Instruction::UNUSED_E9:
2756 case Instruction::UNUSED_FC:
2757 case Instruction::UNUSED_E5:
2758 case Instruction::UNUSED_EA:
2759 case Instruction::UNUSED_FD:
2760 case Instruction::UNUSED_E6:
2761 case Instruction::UNUSED_EB:
2762 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002763 case Instruction::UNUSED_3E:
2764 case Instruction::UNUSED_3F:
2765 case Instruction::UNUSED_40:
2766 case Instruction::UNUSED_41:
2767 case Instruction::UNUSED_42:
2768 case Instruction::UNUSED_43:
2769 case Instruction::UNUSED_73:
2770 case Instruction::UNUSED_79:
2771 case Instruction::UNUSED_7A:
2772 case Instruction::UNUSED_EC:
2773 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002774 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776
2777 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002778 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002779 * complain if an instruction is missing (which is desirable).
2780 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002782
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 if (failure_ != VERIFY_ERROR_NONE) {
2784 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002785 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002786 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002787 return false;
2788 } else {
2789 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002790 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002792 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002794 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002796 opcode_flag = Instruction::kThrow;
2797 }
2798 }
jeffhaobdb76512011-09-07 11:43:16 -07002799 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 * If we didn't just set the result register, clear it out. This ensures that you can only use
2801 * "move-result" immediately after the result is set. (We could check this statically, but it's
2802 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002803 */
2804 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002806 }
2807
jeffhaoa0a764a2011-09-16 10:43:38 -07002808 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002809 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2811 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2812 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002813 return false;
2814 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2816 // next instruction isn't one.
2817 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002818 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 }
2820 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2821 if (next_line != NULL) {
2822 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2823 // needed.
2824 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002825 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 }
jeffhaobdb76512011-09-07 11:43:16 -07002827 } else {
2828 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 * We're not recording register data for the next instruction, so we don't know what the prior
2830 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002831 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002833 }
2834 }
2835
2836 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002837 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002838 *
2839 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002840 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002841 * somebody could get a reference field, check it for zero, and if the
2842 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002843 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002844 * that, and will reject the code.
2845 *
2846 * TODO: avoid re-fetching the branch target
2847 */
2848 if ((opcode_flag & Instruction::kBranch) != 0) {
2849 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002851 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002853 return false;
2854 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002855 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002857 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 }
jeffhaobdb76512011-09-07 11:43:16 -07002859 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002861 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 }
jeffhaobdb76512011-09-07 11:43:16 -07002863 }
2864
2865 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002866 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002867 *
2868 * We've already verified that the table is structurally sound, so we
2869 * just need to walk through and tag the targets.
2870 */
2871 if ((opcode_flag & Instruction::kSwitch) != 0) {
2872 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2873 const uint16_t* switch_insns = insns + offset_to_switch;
2874 int switch_count = switch_insns[1];
2875 int offset_to_targets, targ;
2876
2877 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2878 /* 0 = sig, 1 = count, 2/3 = first key */
2879 offset_to_targets = 4;
2880 } else {
2881 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002882 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002883 offset_to_targets = 2 + 2 * switch_count;
2884 }
2885
2886 /* verify each switch target */
2887 for (targ = 0; targ < switch_count; targ++) {
2888 int offset;
2889 uint32_t abs_offset;
2890
2891 /* offsets are 32-bit, and only partly endian-swapped */
2892 offset = switch_insns[offset_to_targets + targ * 2] |
2893 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 abs_offset = work_insn_idx_ + offset;
2895 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2896 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002897 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
2899 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002900 return false;
2901 }
2902 }
2903
2904 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2906 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002907 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2909 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002910 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002911
Ian Rogers0571d352011-11-03 19:51:38 -07002912 for (; iterator.HasNext(); iterator.Next()) {
2913 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 within_catch_all = true;
2915 }
jeffhaobdb76512011-09-07 11:43:16 -07002916 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2918 * "work_regs", because at runtime the exception will be thrown before the instruction
2919 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002920 */
Ian Rogers0571d352011-11-03 19:51:38 -07002921 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002922 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 }
jeffhaobdb76512011-09-07 11:43:16 -07002924 }
2925
2926 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2928 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002929 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002931 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 * The state in work_line reflects the post-execution state. If the current instruction is a
2933 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002934 * it will do so before grabbing the lock).
2935 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2937 Fail(VERIFY_ERROR_GENERIC)
2938 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002939 return false;
2940 }
2941 }
2942 }
2943
jeffhaod1f0fde2011-09-08 17:25:33 -07002944 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 if ((opcode_flag & Instruction::kReturn) != 0) {
2946 if(!work_line_->VerifyMonitorStackEmpty()) {
2947 return false;
2948 }
jeffhaobdb76512011-09-07 11:43:16 -07002949 }
2950
2951 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002952 * Update start_guess. Advance to the next instruction of that's
2953 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002954 * neither of those exists we're in a return or throw; leave start_guess
2955 * alone and let the caller sort it out.
2956 */
2957 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002959 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2960 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002962 }
2963
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2965 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002966
2967 return true;
2968}
2969
Ian Rogers28ad40d2011-10-27 15:19:26 -07002970const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002971 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002972 Class* referrer = method_->GetDeclaringClass();
2973 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2974 const RegType& result =
2975 klass != NULL ? reg_types_.FromClass(klass)
2976 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2977 if (klass == NULL && !result.IsUnresolvedTypes()) {
2978 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002980 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2981 // check at runtime if access is allowed and so pass here.
2982 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2983 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002984 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002985 << result << "'";
2986 return reg_types_.Unknown();
2987 } else {
2988 return result;
2989 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002990}
2991
Ian Rogers28ad40d2011-10-27 15:19:26 -07002992const RegType& DexVerifier::GetCaughtExceptionType() {
2993 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002995 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2997 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002998 CatchHandlerIterator iterator(handlers_ptr);
2999 for (; iterator.HasNext(); iterator.Next()) {
3000 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3001 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003002 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003004 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3006 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3007 * test, so is essentially harmless.
3008 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003009 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3010 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3011 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003013 common_super = &exception;
3014 } else if (common_super->Equals(exception)) {
3015 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003017 common_super = &common_super->Merge(exception, &reg_types_);
3018 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
3020 }
3021 }
3022 }
Ian Rogers0571d352011-11-03 19:51:38 -07003023 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 }
3025 }
3026 if (common_super == NULL) {
3027 /* no catch blocks, or no catches with classes we can find */
3028 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3029 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003030 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003031}
3032
3033Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003034 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3035 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3036 if (failure_ != VERIFY_ERROR_NONE) {
3037 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3038 return NULL;
3039 }
3040 if(klass_type.IsUnresolvedTypes()) {
3041 return NULL; // Can't resolve Class so no more to do here
3042 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 Class* referrer = method_->GetDeclaringClass();
3044 DexCache* dex_cache = referrer->GetDexCache();
3045 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3046 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003047 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003048 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003049 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 if (is_direct) {
3051 res_method = klass->FindDirectMethod(name, signature);
3052 } else if (klass->IsInterface()) {
3053 res_method = klass->FindInterfaceMethod(name, signature);
3054 } else {
3055 res_method = klass->FindVirtualMethod(name, signature);
3056 }
3057 if (res_method != NULL) {
3058 dex_cache->SetResolvedMethod(method_idx, res_method);
3059 } else {
3060 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003061 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 << " " << signature;
3063 return NULL;
3064 }
3065 }
3066 /* Check if access is allowed. */
3067 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3068 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003069 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 return NULL;
3071 }
3072 return res_method;
3073}
3074
3075Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3076 MethodType method_type, bool is_range, bool is_super) {
3077 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3078 // we're making.
3079 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3080 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003081 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 return NULL;
3083 }
3084 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3085 // enforce them here.
3086 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3087 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3088 << PrettyMethod(res_method);
3089 return NULL;
3090 }
3091 // See if the method type implied by the invoke instruction matches the access flags for the
3092 // target method.
3093 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3094 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3095 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3096 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003097 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3098 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 return NULL;
3100 }
3101 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3102 // has a vtable entry for the target method.
3103 if (is_super) {
3104 DCHECK(method_type == METHOD_VIRTUAL);
3105 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3106 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3107 if (super == NULL) { // Only Object has no super class
3108 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3109 << " to super " << PrettyMethod(res_method);
3110 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003111 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003113 << " to super " << PrettyDescriptor(super)
3114 << "." << mh.GetName()
3115 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 }
3117 return NULL;
3118 }
3119 }
3120 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3121 // match the call to the signature. Also, we might might be calling through an abstract method
3122 // definition (which doesn't have register count values).
3123 int expected_args = dec_insn.vA_;
3124 /* caught by static verifier */
3125 DCHECK(is_range || expected_args <= 5);
3126 if (expected_args > code_item_->outs_size_) {
3127 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3128 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3129 return NULL;
3130 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003131 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003132 if (sig[0] != '(') {
3133 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3134 << " as descriptor doesn't start with '(': " << sig;
3135 return NULL;
3136 }
jeffhaobdb76512011-09-07 11:43:16 -07003137 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 * Check the "this" argument, which must be an instance of the class
3139 * that declared the method. For an interface class, we don't do the
3140 * full interface merge, so we can't do a rigorous check here (which
3141 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003142 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 int actual_args = 0;
3144 if (!res_method->IsStatic()) {
3145 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3146 if (failure_ != VERIFY_ERROR_NONE) {
3147 return NULL;
3148 }
3149 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3150 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3151 return NULL;
3152 }
3153 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003154 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3155 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3156 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3157 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 return NULL;
3159 }
3160 }
3161 actual_args++;
3162 }
3163 /*
3164 * Process the target method's signature. This signature may or may not
3165 * have been verified, so we can't assume it's properly formed.
3166 */
3167 size_t sig_offset = 0;
3168 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3169 if (actual_args >= expected_args) {
3170 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3171 << "'. Expected " << expected_args << " args, found more ("
3172 << sig.substr(sig_offset) << ")";
3173 return NULL;
3174 }
3175 std::string descriptor;
3176 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3177 size_t end;
3178 if (sig[sig_offset] == 'L') {
3179 end = sig.find(';', sig_offset);
3180 } else {
3181 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3182 if (sig[end] == 'L') {
3183 end = sig.find(';', end);
3184 }
3185 }
3186 if (end == std::string::npos) {
3187 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3188 << "bad signature component '" << sig << "' (missing ';')";
3189 return NULL;
3190 }
3191 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3192 sig_offset = end;
3193 } else {
3194 descriptor = sig[sig_offset];
3195 }
3196 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003197 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3198 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003199 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3200 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3201 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 }
3203 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3204 }
3205 if (sig[sig_offset] != ')') {
3206 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3207 return NULL;
3208 }
3209 if (actual_args != expected_args) {
3210 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3211 << " expected " << expected_args << " args, found " << actual_args;
3212 return NULL;
3213 } else {
3214 return res_method;
3215 }
3216}
3217
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003218const RegType& DexVerifier::GetMethodReturnType() {
3219 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3220 MethodHelper(method_).GetReturnTypeDescriptor());
3221}
3222
Ian Rogersd81871c2011-10-03 13:57:23 -07003223void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3224 const RegType& insn_type, bool is_primitive) {
3225 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3226 if (!index_type.IsArrayIndexTypes()) {
3227 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3228 } else {
3229 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3230 if (failure_ == VERIFY_ERROR_NONE) {
3231 if (array_class == NULL) {
3232 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3233 // instruction type. TODO: have a proper notion of bottom here.
3234 if (!is_primitive || insn_type.IsCategory1Types()) {
3235 // Reference or category 1
3236 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3237 } else {
3238 // Category 2
3239 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3240 }
3241 } else {
3242 /* verify the class */
3243 Class* component_class = array_class->GetComponentType();
3244 const RegType& component_type = reg_types_.FromClass(component_class);
3245 if (!array_class->IsArrayClass()) {
3246 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003247 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 } else if (component_class->IsPrimitive() && !is_primitive) {
3249 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003250 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 << " source for aget-object";
3252 } else if (!component_class->IsPrimitive() && is_primitive) {
3253 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003254 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 << " source for category 1 aget";
3256 } else if (is_primitive && !insn_type.Equals(component_type) &&
3257 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3258 (insn_type.IsLong() && component_type.IsDouble()))) {
3259 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003260 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 << " incompatible with aget of type " << insn_type;
3262 } else {
3263 // Use knowledge of the field type which is stronger than the type inferred from the
3264 // instruction, which can't differentiate object types and ints from floats, longs from
3265 // doubles.
3266 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3267 }
3268 }
3269 }
3270 }
3271}
3272
3273void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3274 const RegType& insn_type, bool is_primitive) {
3275 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3276 if (!index_type.IsArrayIndexTypes()) {
3277 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3278 } else {
3279 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3280 if (failure_ == VERIFY_ERROR_NONE) {
3281 if (array_class == NULL) {
3282 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3283 // instruction type.
3284 } else {
3285 /* verify the class */
3286 Class* component_class = array_class->GetComponentType();
3287 const RegType& component_type = reg_types_.FromClass(component_class);
3288 if (!array_class->IsArrayClass()) {
3289 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003290 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 } else if (component_class->IsPrimitive() && !is_primitive) {
3292 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003293 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 << " source for aput-object";
3295 } else if (!component_class->IsPrimitive() && is_primitive) {
3296 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003297 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 << " source for category 1 aput";
3299 } else if (is_primitive && !insn_type.Equals(component_type) &&
3300 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3301 (insn_type.IsLong() && component_type.IsDouble()))) {
3302 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003303 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 << " incompatible with aput of type " << insn_type;
3305 } else {
3306 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003307 // Note: we use the instruction type (rather than the component type) for aput-object as
3308 // incompatible classes will be caught at runtime as an array store exception
3309 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 }
3311 }
3312 }
3313 }
3314}
3315
3316Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003317 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3318 // Check access to class
3319 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3320 if (failure_ != VERIFY_ERROR_NONE) {
3321 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3322 << dex_file_->GetFieldName(field_id) << ") in "
3323 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3324 return NULL;
3325 }
3326 if(klass_type.IsUnresolvedTypes()) {
3327 return NULL; // Can't resolve Class so no more to do here
3328 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003329 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003331 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3332 << dex_file_->GetFieldName(field_id) << ") in "
3333 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 DCHECK(Thread::Current()->IsExceptionPending());
3335 Thread::Current()->ClearException();
3336 return NULL;
3337 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3338 field->GetAccessFlags())) {
3339 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3340 << " from " << PrettyClass(method_->GetDeclaringClass());
3341 return NULL;
3342 } else if (!field->IsStatic()) {
3343 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3344 return NULL;
3345 } else {
3346 return field;
3347 }
3348}
3349
Ian Rogersd81871c2011-10-03 13:57:23 -07003350Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003351 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3352 // Check access to class
3353 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3354 if (failure_ != VERIFY_ERROR_NONE) {
3355 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3356 << dex_file_->GetFieldName(field_id) << ") in "
3357 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3358 return NULL;
3359 }
3360 if(klass_type.IsUnresolvedTypes()) {
3361 return NULL; // Can't resolve Class so no more to do here
3362 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003363 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003365 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3366 << dex_file_->GetFieldName(field_id) << ") in "
3367 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 DCHECK(Thread::Current()->IsExceptionPending());
3369 Thread::Current()->ClearException();
3370 return NULL;
3371 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3372 field->GetAccessFlags())) {
3373 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3374 << " from " << PrettyClass(method_->GetDeclaringClass());
3375 return NULL;
3376 } else if (field->IsStatic()) {
3377 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3378 << " to not be static";
3379 return NULL;
3380 } else if (obj_type.IsZero()) {
3381 // Cannot infer and check type, however, access will cause null pointer exception
3382 return field;
3383 } else if(obj_type.IsUninitializedReference() &&
3384 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3385 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3386 // Field accesses through uninitialized references are only allowable for constructors where
3387 // the field is declared in this class
3388 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3389 << " of a not fully initialized object within the context of "
3390 << PrettyMethod(method_);
3391 return NULL;
3392 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3393 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3394 // of C1. For resolution to occur the declared class of the field must be compatible with
3395 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3396 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3397 << " from object of type " << PrettyClass(obj_type.GetClass());
3398 return NULL;
3399 } else {
3400 return field;
3401 }
3402}
3403
Ian Rogersb94a27b2011-10-26 00:33:41 -07003404void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3405 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003406 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003407 Field* field;
3408 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003409 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003410 } else {
3411 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003412 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003413 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003414 if (failure_ != VERIFY_ERROR_NONE) {
3415 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3416 } else {
3417 const char* descriptor;
3418 const ClassLoader* loader;
3419 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003420 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003421 loader = field->GetDeclaringClass()->GetClassLoader();
3422 } else {
3423 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3424 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3425 loader = method_->GetDeclaringClass()->GetClassLoader();
3426 }
3427 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003429 if (field_type.Equals(insn_type) ||
3430 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3431 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 // expected that read is of the correct primitive type or that int reads are reading
3433 // floats or long reads are reading doubles
3434 } else {
3435 // This is a global failure rather than a class change failure as the instructions and
3436 // the descriptors for the type should have been consistent within the same file at
3437 // compile time
3438 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003439 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003440 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 return;
3442 }
3443 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003444 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003446 << " to be compatible with type '" << insn_type
3447 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003448 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003449 return;
3450 }
3451 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003452 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 }
3454}
3455
Ian Rogersb94a27b2011-10-26 00:33:41 -07003456void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3457 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003458 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003459 Field* field;
3460 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003461 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003462 } else {
3463 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003464 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003465 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003466 if (failure_ != VERIFY_ERROR_NONE) {
3467 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3468 } else {
3469 const char* descriptor;
3470 const ClassLoader* loader;
3471 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003472 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003473 loader = field->GetDeclaringClass()->GetClassLoader();
3474 } else {
3475 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3476 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3477 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003479 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3480 if (field != NULL) {
3481 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3482 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3483 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3484 return;
3485 }
3486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003488 // Primitive field assignability rules are weaker than regular assignability rules
3489 bool instruction_compatible;
3490 bool value_compatible;
3491 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3492 if (field_type.IsIntegralTypes()) {
3493 instruction_compatible = insn_type.IsIntegralTypes();
3494 value_compatible = value_type.IsIntegralTypes();
3495 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003496 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003497 value_compatible = value_type.IsFloatTypes();
3498 } else if (field_type.IsLong()) {
3499 instruction_compatible = insn_type.IsLong();
3500 value_compatible = value_type.IsLongTypes();
3501 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003502 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003503 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003505 instruction_compatible = false; // reference field with primitive store
3506 value_compatible = false; // unused
3507 }
3508 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 // This is a global failure rather than a class change failure as the instructions and
3510 // the descriptors for the type should have been consistent within the same file at
3511 // compile time
3512 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003513 << " to be of type '" << insn_type
3514 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003515 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003516 return;
3517 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003518 if (!value_compatible) {
3519 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3520 << " of type " << value_type
3521 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003522 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003523 return;
3524 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003526 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003528 << " to be compatible with type '" << insn_type
3529 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003530 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 return;
3532 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003533 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 }
3536}
3537
3538bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3539 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3540 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3541 return false;
3542 }
3543 return true;
3544}
3545
3546void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003547 const RegType& res_type, bool is_range) {
3548 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 /*
3550 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3551 * list and fail. It's legal, if silly, for arg_count to be zero.
3552 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003553 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3554 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 uint32_t arg_count = dec_insn.vA_;
3556 for (size_t ui = 0; ui < arg_count; ui++) {
3557 uint32_t get_reg;
3558
3559 if (is_range)
3560 get_reg = dec_insn.vC_ + ui;
3561 else
3562 get_reg = dec_insn.arg_[ui];
3563
3564 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3565 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3566 << ") not valid";
3567 return;
3568 }
3569 }
3570}
3571
3572void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003573 if (Runtime::Current()->IsStarted()) {
3574 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3575 << " " << fail_messages_.str();
3576 return;
3577 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3579 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3580 VerifyErrorRefType ref_type;
3581 switch (inst->Opcode()) {
3582 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003583 case Instruction::CHECK_CAST:
3584 case Instruction::INSTANCE_OF:
3585 case Instruction::NEW_INSTANCE:
3586 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003588 case Instruction::FILLED_NEW_ARRAY_RANGE:
3589 ref_type = VERIFY_ERROR_REF_CLASS;
3590 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003592 case Instruction::IGET_BOOLEAN:
3593 case Instruction::IGET_BYTE:
3594 case Instruction::IGET_CHAR:
3595 case Instruction::IGET_SHORT:
3596 case Instruction::IGET_WIDE:
3597 case Instruction::IGET_OBJECT:
3598 case Instruction::IPUT:
3599 case Instruction::IPUT_BOOLEAN:
3600 case Instruction::IPUT_BYTE:
3601 case Instruction::IPUT_CHAR:
3602 case Instruction::IPUT_SHORT:
3603 case Instruction::IPUT_WIDE:
3604 case Instruction::IPUT_OBJECT:
3605 case Instruction::SGET:
3606 case Instruction::SGET_BOOLEAN:
3607 case Instruction::SGET_BYTE:
3608 case Instruction::SGET_CHAR:
3609 case Instruction::SGET_SHORT:
3610 case Instruction::SGET_WIDE:
3611 case Instruction::SGET_OBJECT:
3612 case Instruction::SPUT:
3613 case Instruction::SPUT_BOOLEAN:
3614 case Instruction::SPUT_BYTE:
3615 case Instruction::SPUT_CHAR:
3616 case Instruction::SPUT_SHORT:
3617 case Instruction::SPUT_WIDE:
3618 case Instruction::SPUT_OBJECT:
3619 ref_type = VERIFY_ERROR_REF_FIELD;
3620 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003622 case Instruction::INVOKE_VIRTUAL_RANGE:
3623 case Instruction::INVOKE_SUPER:
3624 case Instruction::INVOKE_SUPER_RANGE:
3625 case Instruction::INVOKE_DIRECT:
3626 case Instruction::INVOKE_DIRECT_RANGE:
3627 case Instruction::INVOKE_STATIC:
3628 case Instruction::INVOKE_STATIC_RANGE:
3629 case Instruction::INVOKE_INTERFACE:
3630 case Instruction::INVOKE_INTERFACE_RANGE:
3631 ref_type = VERIFY_ERROR_REF_METHOD;
3632 break;
jeffhaobdb76512011-09-07 11:43:16 -07003633 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003634 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003635 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003636 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003637 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3638 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3639 // instruction, so assert it.
3640 size_t width = inst->SizeInCodeUnits();
3641 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003642 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 // NOPs
3644 for (size_t i = 2; i < width; i++) {
3645 insns[work_insn_idx_ + i] = Instruction::NOP;
3646 }
3647 // Encode the opcode, with the failure code in the high byte
3648 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3649 (failure_ << 8) | // AA - component
3650 (ref_type << (8 + kVerifyErrorRefTypeShift));
3651 insns[work_insn_idx_] = new_instruction;
3652 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3653 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003654 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3655 << fail_messages_.str();
3656 if (gDebugVerify) {
3657 std::cout << std::endl << info_messages_.str();
3658 Dump(std::cout);
3659 }
jeffhaobdb76512011-09-07 11:43:16 -07003660}
jeffhaoba5ebb92011-08-25 17:24:37 -07003661
Ian Rogersd81871c2011-10-03 13:57:23 -07003662bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3663 const bool merge_debug = true;
3664 bool changed = true;
3665 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3666 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003667 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 * We haven't processed this instruction before, and we haven't touched the registers here, so
3669 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3670 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003671 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003673 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003674 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3675 copy->CopyFromLine(target_line);
3676 changed = target_line->MergeRegisters(merge_line);
3677 if (failure_ != VERIFY_ERROR_NONE) {
3678 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003679 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003680 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3682 << *copy.get() << " MERGE" << std::endl
3683 << *merge_line << " ==" << std::endl
3684 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003685 }
3686 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 if (changed) {
3688 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003689 }
3690 return true;
3691}
3692
Ian Rogersd81871c2011-10-03 13:57:23 -07003693void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3694 size_t* log2_max_gc_pc) {
3695 size_t local_gc_points = 0;
3696 size_t max_insn = 0;
3697 size_t max_ref_reg = -1;
3698 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3699 if (insn_flags_[i].IsGcPoint()) {
3700 local_gc_points++;
3701 max_insn = i;
3702 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003703 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003704 }
3705 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003706 *gc_points = local_gc_points;
3707 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3708 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003709 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 i++;
3711 }
3712 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003713}
3714
Ian Rogersd81871c2011-10-03 13:57:23 -07003715ByteArray* DexVerifier::GenerateGcMap() {
3716 size_t num_entries, ref_bitmap_bits, pc_bits;
3717 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3718 // There's a single byte to encode the size of each bitmap
3719 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3720 // TODO: either a better GC map format or per method failures
3721 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3722 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003723 return NULL;
3724 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3726 // There are 2 bytes to encode the number of entries
3727 if (num_entries >= 65536) {
3728 // TODO: either a better GC map format or per method failures
3729 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3730 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003731 return NULL;
3732 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003734 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003735 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003736 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003738 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003739 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003741 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 // TODO: either a better GC map format or per method failures
3743 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3744 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3745 return NULL;
3746 }
3747 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3748 ByteArray* table = ByteArray::Alloc(table_size);
3749 if (table == NULL) {
3750 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3751 return NULL;
3752 }
3753 // Write table header
3754 table->Set(0, format);
3755 table->Set(1, ref_bitmap_bytes);
3756 table->Set(2, num_entries & 0xFF);
3757 table->Set(3, (num_entries >> 8) & 0xFF);
3758 // Write table data
3759 size_t offset = 4;
3760 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3761 if (insn_flags_[i].IsGcPoint()) {
3762 table->Set(offset, i & 0xFF);
3763 offset++;
3764 if (pc_bytes == 2) {
3765 table->Set(offset, (i >> 8) & 0xFF);
3766 offset++;
3767 }
3768 RegisterLine* line = reg_table_.GetLine(i);
3769 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3770 offset += ref_bitmap_bytes;
3771 }
3772 }
3773 DCHECK(offset == table_size);
3774 return table;
3775}
jeffhaoa0a764a2011-09-16 10:43:38 -07003776
Ian Rogersd81871c2011-10-03 13:57:23 -07003777void DexVerifier::VerifyGcMap() {
3778 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3779 // that the table data is well formed and all references are marked (or not) in the bitmap
3780 PcToReferenceMap map(method_);
3781 size_t map_index = 0;
3782 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3783 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3784 if (insn_flags_[i].IsGcPoint()) {
3785 CHECK_LT(map_index, map.NumEntries());
3786 CHECK_EQ(map.GetPC(map_index), i);
3787 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3788 map_index++;
3789 RegisterLine* line = reg_table_.GetLine(i);
3790 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003791 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 CHECK_LT(j / 8, map.RegWidth());
3793 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3794 } else if ((j / 8) < map.RegWidth()) {
3795 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3796 } else {
3797 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3798 }
3799 }
3800 } else {
3801 CHECK(reg_bitmap == NULL);
3802 }
3803 }
3804}
jeffhaoa0a764a2011-09-16 10:43:38 -07003805
Ian Rogersd81871c2011-10-03 13:57:23 -07003806const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3807 size_t num_entries = NumEntries();
3808 // Do linear or binary search?
3809 static const size_t kSearchThreshold = 8;
3810 if (num_entries < kSearchThreshold) {
3811 for (size_t i = 0; i < num_entries; i++) {
3812 if (GetPC(i) == dex_pc) {
3813 return GetBitMap(i);
3814 }
3815 }
3816 } else {
3817 int lo = 0;
3818 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003819 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 int mid = (hi + lo) / 2;
3821 int mid_pc = GetPC(mid);
3822 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003823 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003825 hi = mid - 1;
3826 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003827 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003828 }
3829 }
3830 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003831 if (error_if_not_present) {
3832 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3833 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003834 return NULL;
3835}
3836
Ian Rogersd81871c2011-10-03 13:57:23 -07003837} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003838} // namespace art