blob: 1b4a26207dddc632e3e744f494d34f298c3782d0 [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"
Brian Carlstrome7d856b2012-01-11 18:10:55 -08008#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -07009#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070010#include "dex_file.h"
11#include "dex_instruction.h"
12#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070013#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070014#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070015#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070016#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080017#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070022namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070023
Ian Rogers2c8a8572011-10-24 17:11:36 -070024static const bool gDebugVerify = false;
25
Ian Rogersd81871c2011-10-03 13:57:23 -070026std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
27 return os << (int)rhs;
28}
jeffhaobdb76512011-09-07 11:43:16 -070029
Ian Rogers84fa0742011-10-25 18:13:30 -070030static const char* type_strings[] = {
31 "Unknown",
32 "Conflict",
33 "Boolean",
34 "Byte",
35 "Short",
36 "Char",
37 "Integer",
38 "Float",
39 "Long (Low Half)",
40 "Long (High Half)",
41 "Double (Low Half)",
42 "Double (High Half)",
43 "64-bit Constant (Low Half)",
44 "64-bit Constant (High Half)",
45 "32-bit Constant",
46 "Unresolved Reference",
47 "Uninitialized Reference",
48 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070049 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070050 "Reference",
51};
Ian Rogersd81871c2011-10-03 13:57:23 -070052
Ian Rogers2c8a8572011-10-24 17:11:36 -070053std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070054 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
55 std::string result;
56 if (IsConstant()) {
57 uint32_t val = ConstantValue();
58 if (val == 0) {
59 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070060 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070061 if(IsConstantShort()) {
62 result = StringPrintf("32-bit Constant: %d", val);
63 } else {
64 result = StringPrintf("32-bit Constant: 0x%x", val);
65 }
66 }
67 } else {
68 result = type_strings[type_];
69 if (IsReferenceTypes()) {
70 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -070071 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070072 result += PrettyDescriptor(GetDescriptor());
73 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080074 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -070075 }
Ian Rogersd81871c2011-10-03 13:57:23 -070076 }
77 }
Ian Rogers84fa0742011-10-25 18:13:30 -070078 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070079}
80
81const RegType& RegType::HighHalf(RegTypeCache* cache) const {
82 CHECK(IsLowHalf());
83 if (type_ == kRegTypeLongLo) {
84 return cache->FromType(kRegTypeLongHi);
85 } else if (type_ == kRegTypeDoubleLo) {
86 return cache->FromType(kRegTypeDoubleHi);
87 } else {
88 return cache->FromType(kRegTypeConstHi);
89 }
90}
91
92/*
93 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
94 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
95 * 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
96 * is the deepest (lowest upper bound) parent of S and T).
97 *
98 * This operation applies for regular classes and arrays, however, for interface types there needn't
99 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
100 * introducing sets of types, however, the only operation permissible on an interface is
101 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
102 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700103 * the perversion of any Object being assignable to an interface type (note, however, that we don't
104 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
105 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700106 */
107Class* RegType::ClassJoin(Class* s, Class* t) {
108 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
109 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
110 if (s == t) {
111 return s;
112 } else if (s->IsAssignableFrom(t)) {
113 return s;
114 } else if (t->IsAssignableFrom(s)) {
115 return t;
116 } else if (s->IsArrayClass() && t->IsArrayClass()) {
117 Class* s_ct = s->GetComponentType();
118 Class* t_ct = t->GetComponentType();
119 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
120 // Given the types aren't the same, if either array is of primitive types then the only
121 // common parent is java.lang.Object
122 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
123 DCHECK(result->IsObjectClass());
124 return result;
125 }
126 Class* common_elem = ClassJoin(s_ct, t_ct);
127 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
128 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800129 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700131 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
132 DCHECK(array_class != NULL);
133 return array_class;
134 } else {
135 size_t s_depth = s->Depth();
136 size_t t_depth = t->Depth();
137 // Get s and t to the same depth in the hierarchy
138 if (s_depth > t_depth) {
139 while (s_depth > t_depth) {
140 s = s->GetSuperClass();
141 s_depth--;
142 }
143 } else {
144 while (t_depth > s_depth) {
145 t = t->GetSuperClass();
146 t_depth--;
147 }
148 }
149 // Go up the hierarchy until we get to the common parent
150 while (s != t) {
151 s = s->GetSuperClass();
152 t = t->GetSuperClass();
153 }
154 return s;
155 }
156}
157
Ian Rogersb5e95b92011-10-25 23:28:55 -0700158bool RegType::IsAssignableFrom(const RegType& src) const {
159 if (Equals(src)) {
160 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700161 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700162 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700163 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
164 case RegType::kRegTypeByte: return src.IsByteTypes();
165 case RegType::kRegTypeShort: return src.IsShortTypes();
166 case RegType::kRegTypeChar: return src.IsCharTypes();
167 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
168 case RegType::kRegTypeFloat: return src.IsFloatTypes();
169 case RegType::kRegTypeLongLo: return src.IsLongTypes();
170 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700171 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700172 if (!IsReferenceTypes()) {
173 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700174 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700175 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700176 return true; // all reference types can be assigned null
177 } else if (!src.IsReferenceTypes()) {
178 return false; // expect src to be a reference type
179 } else if (IsJavaLangObject()) {
180 return true; // all reference types can be assigned to Object
181 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700182 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700183 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700184 GetClass()->IsAssignableFrom(src.GetClass())) {
185 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700186 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700188 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700189 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700190 }
191 }
192}
193
Ian Rogers84fa0742011-10-25 18:13:30 -0700194static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
195 return a.IsConstant() ? b : a;
196}
jeffhaobdb76512011-09-07 11:43:16 -0700197
Ian Rogersd81871c2011-10-03 13:57:23 -0700198const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
199 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700200 if (IsUnknown() && incoming_type.IsUnknown()) {
201 return *this; // Unknown MERGE Unknown => Unknown
202 } else if (IsConflict()) {
203 return *this; // Conflict MERGE * => Conflict
204 } else if (incoming_type.IsConflict()) {
205 return incoming_type; // * MERGE Conflict => Conflict
206 } else if (IsUnknown() || incoming_type.IsUnknown()) {
207 return reg_types->Conflict(); // Unknown MERGE * => Conflict
208 } else if(IsConstant() && incoming_type.IsConstant()) {
209 int32_t val1 = ConstantValue();
210 int32_t val2 = incoming_type.ConstantValue();
211 if (val1 >= 0 && val2 >= 0) {
212 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
213 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700214 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700215 } else {
216 return incoming_type;
217 }
218 } else if (val1 < 0 && val2 < 0) {
219 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
220 if (val1 <= val2) {
221 return *this;
222 } else {
223 return incoming_type;
224 }
225 } else {
226 // Values are +ve and -ve, choose smallest signed type in which they both fit
227 if (IsConstantByte()) {
228 if (incoming_type.IsConstantByte()) {
229 return reg_types->ByteConstant();
230 } else if (incoming_type.IsConstantShort()) {
231 return reg_types->ShortConstant();
232 } else {
233 return reg_types->IntConstant();
234 }
235 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700236 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700237 return reg_types->ShortConstant();
238 } else {
239 return reg_types->IntConstant();
240 }
241 } else {
242 return reg_types->IntConstant();
243 }
244 }
245 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
246 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
247 return reg_types->Boolean(); // boolean MERGE boolean => boolean
248 }
249 if (IsByteTypes() && incoming_type.IsByteTypes()) {
250 return reg_types->Byte(); // byte MERGE byte => byte
251 }
252 if (IsShortTypes() && incoming_type.IsShortTypes()) {
253 return reg_types->Short(); // short MERGE short => short
254 }
255 if (IsCharTypes() && incoming_type.IsCharTypes()) {
256 return reg_types->Char(); // char MERGE char => char
257 }
258 return reg_types->Integer(); // int MERGE * => int
259 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
260 (IsLongTypes() && incoming_type.IsLongTypes()) ||
261 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
262 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
263 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
264 // check constant case was handled prior to entry
265 DCHECK(!IsConstant() || !incoming_type.IsConstant());
266 // float/long/double MERGE float/long/double_constant => float/long/double
267 return SelectNonConstant(*this, incoming_type);
268 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700269 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700270 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700271 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
272 return reg_types->JavaLangObject(); // Object MERGE ref => Object
273 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
274 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
275 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
276 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700277 return reg_types->Conflict();
278 } else { // Two reference types, compute Join
279 Class* c1 = GetClass();
280 Class* c2 = incoming_type.GetClass();
281 DCHECK(c1 != NULL && !c1->IsPrimitive());
282 DCHECK(c2 != NULL && !c2->IsPrimitive());
283 Class* join_class = ClassJoin(c1, c2);
284 if (c1 == join_class) {
285 return *this;
286 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700287 return incoming_type;
288 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700289 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700290 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700291 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700292 } else {
293 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700294 }
295}
296
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700297static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700298 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700299 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
300 case Primitive::kPrimByte: return RegType::kRegTypeByte;
301 case Primitive::kPrimShort: return RegType::kRegTypeShort;
302 case Primitive::kPrimChar: return RegType::kRegTypeChar;
303 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
304 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
305 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
306 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
307 case Primitive::kPrimVoid:
308 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 }
310}
311
312static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
313 if (descriptor.length() == 1) {
314 switch (descriptor[0]) {
315 case 'Z': return RegType::kRegTypeBoolean;
316 case 'B': return RegType::kRegTypeByte;
317 case 'S': return RegType::kRegTypeShort;
318 case 'C': return RegType::kRegTypeChar;
319 case 'I': return RegType::kRegTypeInteger;
320 case 'J': return RegType::kRegTypeLongLo;
321 case 'F': return RegType::kRegTypeFloat;
322 case 'D': return RegType::kRegTypeDoubleLo;
323 case 'V':
324 default: return RegType::kRegTypeUnknown;
325 }
326 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
327 return RegType::kRegTypeReference;
328 } else {
329 return RegType::kRegTypeUnknown;
330 }
331}
332
333std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700334 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700335 return os;
336}
337
338const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800339 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700340 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
341}
342
343const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800344 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700345 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700346 // entries should be sized greater than primitive types
347 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
348 RegType* entry = entries_[type];
349 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700350 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800351 if (strlen(descriptor) != 0) {
352 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700353 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700354 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700355 entries_[type] = entry;
356 }
357 return *entry;
358 } else {
359 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800360 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700361 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700363 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364 if (cur_entry->IsReference()) {
365 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800366 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800367 return *cur_entry;
368 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700369 } else if (cur_entry->IsUnresolvedReference() &&
370 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 return *cur_entry;
372 }
373 }
Ian Rogers672297c2012-01-10 14:50:55 -0800374 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700375 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700376 // Able to resolve so create resolved register type
377 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700378 entries_.push_back(entry);
379 return *entry;
380 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700381 // TODO: we assume unresolved, but we may be able to do better by validating whether the
382 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700383 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700384 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700385 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800386 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700387 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800388 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700389 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
390 entries_.size());
391 entries_.push_back(entry);
392 return *entry;
393 } else {
394 // The descriptor is broken return the unknown type as there's nothing sensible that
395 // could be done at runtime
396 return Unknown();
397 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700398 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700399 }
400}
401
402const RegType& RegTypeCache::FromClass(Class* klass) {
403 if (klass->IsPrimitive()) {
404 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
405 // entries should be sized greater than primitive types
406 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
407 RegType* entry = entries_[type];
408 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700409 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 entries_[type] = entry;
411 }
412 return *entry;
413 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700414 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700416 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700417 return *cur_entry;
418 }
419 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700420 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 entries_.push_back(entry);
422 return *entry;
423 }
424}
425
Ian Rogers28ad40d2011-10-27 15:19:26 -0700426const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
427 RegType* entry;
428 if (type.IsUnresolvedTypes()) {
429 String* descriptor = type.GetDescriptor();
430 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
431 RegType* cur_entry = entries_[i];
432 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
433 cur_entry->GetAllocationPc() == allocation_pc &&
434 cur_entry->GetDescriptor() == descriptor) {
435 return *cur_entry;
436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700438 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
439 descriptor, allocation_pc, entries_.size());
440 } else {
441 Class* klass = type.GetClass();
442 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
443 RegType* cur_entry = entries_[i];
444 if (cur_entry->IsUninitializedReference() &&
445 cur_entry->GetAllocationPc() == allocation_pc &&
446 cur_entry->GetClass() == klass) {
447 return *cur_entry;
448 }
449 }
450 entry = new RegType(RegType::kRegTypeUninitializedReference,
451 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700453 entries_.push_back(entry);
454 return *entry;
455}
456
457const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
458 RegType* entry;
459 if (uninit_type.IsUnresolvedTypes()) {
460 String* descriptor = uninit_type.GetDescriptor();
461 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
462 RegType* cur_entry = entries_[i];
463 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
464 return *cur_entry;
465 }
466 }
467 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
468 } else {
469 Class* klass = uninit_type.GetClass();
470 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
471 RegType* cur_entry = entries_[i];
472 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
473 return *cur_entry;
474 }
475 }
476 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
477 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700478 entries_.push_back(entry);
479 return *entry;
480}
481
482const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700483 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700484 RegType* cur_entry = entries_[i];
485 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
486 return *cur_entry;
487 }
488 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700489 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 entries_.size());
491 entries_.push_back(entry);
492 return *entry;
493}
494
495const RegType& RegTypeCache::FromType(RegType::Type type) {
496 CHECK(type < RegType::kRegTypeReference);
497 switch (type) {
498 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
499 case RegType::kRegTypeByte: return From(type, NULL, "B");
500 case RegType::kRegTypeShort: return From(type, NULL, "S");
501 case RegType::kRegTypeChar: return From(type, NULL, "C");
502 case RegType::kRegTypeInteger: return From(type, NULL, "I");
503 case RegType::kRegTypeFloat: return From(type, NULL, "F");
504 case RegType::kRegTypeLongLo:
505 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
506 case RegType::kRegTypeDoubleLo:
507 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
508 default: return From(type, NULL, "");
509 }
510}
511
512const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700513 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
514 RegType* cur_entry = entries_[i];
515 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
516 return *cur_entry;
517 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700518 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700519 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
520 entries_.push_back(entry);
521 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700522}
523
Ian Rogers28ad40d2011-10-27 15:19:26 -0700524const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
525 CHECK(array.IsArrayClass());
526 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800527 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
528 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800529 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700530 } else {
531 return FromClass(array.GetClass()->GetComponentType());
532 }
533}
534
535
Ian Rogersd81871c2011-10-03 13:57:23 -0700536bool RegisterLine::CheckConstructorReturn() const {
537 for (size_t i = 0; i < num_regs_; i++) {
538 if (GetRegisterType(i).IsUninitializedThisReference()) {
539 verifier_->Fail(VERIFY_ERROR_GENERIC)
540 << "Constructor returning without calling superclass constructor";
541 return false;
542 }
543 }
544 return true;
545}
546
547void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
548 DCHECK(vdst < num_regs_);
549 if (new_type.IsLowHalf()) {
550 line_[vdst] = new_type.GetId();
551 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
552 } else if (new_type.IsHighHalf()) {
553 /* should never set these explicitly */
554 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
555 } else if (new_type.IsConflict()) { // should only be set during a merge
556 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
557 } else {
558 line_[vdst] = new_type.GetId();
559 }
560 // Clear the monitor entry bits for this register.
561 ClearAllRegToLockDepths(vdst);
562}
563
564void RegisterLine::SetResultTypeToUnknown() {
565 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
566 result_[0] = unknown_id;
567 result_[1] = unknown_id;
568}
569
570void RegisterLine::SetResultRegisterType(const RegType& new_type) {
571 result_[0] = new_type.GetId();
572 if(new_type.IsLowHalf()) {
573 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
574 result_[1] = new_type.GetId() + 1;
575 } else {
576 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
577 }
578}
579
580const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
581 // The register index was validated during the static pass, so we don't need to check it here.
582 DCHECK_LT(vsrc, num_regs_);
583 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
584}
585
586const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
587 if (dec_insn.vA_ < 1) {
588 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
589 return verifier_->GetRegTypeCache()->Unknown();
590 }
591 /* get the element type of the array held in vsrc */
592 const RegType& this_type = GetRegisterType(dec_insn.vC_);
593 if (!this_type.IsReferenceTypes()) {
594 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
595 << dec_insn.vC_ << " (type=" << this_type << ")";
596 return verifier_->GetRegTypeCache()->Unknown();
597 }
598 return this_type;
599}
600
601Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
602 /* get the element type of the array held in vsrc */
603 const RegType& type = GetRegisterType(vsrc);
604 /* if "always zero", we allow it to fail at runtime */
605 if (type.IsZero()) {
606 return NULL;
607 } else if (!type.IsReferenceTypes()) {
608 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
609 << " (type=" << type << ")";
610 return NULL;
611 } else if (type.IsUninitializedReference()) {
612 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
613 return NULL;
614 } else {
615 return type.GetClass();
616 }
617}
618
619bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
620 // Verify the src register type against the check type refining the type of the register
621 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700622 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
624 << " but expected " << check_type;
625 return false;
626 }
627 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
628 // precise than the subtype in vsrc so leave it for reference types. For primitive types
629 // if they are a defined type then they are as precise as we can get, however, for constant
630 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
631 return true;
632}
633
634void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700635 DCHECK(uninit_type.IsUninitializedTypes());
636 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
637 size_t changed = 0;
638 for (size_t i = 0; i < num_regs_; i++) {
639 if (GetRegisterType(i).Equals(uninit_type)) {
640 line_[i] = init_type.GetId();
641 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700644 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645}
646
647void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
648 for (size_t i = 0; i < num_regs_; i++) {
649 if (GetRegisterType(i).Equals(uninit_type)) {
650 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
651 ClearAllRegToLockDepths(i);
652 }
653 }
654}
655
656void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
657 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
658 const RegType& type = GetRegisterType(vsrc);
659 SetRegisterType(vdst, type);
660 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
661 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
662 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
663 << " cat=" << static_cast<int>(cat);
664 } else if (cat == kTypeCategoryRef) {
665 CopyRegToLockDepth(vdst, vsrc);
666 }
667}
668
669void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
670 const RegType& type_l = GetRegisterType(vsrc);
671 const RegType& type_h = GetRegisterType(vsrc + 1);
672
673 if (!type_l.CheckWidePair(type_h)) {
674 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
675 << " type=" << type_l << "/" << type_h;
676 } else {
677 SetRegisterType(vdst, type_l); // implicitly sets the second half
678 }
679}
680
681void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
682 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
683 if ((!is_reference && !type.IsCategory1Types()) ||
684 (is_reference && !type.IsReferenceTypes())) {
685 verifier_->Fail(VERIFY_ERROR_GENERIC)
686 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
687 } else {
688 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
689 SetRegisterType(vdst, type);
690 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
691 }
692}
693
694/*
695 * Implement "move-result-wide". Copy the category-2 value from the result
696 * register to another register, and reset the result register.
697 */
698void RegisterLine::CopyResultRegister2(uint32_t vdst) {
699 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
700 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
701 if (!type_l.IsCategory2Types()) {
702 verifier_->Fail(VERIFY_ERROR_GENERIC)
703 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
704 } else {
705 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
706 SetRegisterType(vdst, type_l); // also sets the high
707 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
708 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
709 }
710}
711
712void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
713 const RegType& dst_type, const RegType& src_type) {
714 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
715 SetRegisterType(dec_insn.vA_, dst_type);
716 }
717}
718
719void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
720 const RegType& dst_type,
721 const RegType& src_type1, const RegType& src_type2,
722 bool check_boolean_op) {
723 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
724 VerifyRegisterType(dec_insn.vC_, src_type2)) {
725 if (check_boolean_op) {
726 DCHECK(dst_type.IsInteger());
727 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
728 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
729 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
730 return;
731 }
732 }
733 SetRegisterType(dec_insn.vA_, dst_type);
734 }
735}
736
737void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
738 const RegType& dst_type, const RegType& src_type1,
739 const RegType& src_type2, bool check_boolean_op) {
740 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
741 VerifyRegisterType(dec_insn.vB_, src_type2)) {
742 if (check_boolean_op) {
743 DCHECK(dst_type.IsInteger());
744 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
745 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
746 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
747 return;
748 }
749 }
750 SetRegisterType(dec_insn.vA_, dst_type);
751 }
752}
753
754void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
755 const RegType& dst_type, const RegType& src_type,
756 bool check_boolean_op) {
757 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
758 if (check_boolean_op) {
759 DCHECK(dst_type.IsInteger());
760 /* check vB with the call, then check the constant manually */
761 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
762 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
763 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
764 return;
765 }
766 }
767 SetRegisterType(dec_insn.vA_, dst_type);
768 }
769}
770
771void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
772 const RegType& reg_type = GetRegisterType(reg_idx);
773 if (!reg_type.IsReferenceTypes()) {
774 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800775 } else if (monitors_.size() >= 32) {
776 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 } else {
778 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700779 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 }
781}
782
783void RegisterLine::PopMonitor(uint32_t reg_idx) {
784 const RegType& reg_type = GetRegisterType(reg_idx);
785 if (!reg_type.IsReferenceTypes()) {
786 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
787 } else if (monitors_.empty()) {
788 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
789 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700790 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
792 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
793 // format "036" the constant collector may create unlocks on the same object but referenced
794 // via different registers.
795 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
796 : verifier_->LogVerifyInfo())
797 << "monitor-exit not unlocking the top of the monitor stack";
798 } else {
799 // Record the register was unlocked
800 ClearRegToLockDepth(reg_idx, monitors_.size());
801 }
802 }
803}
804
805bool RegisterLine::VerifyMonitorStackEmpty() {
806 if (MonitorStackDepth() != 0) {
807 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
808 return false;
809 } else {
810 return true;
811 }
812}
813
814bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
815 bool changed = false;
816 for (size_t idx = 0; idx < num_regs_; idx++) {
817 if (line_[idx] != incoming_line->line_[idx]) {
818 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
819 const RegType& cur_type = GetRegisterType(idx);
820 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
821 changed = changed || !cur_type.Equals(new_type);
822 line_[idx] = new_type.GetId();
823 }
824 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700825 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
827 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
828 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
829 for (uint32_t idx = 0; idx < num_regs_; idx++) {
830 size_t depths = reg_to_lock_depths_.count(idx);
831 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
832 if (depths != incoming_depths) {
833 if (depths == 0 || incoming_depths == 0) {
834 reg_to_lock_depths_.erase(idx);
835 } else {
836 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
837 << ": " << depths << " != " << incoming_depths;
838 break;
839 }
840 }
841 }
842 }
843 return changed;
844}
845
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800846void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 for (size_t i = 0; i < num_regs_; i += 8) {
848 uint8_t val = 0;
849 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
850 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700851 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 val |= 1 << j;
853 }
854 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800855 if ((i / 8) >= max_bytes) {
856 DCHECK_EQ(0, val);
857 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800859 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
860 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 }
862}
863
864std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700865 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 return os;
867}
868
869
870void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
871 uint32_t insns_size, uint16_t registers_size,
872 DexVerifier* verifier) {
873 DCHECK_GT(insns_size, 0U);
874
875 for (uint32_t i = 0; i < insns_size; i++) {
876 bool interesting = false;
877 switch (mode) {
878 case kTrackRegsAll:
879 interesting = flags[i].IsOpcode();
880 break;
881 case kTrackRegsGcPoints:
882 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
883 break;
884 case kTrackRegsBranches:
885 interesting = flags[i].IsBranchTarget();
886 break;
887 default:
888 break;
889 }
890 if (interesting) {
891 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
892 }
893 }
894}
895
896bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700897 if (klass->IsVerified()) {
898 return true;
899 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800901 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
903 return false;
904 }
905 if (super != NULL) {
Ian Rogers672f5202012-01-12 18:06:40 -0800906 // Acquire lock to prevent races on verifying the super class
907 ObjectLock lock(super);
908
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 if (!super->IsVerified() && !super->IsErroneous()) {
910 Runtime::Current()->GetClassLinker()->VerifyClass(super);
911 }
912 if (!super->IsVerified()) {
913 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
914 << " that attempts to sub-class corrupt class " << PrettyClass(super);
915 return false;
916 } else if (super->IsFinal()) {
917 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
918 << " that attempts to sub-class final class " << PrettyClass(super);
919 return false;
920 }
921 }
jeffhaobdb76512011-09-07 11:43:16 -0700922 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
923 Method* method = klass->GetDirectMethod(i);
924 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
926 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700927 return false;
928 }
929 }
930 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
931 Method* method = klass->GetVirtualMethod(i);
932 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
934 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700935 return false;
936 }
937 }
938 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939}
940
jeffhaobdb76512011-09-07 11:43:16 -0700941bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 DexVerifier verifier(method);
943 bool success = verifier.Verify();
944 // We expect either success and no verification error, or failure and a generic failure to
945 // reject the class.
946 if (success) {
947 if (verifier.failure_ != VERIFY_ERROR_NONE) {
948 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
949 << verifier.fail_messages_;
950 }
951 } else {
952 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700953 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700954 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700955 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700956 verifier.Dump(std::cout);
957 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
959 }
960 return success;
961}
962
Shih-wei Liao371814f2011-10-27 16:52:10 -0700963void DexVerifier::VerifyMethodAndDump(Method* method) {
964 DexVerifier verifier(method);
965 verifier.Verify();
966
Elliott Hughese0918552011-10-28 17:18:29 -0700967 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
968 << verifier.fail_messages_.str() << std::endl
969 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700970}
971
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800972DexVerifier::DexVerifier(Method* method)
973 : work_insn_idx_(-1),
974 method_(method),
975 failure_(VERIFY_ERROR_NONE),
976 new_instance_count_(0),
977 monitor_enter_count_(0) {
978 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -0700979 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -0800980 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 dex_file_ = &class_linker->FindDexFile(dex_cache);
982 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700983}
984
Ian Rogersd81871c2011-10-03 13:57:23 -0700985bool DexVerifier::Verify() {
986 // If there aren't any instructions, make sure that's expected, then exit successfully.
987 if (code_item_ == NULL) {
988 if (!method_->IsNative() && !method_->IsAbstract()) {
989 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700990 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 } else {
992 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700993 }
jeffhaobdb76512011-09-07 11:43:16 -0700994 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
996 if (code_item_->ins_size_ > code_item_->registers_size_) {
997 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
998 << " regs=" << code_item_->registers_size_;
999 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001000 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 // Allocate and initialize an array to hold instruction data.
1002 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1003 // Run through the instructions and see if the width checks out.
1004 bool result = ComputeWidthsAndCountOps();
1005 // Flag instructions guarded by a "try" block and check exception handlers.
1006 result = result && ScanTryCatchBlocks();
1007 // Perform static instruction verification.
1008 result = result && VerifyInstructions();
1009 // Perform code flow analysis.
1010 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001011 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001012}
1013
Ian Rogersd81871c2011-10-03 13:57:23 -07001014bool DexVerifier::ComputeWidthsAndCountOps() {
1015 const uint16_t* insns = code_item_->insns_;
1016 size_t insns_size = code_item_->insns_size_in_code_units_;
1017 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001018 size_t new_instance_count = 0;
1019 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001021
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001023 Instruction::Code opcode = inst->Opcode();
1024 if (opcode == Instruction::NEW_INSTANCE) {
1025 new_instance_count++;
1026 } else if (opcode == Instruction::MONITOR_ENTER) {
1027 monitor_enter_count++;
1028 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 size_t inst_size = inst->SizeInCodeUnits();
1030 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1031 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001032 inst = inst->Next();
1033 }
1034
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 if (dex_pc != insns_size) {
1036 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1037 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001038 return false;
1039 }
1040
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 new_instance_count_ = new_instance_count;
1042 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001043 return true;
1044}
1045
Ian Rogersd81871c2011-10-03 13:57:23 -07001046bool DexVerifier::ScanTryCatchBlocks() {
1047 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001048 if (tries_size == 0) {
1049 return true;
1050 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001052 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001053
1054 for (uint32_t idx = 0; idx < tries_size; idx++) {
1055 const DexFile::TryItem* try_item = &tries[idx];
1056 uint32_t start = try_item->start_addr_;
1057 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001058 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1060 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001061 return false;
1062 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 if (!insn_flags_[start].IsOpcode()) {
1064 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001065 return false;
1066 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 for (uint32_t dex_pc = start; dex_pc < end;
1068 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1069 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001070 }
1071 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001072 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001073 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001074 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001075 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001076 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001077 CatchHandlerIterator iterator(handlers_ptr);
1078 for (; iterator.HasNext(); iterator.Next()) {
1079 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 if (!insn_flags_[dex_pc].IsOpcode()) {
1081 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001082 return false;
1083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001085 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1086 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001087 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1088 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001089 if (exception_type == NULL) {
1090 DCHECK(Thread::Current()->IsExceptionPending());
1091 Thread::Current()->ClearException();
1092 }
1093 }
jeffhaobdb76512011-09-07 11:43:16 -07001094 }
Ian Rogers0571d352011-11-03 19:51:38 -07001095 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001096 }
jeffhaobdb76512011-09-07 11:43:16 -07001097 return true;
1098}
1099
Ian Rogersd81871c2011-10-03 13:57:23 -07001100bool DexVerifier::VerifyInstructions() {
1101 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001102
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 /* Flag the start of the method as a branch target. */
1104 insn_flags_[0].SetBranchTarget();
1105
1106 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1107 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1108 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001109 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1110 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 /* Flag instructions that are garbage collection points */
1114 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1115 insn_flags_[dex_pc].SetGcPoint();
1116 }
1117 dex_pc += inst->SizeInCodeUnits();
1118 inst = inst->Next();
1119 }
1120 return true;
1121}
1122
1123bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1124 Instruction::DecodedInstruction dec_insn(inst);
1125 bool result = true;
1126 switch (inst->GetVerifyTypeArgumentA()) {
1127 case Instruction::kVerifyRegA:
1128 result = result && CheckRegisterIndex(dec_insn.vA_);
1129 break;
1130 case Instruction::kVerifyRegAWide:
1131 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1132 break;
1133 }
1134 switch (inst->GetVerifyTypeArgumentB()) {
1135 case Instruction::kVerifyRegB:
1136 result = result && CheckRegisterIndex(dec_insn.vB_);
1137 break;
1138 case Instruction::kVerifyRegBField:
1139 result = result && CheckFieldIndex(dec_insn.vB_);
1140 break;
1141 case Instruction::kVerifyRegBMethod:
1142 result = result && CheckMethodIndex(dec_insn.vB_);
1143 break;
1144 case Instruction::kVerifyRegBNewInstance:
1145 result = result && CheckNewInstance(dec_insn.vB_);
1146 break;
1147 case Instruction::kVerifyRegBString:
1148 result = result && CheckStringIndex(dec_insn.vB_);
1149 break;
1150 case Instruction::kVerifyRegBType:
1151 result = result && CheckTypeIndex(dec_insn.vB_);
1152 break;
1153 case Instruction::kVerifyRegBWide:
1154 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1155 break;
1156 }
1157 switch (inst->GetVerifyTypeArgumentC()) {
1158 case Instruction::kVerifyRegC:
1159 result = result && CheckRegisterIndex(dec_insn.vC_);
1160 break;
1161 case Instruction::kVerifyRegCField:
1162 result = result && CheckFieldIndex(dec_insn.vC_);
1163 break;
1164 case Instruction::kVerifyRegCNewArray:
1165 result = result && CheckNewArray(dec_insn.vC_);
1166 break;
1167 case Instruction::kVerifyRegCType:
1168 result = result && CheckTypeIndex(dec_insn.vC_);
1169 break;
1170 case Instruction::kVerifyRegCWide:
1171 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1172 break;
1173 }
1174 switch (inst->GetVerifyExtraFlags()) {
1175 case Instruction::kVerifyArrayData:
1176 result = result && CheckArrayData(code_offset);
1177 break;
1178 case Instruction::kVerifyBranchTarget:
1179 result = result && CheckBranchTarget(code_offset);
1180 break;
1181 case Instruction::kVerifySwitchTargets:
1182 result = result && CheckSwitchTargets(code_offset);
1183 break;
1184 case Instruction::kVerifyVarArg:
1185 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1186 break;
1187 case Instruction::kVerifyVarArgRange:
1188 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1189 break;
1190 case Instruction::kVerifyError:
1191 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1192 result = false;
1193 break;
1194 }
1195 return result;
1196}
1197
1198bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1199 if (idx >= code_item_->registers_size_) {
1200 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1201 << code_item_->registers_size_ << ")";
1202 return false;
1203 }
1204 return true;
1205}
1206
1207bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1208 if (idx + 1 >= code_item_->registers_size_) {
1209 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1210 << "+1 >= " << code_item_->registers_size_ << ")";
1211 return false;
1212 }
1213 return true;
1214}
1215
1216bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1217 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1218 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1219 << dex_file_->GetHeader().field_ids_size_ << ")";
1220 return false;
1221 }
1222 return true;
1223}
1224
1225bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1226 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1227 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1228 << dex_file_->GetHeader().method_ids_size_ << ")";
1229 return false;
1230 }
1231 return true;
1232}
1233
1234bool DexVerifier::CheckNewInstance(uint32_t idx) {
1235 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1236 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1237 << dex_file_->GetHeader().type_ids_size_ << ")";
1238 return false;
1239 }
1240 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001241 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 if (descriptor[0] != 'L') {
1243 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1244 return false;
1245 }
1246 return true;
1247}
1248
1249bool DexVerifier::CheckStringIndex(uint32_t idx) {
1250 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1251 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1252 << dex_file_->GetHeader().string_ids_size_ << ")";
1253 return false;
1254 }
1255 return true;
1256}
1257
1258bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1259 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1260 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1261 << dex_file_->GetHeader().type_ids_size_ << ")";
1262 return false;
1263 }
1264 return true;
1265}
1266
1267bool DexVerifier::CheckNewArray(uint32_t idx) {
1268 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1269 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1270 << dex_file_->GetHeader().type_ids_size_ << ")";
1271 return false;
1272 }
1273 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001274 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 const char* cp = descriptor;
1276 while (*cp++ == '[') {
1277 bracket_count++;
1278 }
1279 if (bracket_count == 0) {
1280 /* The given class must be an array type. */
1281 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1282 return false;
1283 } else if (bracket_count > 255) {
1284 /* It is illegal to create an array of more than 255 dimensions. */
1285 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1286 return false;
1287 }
1288 return true;
1289}
1290
1291bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1292 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1293 const uint16_t* insns = code_item_->insns_ + cur_offset;
1294 const uint16_t* array_data;
1295 int32_t array_data_offset;
1296
1297 DCHECK_LT(cur_offset, insn_count);
1298 /* make sure the start of the array data table is in range */
1299 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1300 if ((int32_t) cur_offset + array_data_offset < 0 ||
1301 cur_offset + array_data_offset + 2 >= insn_count) {
1302 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1303 << ", data offset " << array_data_offset << ", count " << insn_count;
1304 return false;
1305 }
1306 /* offset to array data table is a relative branch-style offset */
1307 array_data = insns + array_data_offset;
1308 /* make sure the table is 32-bit aligned */
1309 if ((((uint32_t) array_data) & 0x03) != 0) {
1310 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1311 << ", data offset " << array_data_offset;
1312 return false;
1313 }
1314 uint32_t value_width = array_data[1];
1315 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1316 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1317 /* make sure the end of the switch is in range */
1318 if (cur_offset + array_data_offset + table_size > insn_count) {
1319 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1320 << ", data offset " << array_data_offset << ", end "
1321 << cur_offset + array_data_offset + table_size
1322 << ", count " << insn_count;
1323 return false;
1324 }
1325 return true;
1326}
1327
1328bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1329 int32_t offset;
1330 bool isConditional, selfOkay;
1331 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1332 return false;
1333 }
1334 if (!selfOkay && offset == 0) {
1335 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1336 return false;
1337 }
1338 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1339 // identical "wrap-around" behavior, but it's unwise to depend on that.
1340 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1341 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1342 return false;
1343 }
1344 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1345 int32_t abs_offset = cur_offset + offset;
1346 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1347 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1348 << (void*) abs_offset << ") at " << (void*) cur_offset;
1349 return false;
1350 }
1351 insn_flags_[abs_offset].SetBranchTarget();
1352 return true;
1353}
1354
1355bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1356 bool* selfOkay) {
1357 const uint16_t* insns = code_item_->insns_ + cur_offset;
1358 *pConditional = false;
1359 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001360 switch (*insns & 0xff) {
1361 case Instruction::GOTO:
1362 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 break;
1364 case Instruction::GOTO_32:
1365 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 *selfOkay = true;
1367 break;
1368 case Instruction::GOTO_16:
1369 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 break;
1371 case Instruction::IF_EQ:
1372 case Instruction::IF_NE:
1373 case Instruction::IF_LT:
1374 case Instruction::IF_GE:
1375 case Instruction::IF_GT:
1376 case Instruction::IF_LE:
1377 case Instruction::IF_EQZ:
1378 case Instruction::IF_NEZ:
1379 case Instruction::IF_LTZ:
1380 case Instruction::IF_GEZ:
1381 case Instruction::IF_GTZ:
1382 case Instruction::IF_LEZ:
1383 *pOffset = (int16_t) insns[1];
1384 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 break;
1386 default:
1387 return false;
1388 break;
1389 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001390 return true;
1391}
1392
Ian Rogersd81871c2011-10-03 13:57:23 -07001393bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1394 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001395 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1399 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1400 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1401 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 return false;
1403 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001404 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001406 /* make sure the table is 32-bit aligned */
1407 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1409 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001410 return false;
1411 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 uint32_t switch_count = switch_insns[1];
1413 int32_t keys_offset, targets_offset;
1414 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001415 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1416 /* 0=sig, 1=count, 2/3=firstKey */
1417 targets_offset = 4;
1418 keys_offset = -1;
1419 expected_signature = Instruction::kPackedSwitchSignature;
1420 } else {
1421 /* 0=sig, 1=count, 2..count*2 = keys */
1422 keys_offset = 2;
1423 targets_offset = 2 + 2 * switch_count;
1424 expected_signature = Instruction::kSparseSwitchSignature;
1425 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001427 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001428 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1429 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 return false;
1431 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 /* make sure the end of the switch is in range */
1433 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1435 << switch_offset << ", end "
1436 << (cur_offset + switch_offset + table_size)
1437 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 return false;
1439 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001440 /* for a sparse switch, verify the keys are in ascending order */
1441 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1443 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001444 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1445 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1446 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1448 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001449 return false;
1450 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001451 last_key = key;
1452 }
1453 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001454 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 for (uint32_t targ = 0; targ < switch_count; targ++) {
1456 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1457 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1458 int32_t abs_offset = cur_offset + offset;
1459 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1460 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1461 << (void*) abs_offset << ") at "
1462 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 return false;
1464 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 insn_flags_[abs_offset].SetBranchTarget();
1466 }
1467 return true;
1468}
1469
1470bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1471 if (vA > 5) {
1472 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1473 return false;
1474 }
1475 uint16_t registers_size = code_item_->registers_size_;
1476 for (uint32_t idx = 0; idx < vA; idx++) {
1477 if (arg[idx] > registers_size) {
1478 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1479 << ") in non-range invoke (> " << registers_size << ")";
1480 return false;
1481 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 }
1483
1484 return true;
1485}
1486
Ian Rogersd81871c2011-10-03 13:57:23 -07001487bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1488 uint16_t registers_size = code_item_->registers_size_;
1489 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1490 // integer overflow when adding them here.
1491 if (vA + vC > registers_size) {
1492 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1493 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001494 return false;
1495 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 return true;
1497}
1498
Ian Rogersd81871c2011-10-03 13:57:23 -07001499bool DexVerifier::VerifyCodeFlow() {
1500 uint16_t registers_size = code_item_->registers_size_;
1501 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001502
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 if (registers_size * insns_size > 4*1024*1024) {
1504 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1505 << " insns_size=" << insns_size << ")";
1506 }
1507 /* Create and initialize table holding register status */
1508 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1509 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001510
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 work_line_.reset(new RegisterLine(registers_size, this));
1512 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 /* Initialize register types of method arguments. */
1515 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001516 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1517 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 return false;
1519 }
1520 /* Perform code flow verification. */
1521 if (!CodeFlowVerifyMethod()) {
1522 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001523 }
1524
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 /* Generate a register map and add it to the method. */
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001526 const std::vector<uint8_t>* map = GenerateGcMap();
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 if (map == NULL) {
1528 return false; // Not a real failure, but a failure to encode
1529 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001530 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1531 verifier::DexVerifier::SetGcMap(ref, *map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001532#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001533 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001534#endif
jeffhaobdb76512011-09-07 11:43:16 -07001535 return true;
1536}
1537
Ian Rogersd81871c2011-10-03 13:57:23 -07001538void DexVerifier::Dump(std::ostream& os) {
1539 if (method_->IsNative()) {
1540 os << "Native method" << std::endl;
1541 return;
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 DCHECK(code_item_ != NULL);
1544 const Instruction* inst = Instruction::At(code_item_->insns_);
1545 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1546 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001547 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1548 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1550 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001551 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001552 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 inst = inst->Next();
1554 }
jeffhaobdb76512011-09-07 11:43:16 -07001555}
1556
Ian Rogersd81871c2011-10-03 13:57:23 -07001557static bool IsPrimitiveDescriptor(char descriptor) {
1558 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001559 case 'I':
1560 case 'C':
1561 case 'S':
1562 case 'B':
1563 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001564 case 'F':
1565 case 'D':
1566 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001568 default:
1569 return false;
1570 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001571}
1572
Ian Rogersd81871c2011-10-03 13:57:23 -07001573bool DexVerifier::SetTypesFromSignature() {
1574 RegisterLine* reg_line = reg_table_.GetLine(0);
1575 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1576 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001577
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1579 //Include the "this" pointer.
1580 size_t cur_arg = 0;
1581 if (!method_->IsStatic()) {
1582 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1583 // argument as uninitialized. This restricts field access until the superclass constructor is
1584 // called.
1585 Class* declaring_class = method_->GetDeclaringClass();
1586 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1587 reg_line->SetRegisterType(arg_start + cur_arg,
1588 reg_types_.UninitializedThisArgument(declaring_class));
1589 } else {
1590 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
1594
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001595 const DexFile::ProtoId& proto_id =
1596 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001597 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001598
1599 for (; iterator.HasNext(); iterator.Next()) {
1600 const char* descriptor = iterator.GetDescriptor();
1601 if (descriptor == NULL) {
1602 LOG(FATAL) << "Null descriptor";
1603 }
1604 if (cur_arg >= expected_args) {
1605 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1606 << " args, found more (" << descriptor << ")";
1607 return false;
1608 }
1609 switch (descriptor[0]) {
1610 case 'L':
1611 case '[':
1612 // We assume that reference arguments are initialized. The only way it could be otherwise
1613 // (assuming the caller was verified) is if the current method is <init>, but in that case
1614 // it's effectively considered initialized the instant we reach here (in the sense that we
1615 // can return without doing anything or call virtual methods).
1616 {
1617 const RegType& reg_type =
1618 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001619 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 }
1621 break;
1622 case 'Z':
1623 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1624 break;
1625 case 'C':
1626 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1627 break;
1628 case 'B':
1629 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1630 break;
1631 case 'I':
1632 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1633 break;
1634 case 'S':
1635 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1636 break;
1637 case 'F':
1638 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1639 break;
1640 case 'J':
1641 case 'D': {
1642 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1643 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1644 cur_arg++;
1645 break;
1646 }
1647 default:
1648 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1649 return false;
1650 }
1651 cur_arg++;
1652 }
1653 if (cur_arg != expected_args) {
1654 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1655 return false;
1656 }
1657 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1658 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1659 // format. Only major difference from the method argument format is that 'V' is supported.
1660 bool result;
1661 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1662 result = descriptor[1] == '\0';
1663 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1664 size_t i = 0;
1665 do {
1666 i++;
1667 } while (descriptor[i] == '['); // process leading [
1668 if (descriptor[i] == 'L') { // object array
1669 do {
1670 i++; // find closing ;
1671 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1672 result = descriptor[i] == ';';
1673 } else { // primitive array
1674 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1675 }
1676 } else if (descriptor[0] == 'L') {
1677 // could be more thorough here, but shouldn't be required
1678 size_t i = 0;
1679 do {
1680 i++;
1681 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1682 result = descriptor[i] == ';';
1683 } else {
1684 result = false;
1685 }
1686 if (!result) {
1687 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1688 << descriptor << "'";
1689 }
1690 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001691}
1692
Ian Rogersd81871c2011-10-03 13:57:23 -07001693bool DexVerifier::CodeFlowVerifyMethod() {
1694 const uint16_t* insns = code_item_->insns_;
1695 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001696
jeffhaobdb76512011-09-07 11:43:16 -07001697 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 insn_flags_[0].SetChanged();
1699 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001700
jeffhaobdb76512011-09-07 11:43:16 -07001701 /* Continue until no instructions are marked "changed". */
1702 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1704 uint32_t insn_idx = start_guess;
1705 for (; insn_idx < insns_size; insn_idx++) {
1706 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001707 break;
1708 }
jeffhaobdb76512011-09-07 11:43:16 -07001709 if (insn_idx == insns_size) {
1710 if (start_guess != 0) {
1711 /* try again, starting from the top */
1712 start_guess = 0;
1713 continue;
1714 } else {
1715 /* all flags are clear */
1716 break;
1717 }
1718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 // We carry the working set of registers from instruction to instruction. If this address can
1720 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1721 // "changed" flags, we need to load the set of registers from the table.
1722 // Because we always prefer to continue on to the next instruction, we should never have a
1723 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1724 // target.
1725 work_insn_idx_ = insn_idx;
1726 if (insn_flags_[insn_idx].IsBranchTarget()) {
1727 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001728 } else {
1729#ifndef NDEBUG
1730 /*
1731 * Sanity check: retrieve the stored register line (assuming
1732 * a full table) and make sure it actually matches.
1733 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1735 if (register_line != NULL) {
1736 if (work_line_->CompareLine(register_line) != 0) {
1737 Dump(std::cout);
1738 std::cout << info_messages_.str();
1739 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1740 << "@" << (void*)work_insn_idx_ << std::endl
1741 << " work_line=" << *work_line_ << std::endl
1742 << " expected=" << *register_line;
1743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 }
1745#endif
1746 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 if (!CodeFlowVerifyInstruction(&start_guess)) {
1748 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001749 return false;
1750 }
jeffhaobdb76512011-09-07 11:43:16 -07001751 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 insn_flags_[insn_idx].SetVisited();
1753 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001754 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001755
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001757 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001758 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001759 * (besides the wasted space), but it indicates a flaw somewhere
1760 * down the line, possibly in the verifier.
1761 *
1762 * If we've substituted "always throw" instructions into the stream,
1763 * we are almost certainly going to have some dead code.
1764 */
1765 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 uint32_t insn_idx = 0;
1767 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001768 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001769 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001770 * may or may not be preceded by a padding NOP (for alignment).
1771 */
1772 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1773 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1774 insns[insn_idx] == Instruction::kArrayDataSignature ||
1775 (insns[insn_idx] == Instruction::NOP &&
1776 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1777 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1778 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001780 }
1781
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001783 if (dead_start < 0)
1784 dead_start = insn_idx;
1785 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001787 dead_start = -1;
1788 }
1789 }
1790 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001792 }
1793 }
jeffhaobdb76512011-09-07 11:43:16 -07001794 return true;
1795}
1796
Ian Rogersd81871c2011-10-03 13:57:23 -07001797bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001798#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001800 gDvm.verifierStats.instrsReexamined++;
1801 } else {
1802 gDvm.verifierStats.instrsExamined++;
1803 }
1804#endif
1805
1806 /*
1807 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001808 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001809 * control to another statement:
1810 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001811 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001812 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001813 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001814 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001815 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001816 * throw an exception that is handled by an encompassing "try"
1817 * block.
1818 *
1819 * We can also return, in which case there is no successor instruction
1820 * from this point.
1821 *
1822 * The behavior can be determined from the OpcodeFlags.
1823 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1825 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001826 Instruction::DecodedInstruction dec_insn(inst);
1827 int opcode_flag = inst->Flag();
1828
jeffhaobdb76512011-09-07 11:43:16 -07001829 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001830 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001831 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001833 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1834 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 }
jeffhaobdb76512011-09-07 11:43:16 -07001836
1837 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001838 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001839 * can throw an exception, we will copy/merge this into the "catch"
1840 * address rather than work_line, because we don't want the result
1841 * from the "successful" code path (e.g. a check-cast that "improves"
1842 * a type) to be visible to the exception handler.
1843 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1845 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001846 } else {
1847#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001849#endif
1850 }
1851
1852 switch (dec_insn.opcode_) {
1853 case Instruction::NOP:
1854 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001855 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001856 * a signature that looks like a NOP; if we see one of these in
1857 * the course of executing code then we have a problem.
1858 */
1859 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001861 }
1862 break;
1863
1864 case Instruction::MOVE:
1865 case Instruction::MOVE_FROM16:
1866 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001868 break;
1869 case Instruction::MOVE_WIDE:
1870 case Instruction::MOVE_WIDE_FROM16:
1871 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874 case Instruction::MOVE_OBJECT:
1875 case Instruction::MOVE_OBJECT_FROM16:
1876 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001878 break;
1879
1880 /*
1881 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001882 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001883 * might want to hold the result in an actual CPU register, so the
1884 * Dalvik spec requires that these only appear immediately after an
1885 * invoke or filled-new-array.
1886 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001887 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001888 * redundant with the reset done below, but it can make the debug info
1889 * easier to read in some cases.)
1890 */
1891 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
1897 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001902 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 * This statement can only appear as the first instruction in an exception handler (though not
1904 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001905 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001906 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001907 const RegType& res_type = GetCaughtExceptionType();
1908 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001909 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 }
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1913 if (!GetMethodReturnType().IsUnknown()) {
1914 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1915 }
jeffhaobdb76512011-09-07 11:43:16 -07001916 }
1917 break;
1918 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001920 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 const RegType& return_type = GetMethodReturnType();
1922 if (!return_type.IsCategory1Types()) {
1923 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1924 } else {
1925 // Compilers may generate synthetic functions that write byte values into boolean fields.
1926 // Also, it may use integer values for boolean, byte, short, and character return types.
1927 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1928 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1929 ((return_type.IsBoolean() || return_type.IsByte() ||
1930 return_type.IsShort() || return_type.IsChar()) &&
1931 src_type.IsInteger()));
1932 /* check the register contents */
1933 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1934 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001935 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 }
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938 }
1939 break;
1940 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001942 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 const RegType& return_type = GetMethodReturnType();
1944 if (!return_type.IsCategory2Types()) {
1945 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1946 } else {
1947 /* check the register contents */
1948 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1949 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001950 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 }
jeffhaobdb76512011-09-07 11:43:16 -07001952 }
1953 }
1954 break;
1955 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1957 const RegType& return_type = GetMethodReturnType();
1958 if (!return_type.IsReferenceTypes()) {
1959 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1960 } else {
1961 /* return_type is the *expected* return type, not register value */
1962 DCHECK(!return_type.IsZero());
1963 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001964 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1965 // Disallow returning uninitialized values and verify that the reference in vAA is an
1966 // instance of the "return_type"
1967 if (reg_type.IsUninitializedTypes()) {
1968 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1969 } else if (!return_type.IsAssignableFrom(reg_type)) {
1970 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1971 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001972 }
1973 }
1974 }
1975 break;
1976
1977 case Instruction::CONST_4:
1978 case Instruction::CONST_16:
1979 case Instruction::CONST:
1980 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
1983 case Instruction::CONST_HIGH16:
1984 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 work_line_->SetRegisterType(dec_insn.vA_,
1986 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
1988 case Instruction::CONST_WIDE_16:
1989 case Instruction::CONST_WIDE_32:
1990 case Instruction::CONST_WIDE:
1991 case Instruction::CONST_WIDE_HIGH16:
1992 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995 case Instruction::CONST_STRING:
1996 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002000 // Get type from instruction if unresolved then we need an access check
2001 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2002 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2003 // Register holds class, ie its type is class, but on error we keep it Unknown
2004 work_line_->SetRegisterType(dec_insn.vA_,
2005 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 }
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011 case Instruction::MONITOR_EXIT:
2012 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002013 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002014 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002015 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002016 * to the need to handle asynchronous exceptions, a now-deprecated
2017 * feature that Dalvik doesn't support.)
2018 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002019 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002020 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002021 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002022 * structured locking checks are working, the former would have
2023 * failed on the -enter instruction, and the latter is impossible.
2024 *
2025 * This is fortunate, because issue 3221411 prevents us from
2026 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002028 * some catch blocks (which will show up as "dead" code when
2029 * we skip them here); if we can't, then the code path could be
2030 * "live" so we still need to check it.
2031 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 opcode_flag &= ~Instruction::kThrow;
2033 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035
Ian Rogers28ad40d2011-10-27 15:19:26 -07002036 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002038 /*
2039 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2040 * could be a "upcast" -- not expected, so we don't try to address it.)
2041 *
2042 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2043 * dec_insn.vA_ when branching to a handler.
2044 */
2045 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2046 const RegType& res_type =
2047 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002048 if (res_type.IsUnknown()) {
2049 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2050 break; // couldn't resolve class
2051 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002052 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2053 const RegType& orig_type =
2054 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2055 if (!res_type.IsNonZeroReferenceTypes()) {
2056 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2057 } else if (!orig_type.IsReferenceTypes()) {
2058 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002059 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002060 if (is_checkcast) {
2061 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002064 }
jeffhaobdb76512011-09-07 11:43:16 -07002065 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002066 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 }
2068 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002069 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2070 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002071 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002072 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 } else {
2074 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2075 }
2076 }
2077 break;
2078 }
2079 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002080 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2081 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2082 // can't create an instance of an interface or abstract class */
2083 if (!res_type.IsInstantiableTypes()) {
2084 Fail(VERIFY_ERROR_INSTANTIATION)
2085 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002087 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2088 // Any registers holding previous allocations from this address that have not yet been
2089 // initialized must be marked invalid.
2090 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2091 // add the new uninitialized reference to the register state
2092 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 }
2094 break;
2095 }
2096 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002097 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2098 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2099 if (!res_type.IsArrayClass()) {
2100 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002101 } else {
2102 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002104 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002105 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002106 }
2107 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 }
jeffhaobdb76512011-09-07 11:43:16 -07002109 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002111 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2112 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2113 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002115 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002116 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002117 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002118 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002119 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002120 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002121 just_set_result = true;
2122 }
2123 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 }
jeffhaobdb76512011-09-07 11:43:16 -07002125 case Instruction::CMPL_FLOAT:
2126 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2128 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2129 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::CMPL_DOUBLE:
2132 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2134 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2135 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2139 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2140 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002143 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2144 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2145 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002146 }
2147 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 }
jeffhaobdb76512011-09-07 11:43:16 -07002149 case Instruction::GOTO:
2150 case Instruction::GOTO_16:
2151 case Instruction::GOTO_32:
2152 /* no effect on or use of registers */
2153 break;
2154
2155 case Instruction::PACKED_SWITCH:
2156 case Instruction::SPARSE_SWITCH:
2157 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 case Instruction::FILL_ARRAY_DATA: {
2162 /* Similar to the verification done for APUT */
2163 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2164 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002165 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 if (res_class != NULL) {
2167 Class* component_type = res_class->GetComponentType();
2168 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2169 component_type->IsPrimitiveVoid()) {
2170 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002171 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 } else {
2173 const RegType& value_type = reg_types_.FromClass(component_type);
2174 DCHECK(!value_type.IsUnknown());
2175 // Now verify if the element width in the table matches the element width declared in
2176 // the array
2177 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2178 if (array_data[0] != Instruction::kArrayDataSignature) {
2179 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2180 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002181 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 // Since we don't compress the data in Dex, expect to see equal width of data stored
2183 // in the table and expected from the array class.
2184 if (array_data[1] != elem_width) {
2185 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2186 << " vs " << elem_width << ")";
2187 }
2188 }
2189 }
jeffhaobdb76512011-09-07 11:43:16 -07002190 }
2191 }
2192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 case Instruction::IF_NE: {
2196 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2197 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2198 bool mismatch = false;
2199 if (reg_type1.IsZero()) { // zero then integral or reference expected
2200 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2201 } else if (reg_type1.IsReferenceTypes()) { // both references?
2202 mismatch = !reg_type2.IsReferenceTypes();
2203 } else { // both integral?
2204 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2205 }
2206 if (mismatch) {
2207 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2208 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002209 }
2210 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 }
jeffhaobdb76512011-09-07 11:43:16 -07002212 case Instruction::IF_LT:
2213 case Instruction::IF_GE:
2214 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 case Instruction::IF_LE: {
2216 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2217 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2218 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2219 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2220 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002221 }
2222 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 }
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 case Instruction::IF_NEZ: {
2226 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2227 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2228 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2229 }
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 }
jeffhaobdb76512011-09-07 11:43:16 -07002232 case Instruction::IF_LTZ:
2233 case Instruction::IF_GEZ:
2234 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 case Instruction::IF_LEZ: {
2236 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2237 if (!reg_type.IsIntegralTypes()) {
2238 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2239 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 }
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2245 break;
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 VerifyAGet(dec_insn, reg_types_.Char(), true);
2251 break;
jeffhaobdb76512011-09-07 11:43:16 -07002252 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 case Instruction::AGET:
2256 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2257 break;
jeffhaobdb76512011-09-07 11:43:16 -07002258 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 VerifyAGet(dec_insn, reg_types_.Long(), true);
2260 break;
2261 case Instruction::AGET_OBJECT:
2262 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 case Instruction::APUT_BOOLEAN:
2266 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2267 break;
2268 case Instruction::APUT_BYTE:
2269 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2270 break;
2271 case Instruction::APUT_CHAR:
2272 VerifyAPut(dec_insn, reg_types_.Char(), true);
2273 break;
2274 case Instruction::APUT_SHORT:
2275 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002288 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
2299 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002300 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002303 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002306 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
jeffhaobdb76512011-09-07 11:43:16 -07002308
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002310 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
2312 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
2315 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002322 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002325 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
jeffhaobdb76512011-09-07 11:43:16 -07002327 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002328 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002332 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002335 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002338 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002341 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002344 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002347 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002350 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 break;
2352
2353 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002354 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 break;
2356 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002357 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 break;
2359 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002360 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 break;
2362 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002363 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002366 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002369 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002372 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374
2375 case Instruction::INVOKE_VIRTUAL:
2376 case Instruction::INVOKE_VIRTUAL_RANGE:
2377 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 case Instruction::INVOKE_SUPER_RANGE: {
2379 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2380 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2381 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2382 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2383 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2384 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002385 const char* descriptor;
2386 if (called_method == NULL) {
2387 uint32_t method_idx = dec_insn.vB_;
2388 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2389 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002390 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002391 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002392 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002393 }
Ian Rogers9074b992011-10-26 17:41:55 -07002394 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002395 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002397 just_set_result = true;
2398 }
2399 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 }
jeffhaobdb76512011-09-07 11:43:16 -07002401 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 case Instruction::INVOKE_DIRECT_RANGE: {
2403 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2404 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2405 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002406 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 * Some additional checks when calling a constructor. We know from the invocation arg check
2408 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2409 * that to require that called_method->klass is the same as this->klass or this->super,
2410 * allowing the latter only if the "this" argument is the same as the "this" argument to
2411 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002412 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002413 bool is_constructor;
2414 if (called_method != NULL) {
2415 is_constructor = called_method->IsConstructor();
2416 } else {
2417 uint32_t method_idx = dec_insn.vB_;
2418 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2419 const char* name = dex_file_->GetMethodName(method_id);
2420 is_constructor = strcmp(name, "<init>") == 0;
2421 }
2422 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2424 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426
2427 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 if (this_type.IsZero()) {
2429 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002432 if (called_method != NULL) {
2433 Class* this_class = this_type.GetClass();
2434 DCHECK(this_class != NULL);
2435 /* must be in same class or in superclass */
2436 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2437 if (this_class != method_->GetDeclaringClass()) {
2438 Fail(VERIFY_ERROR_GENERIC)
2439 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2440 break;
2441 }
2442 } else if (called_method->GetDeclaringClass() != this_class) {
2443 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 }
jeffhaobdb76512011-09-07 11:43:16 -07002446 }
2447
2448 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002449 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2451 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 }
2454
2455 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002456 * Replace the uninitialized reference with an initialized one. We need to do this for all
2457 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002458 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 work_line_->MarkRefsAsInitialized(this_type);
2460 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002462 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 const char* descriptor;
2464 if (called_method == NULL) {
2465 uint32_t method_idx = dec_insn.vB_;
2466 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2467 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002468 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002469 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002470 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 }
Ian Rogers9074b992011-10-26 17:41:55 -07002472 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002473 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002475 just_set_result = true;
2476 }
2477 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 }
jeffhaobdb76512011-09-07 11:43:16 -07002479 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 case Instruction::INVOKE_STATIC_RANGE: {
2481 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2482 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2483 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 const char* descriptor;
2485 if (called_method == NULL) {
2486 uint32_t method_idx = dec_insn.vB_;
2487 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2488 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002489 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002490 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002491 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002492 }
Ian Rogers9074b992011-10-26 17:41:55 -07002493 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002494 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->SetResultRegisterType(return_type);
2496 just_set_result = true;
2497 }
jeffhaobdb76512011-09-07 11:43:16 -07002498 }
2499 break;
2500 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 case Instruction::INVOKE_INTERFACE_RANGE: {
2502 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2503 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2504 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002505 if (abs_method != NULL) {
2506 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002507 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002508 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2509 << PrettyMethod(abs_method) << "'";
2510 break;
2511 }
2512 }
2513 /* Get the type of the "this" arg, which should either be a sub-interface of called
2514 * interface or Object (see comments in RegType::JoinClass).
2515 */
2516 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2517 if (failure_ == VERIFY_ERROR_NONE) {
2518 if (this_type.IsZero()) {
2519 /* null pointer always passes (and always fails at runtime) */
2520 } else {
2521 if (this_type.IsUninitializedTypes()) {
2522 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2523 << this_type;
2524 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002526 // In the past we have tried to assert that "called_interface" is assignable
2527 // from "this_type.GetClass()", however, as we do an imprecise Join
2528 // (RegType::JoinClass) we don't have full information on what interfaces are
2529 // implemented by "this_type". For example, two classes may implement the same
2530 // interfaces and have a common parent that doesn't implement the interface. The
2531 // join will set "this_type" to the parent class and a test that this implements
2532 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002533 }
2534 }
jeffhaobdb76512011-09-07 11:43:16 -07002535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 * We don't have an object instance, so we can't find the concrete method. However, all of
2537 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002538 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002539 const char* descriptor;
2540 if (abs_method == NULL) {
2541 uint32_t method_idx = dec_insn.vB_;
2542 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2543 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002544 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002545 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002546 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002547 }
Ian Rogers9074b992011-10-26 17:41:55 -07002548 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002549 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2550 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002552 just_set_result = true;
2553 }
2554 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 }
jeffhaobdb76512011-09-07 11:43:16 -07002556 case Instruction::NEG_INT:
2557 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::NEG_LONG:
2561 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615
2616 case Instruction::ADD_INT:
2617 case Instruction::SUB_INT:
2618 case Instruction::MUL_INT:
2619 case Instruction::REM_INT:
2620 case Instruction::DIV_INT:
2621 case Instruction::SHL_INT:
2622 case Instruction::SHR_INT:
2623 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::AND_INT:
2627 case Instruction::OR_INT:
2628 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::ADD_LONG:
2632 case Instruction::SUB_LONG:
2633 case Instruction::MUL_LONG:
2634 case Instruction::DIV_LONG:
2635 case Instruction::REM_LONG:
2636 case Instruction::AND_LONG:
2637 case Instruction::OR_LONG:
2638 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::SHL_LONG:
2642 case Instruction::SHR_LONG:
2643 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 /* shift distance is Int, making these different from other binary operations */
2645 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::ADD_FLOAT:
2648 case Instruction::SUB_FLOAT:
2649 case Instruction::MUL_FLOAT:
2650 case Instruction::DIV_FLOAT:
2651 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::ADD_DOUBLE:
2655 case Instruction::SUB_DOUBLE:
2656 case Instruction::MUL_DOUBLE:
2657 case Instruction::DIV_DOUBLE:
2658 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::ADD_INT_2ADDR:
2662 case Instruction::SUB_INT_2ADDR:
2663 case Instruction::MUL_INT_2ADDR:
2664 case Instruction::REM_INT_2ADDR:
2665 case Instruction::SHL_INT_2ADDR:
2666 case Instruction::SHR_INT_2ADDR:
2667 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::AND_INT_2ADDR:
2671 case Instruction::OR_INT_2ADDR:
2672 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::ADD_LONG_2ADDR:
2679 case Instruction::SUB_LONG_2ADDR:
2680 case Instruction::MUL_LONG_2ADDR:
2681 case Instruction::DIV_LONG_2ADDR:
2682 case Instruction::REM_LONG_2ADDR:
2683 case Instruction::AND_LONG_2ADDR:
2684 case Instruction::OR_LONG_2ADDR:
2685 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
2688 case Instruction::SHL_LONG_2ADDR:
2689 case Instruction::SHR_LONG_2ADDR:
2690 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
2693 case Instruction::ADD_FLOAT_2ADDR:
2694 case Instruction::SUB_FLOAT_2ADDR:
2695 case Instruction::MUL_FLOAT_2ADDR:
2696 case Instruction::DIV_FLOAT_2ADDR:
2697 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::ADD_DOUBLE_2ADDR:
2701 case Instruction::SUB_DOUBLE_2ADDR:
2702 case Instruction::MUL_DOUBLE_2ADDR:
2703 case Instruction::DIV_DOUBLE_2ADDR:
2704 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::ADD_INT_LIT16:
2708 case Instruction::RSUB_INT:
2709 case Instruction::MUL_INT_LIT16:
2710 case Instruction::DIV_INT_LIT16:
2711 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002713 break;
2714 case Instruction::AND_INT_LIT16:
2715 case Instruction::OR_INT_LIT16:
2716 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719 case Instruction::ADD_INT_LIT8:
2720 case Instruction::RSUB_INT_LIT8:
2721 case Instruction::MUL_INT_LIT8:
2722 case Instruction::DIV_INT_LIT8:
2723 case Instruction::REM_INT_LIT8:
2724 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002725 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::AND_INT_LIT8:
2730 case Instruction::OR_INT_LIT8:
2731 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002733 break;
2734
2735 /*
2736 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002737 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002738 * inserted in the course of verification, we can expect to see it here.
2739 */
jeffhaob4df5142011-09-19 20:25:32 -07002740 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::UNUSED_EE:
2745 case Instruction::UNUSED_EF:
2746 case Instruction::UNUSED_F2:
2747 case Instruction::UNUSED_F3:
2748 case Instruction::UNUSED_F4:
2749 case Instruction::UNUSED_F5:
2750 case Instruction::UNUSED_F6:
2751 case Instruction::UNUSED_F7:
2752 case Instruction::UNUSED_F8:
2753 case Instruction::UNUSED_F9:
2754 case Instruction::UNUSED_FA:
2755 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002756 case Instruction::UNUSED_F0:
2757 case Instruction::UNUSED_F1:
2758 case Instruction::UNUSED_E3:
2759 case Instruction::UNUSED_E8:
2760 case Instruction::UNUSED_E7:
2761 case Instruction::UNUSED_E4:
2762 case Instruction::UNUSED_E9:
2763 case Instruction::UNUSED_FC:
2764 case Instruction::UNUSED_E5:
2765 case Instruction::UNUSED_EA:
2766 case Instruction::UNUSED_FD:
2767 case Instruction::UNUSED_E6:
2768 case Instruction::UNUSED_EB:
2769 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::UNUSED_3E:
2771 case Instruction::UNUSED_3F:
2772 case Instruction::UNUSED_40:
2773 case Instruction::UNUSED_41:
2774 case Instruction::UNUSED_42:
2775 case Instruction::UNUSED_43:
2776 case Instruction::UNUSED_73:
2777 case Instruction::UNUSED_79:
2778 case Instruction::UNUSED_7A:
2779 case Instruction::UNUSED_EC:
2780 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002781 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783
2784 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002785 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002786 * complain if an instruction is missing (which is desirable).
2787 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002789
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 if (failure_ != VERIFY_ERROR_NONE) {
2791 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002792 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002793 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002794 return false;
2795 } else {
2796 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002797 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002799 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002801 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002803 opcode_flag = Instruction::kThrow;
2804 }
2805 }
jeffhaobdb76512011-09-07 11:43:16 -07002806 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 * If we didn't just set the result register, clear it out. This ensures that you can only use
2808 * "move-result" immediately after the result is set. (We could check this statically, but it's
2809 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002810 */
2811 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002813 }
2814
jeffhaoa0a764a2011-09-16 10:43:38 -07002815 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002816 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2818 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2819 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002820 return false;
2821 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2823 // next instruction isn't one.
2824 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002825 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 }
2827 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2828 if (next_line != NULL) {
2829 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2830 // needed.
2831 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002832 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 }
jeffhaobdb76512011-09-07 11:43:16 -07002834 } else {
2835 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 * We're not recording register data for the next instruction, so we don't know what the prior
2837 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002838 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002840 }
2841 }
2842
2843 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002844 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002845 *
2846 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002847 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002848 * somebody could get a reference field, check it for zero, and if the
2849 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002850 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002851 * that, and will reject the code.
2852 *
2853 * TODO: avoid re-fetching the branch target
2854 */
2855 if ((opcode_flag & Instruction::kBranch) != 0) {
2856 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002858 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002860 return false;
2861 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002862 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002864 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 }
jeffhaobdb76512011-09-07 11:43:16 -07002866 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002868 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 }
jeffhaobdb76512011-09-07 11:43:16 -07002870 }
2871
2872 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002873 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002874 *
2875 * We've already verified that the table is structurally sound, so we
2876 * just need to walk through and tag the targets.
2877 */
2878 if ((opcode_flag & Instruction::kSwitch) != 0) {
2879 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2880 const uint16_t* switch_insns = insns + offset_to_switch;
2881 int switch_count = switch_insns[1];
2882 int offset_to_targets, targ;
2883
2884 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2885 /* 0 = sig, 1 = count, 2/3 = first key */
2886 offset_to_targets = 4;
2887 } else {
2888 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002889 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002890 offset_to_targets = 2 + 2 * switch_count;
2891 }
2892
2893 /* verify each switch target */
2894 for (targ = 0; targ < switch_count; targ++) {
2895 int offset;
2896 uint32_t abs_offset;
2897
2898 /* offsets are 32-bit, and only partly endian-swapped */
2899 offset = switch_insns[offset_to_targets + targ * 2] |
2900 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 abs_offset = work_insn_idx_ + offset;
2902 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2903 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002904 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
2906 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002907 return false;
2908 }
2909 }
2910
2911 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2913 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002914 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2916 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002917 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002918
Ian Rogers0571d352011-11-03 19:51:38 -07002919 for (; iterator.HasNext(); iterator.Next()) {
2920 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 within_catch_all = true;
2922 }
jeffhaobdb76512011-09-07 11:43:16 -07002923 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2925 * "work_regs", because at runtime the exception will be thrown before the instruction
2926 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002927 */
Ian Rogers0571d352011-11-03 19:51:38 -07002928 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002929 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 }
jeffhaobdb76512011-09-07 11:43:16 -07002931 }
2932
2933 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2935 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002936 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002938 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002939 * The state in work_line reflects the post-execution state. If the current instruction is a
2940 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002941 * it will do so before grabbing the lock).
2942 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2944 Fail(VERIFY_ERROR_GENERIC)
2945 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002946 return false;
2947 }
2948 }
2949 }
2950
jeffhaod1f0fde2011-09-08 17:25:33 -07002951 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 if ((opcode_flag & Instruction::kReturn) != 0) {
2953 if(!work_line_->VerifyMonitorStackEmpty()) {
2954 return false;
2955 }
jeffhaobdb76512011-09-07 11:43:16 -07002956 }
2957
2958 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002959 * Update start_guess. Advance to the next instruction of that's
2960 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002961 * neither of those exists we're in a return or throw; leave start_guess
2962 * alone and let the caller sort it out.
2963 */
2964 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002966 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2967 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002969 }
2970
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2972 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002973
2974 return true;
2975}
2976
Ian Rogers28ad40d2011-10-27 15:19:26 -07002977const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002978 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002979 Class* referrer = method_->GetDeclaringClass();
2980 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2981 const RegType& result =
2982 klass != NULL ? reg_types_.FromClass(klass)
2983 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2984 if (klass == NULL && !result.IsUnresolvedTypes()) {
2985 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002987 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2988 // check at runtime if access is allowed and so pass here.
2989 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2990 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002991 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002992 << result << "'";
2993 return reg_types_.Unknown();
2994 } else {
2995 return result;
2996 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002997}
2998
Ian Rogers28ad40d2011-10-27 15:19:26 -07002999const RegType& DexVerifier::GetCaughtExceptionType() {
3000 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003002 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3004 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003005 CatchHandlerIterator iterator(handlers_ptr);
3006 for (; iterator.HasNext(); iterator.Next()) {
3007 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3008 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003009 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003011 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3013 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3014 * test, so is essentially harmless.
3015 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003016 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3017 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3018 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003020 common_super = &exception;
3021 } else if (common_super->Equals(exception)) {
3022 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003024 common_super = &common_super->Merge(exception, &reg_types_);
3025 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 }
3027 }
3028 }
3029 }
Ian Rogers0571d352011-11-03 19:51:38 -07003030 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 }
3032 }
3033 if (common_super == NULL) {
3034 /* no catch blocks, or no catches with classes we can find */
3035 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3036 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003037 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003038}
3039
3040Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003041 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3042 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3043 if (failure_ != VERIFY_ERROR_NONE) {
3044 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3045 return NULL;
3046 }
3047 if(klass_type.IsUnresolvedTypes()) {
3048 return NULL; // Can't resolve Class so no more to do here
3049 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 Class* referrer = method_->GetDeclaringClass();
3051 DexCache* dex_cache = referrer->GetDexCache();
3052 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3053 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003054 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003055 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003056 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 if (is_direct) {
3058 res_method = klass->FindDirectMethod(name, signature);
3059 } else if (klass->IsInterface()) {
3060 res_method = klass->FindInterfaceMethod(name, signature);
3061 } else {
3062 res_method = klass->FindVirtualMethod(name, signature);
3063 }
3064 if (res_method != NULL) {
3065 dex_cache->SetResolvedMethod(method_idx, res_method);
3066 } else {
3067 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003068 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 << " " << signature;
3070 return NULL;
3071 }
3072 }
3073 /* Check if access is allowed. */
3074 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3075 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003076 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 return NULL;
3078 }
3079 return res_method;
3080}
3081
3082Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3083 MethodType method_type, bool is_range, bool is_super) {
3084 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3085 // we're making.
3086 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3087 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003088 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 return NULL;
3090 }
3091 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3092 // enforce them here.
3093 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3094 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3095 << PrettyMethod(res_method);
3096 return NULL;
3097 }
3098 // See if the method type implied by the invoke instruction matches the access flags for the
3099 // target method.
3100 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3101 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3102 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3103 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003104 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3105 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 return NULL;
3107 }
3108 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3109 // has a vtable entry for the target method.
3110 if (is_super) {
3111 DCHECK(method_type == METHOD_VIRTUAL);
3112 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3113 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3114 if (super == NULL) { // Only Object has no super class
3115 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3116 << " to super " << PrettyMethod(res_method);
3117 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003118 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003120 << " to super " << PrettyDescriptor(super)
3121 << "." << mh.GetName()
3122 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
3124 return NULL;
3125 }
3126 }
3127 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3128 // match the call to the signature. Also, we might might be calling through an abstract method
3129 // definition (which doesn't have register count values).
3130 int expected_args = dec_insn.vA_;
3131 /* caught by static verifier */
3132 DCHECK(is_range || expected_args <= 5);
3133 if (expected_args > code_item_->outs_size_) {
3134 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3135 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3136 return NULL;
3137 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003138 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 if (sig[0] != '(') {
3140 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3141 << " as descriptor doesn't start with '(': " << sig;
3142 return NULL;
3143 }
jeffhaobdb76512011-09-07 11:43:16 -07003144 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 * Check the "this" argument, which must be an instance of the class
3146 * that declared the method. For an interface class, we don't do the
3147 * full interface merge, so we can't do a rigorous check here (which
3148 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003149 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 int actual_args = 0;
3151 if (!res_method->IsStatic()) {
3152 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3153 if (failure_ != VERIFY_ERROR_NONE) {
3154 return NULL;
3155 }
3156 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3157 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3158 return NULL;
3159 }
3160 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003161 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3162 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3163 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3164 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 return NULL;
3166 }
3167 }
3168 actual_args++;
3169 }
3170 /*
3171 * Process the target method's signature. This signature may or may not
3172 * have been verified, so we can't assume it's properly formed.
3173 */
3174 size_t sig_offset = 0;
3175 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3176 if (actual_args >= expected_args) {
3177 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3178 << "'. Expected " << expected_args << " args, found more ("
3179 << sig.substr(sig_offset) << ")";
3180 return NULL;
3181 }
3182 std::string descriptor;
3183 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3184 size_t end;
3185 if (sig[sig_offset] == 'L') {
3186 end = sig.find(';', sig_offset);
3187 } else {
3188 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3189 if (sig[end] == 'L') {
3190 end = sig.find(';', end);
3191 }
3192 }
3193 if (end == std::string::npos) {
3194 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3195 << "bad signature component '" << sig << "' (missing ';')";
3196 return NULL;
3197 }
3198 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3199 sig_offset = end;
3200 } else {
3201 descriptor = sig[sig_offset];
3202 }
3203 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003204 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3205 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003206 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3207 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3208 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 }
3210 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3211 }
3212 if (sig[sig_offset] != ')') {
3213 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3214 return NULL;
3215 }
3216 if (actual_args != expected_args) {
3217 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3218 << " expected " << expected_args << " args, found " << actual_args;
3219 return NULL;
3220 } else {
3221 return res_method;
3222 }
3223}
3224
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003225const RegType& DexVerifier::GetMethodReturnType() {
3226 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3227 MethodHelper(method_).GetReturnTypeDescriptor());
3228}
3229
Ian Rogersd81871c2011-10-03 13:57:23 -07003230void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3231 const RegType& insn_type, bool is_primitive) {
3232 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3233 if (!index_type.IsArrayIndexTypes()) {
3234 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3235 } else {
3236 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3237 if (failure_ == VERIFY_ERROR_NONE) {
3238 if (array_class == NULL) {
3239 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3240 // instruction type. TODO: have a proper notion of bottom here.
3241 if (!is_primitive || insn_type.IsCategory1Types()) {
3242 // Reference or category 1
3243 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3244 } else {
3245 // Category 2
3246 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3247 }
3248 } else {
3249 /* verify the class */
3250 Class* component_class = array_class->GetComponentType();
3251 const RegType& component_type = reg_types_.FromClass(component_class);
3252 if (!array_class->IsArrayClass()) {
3253 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003254 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 } else if (component_class->IsPrimitive() && !is_primitive) {
3256 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003257 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 << " source for aget-object";
3259 } else if (!component_class->IsPrimitive() && is_primitive) {
3260 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003261 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 << " source for category 1 aget";
3263 } else if (is_primitive && !insn_type.Equals(component_type) &&
3264 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3265 (insn_type.IsLong() && component_type.IsDouble()))) {
3266 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003267 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 << " incompatible with aget of type " << insn_type;
3269 } else {
3270 // Use knowledge of the field type which is stronger than the type inferred from the
3271 // instruction, which can't differentiate object types and ints from floats, longs from
3272 // doubles.
3273 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3274 }
3275 }
3276 }
3277 }
3278}
3279
3280void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3281 const RegType& insn_type, bool is_primitive) {
3282 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3283 if (!index_type.IsArrayIndexTypes()) {
3284 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3285 } else {
3286 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3287 if (failure_ == VERIFY_ERROR_NONE) {
3288 if (array_class == NULL) {
3289 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3290 // instruction type.
3291 } else {
3292 /* verify the class */
3293 Class* component_class = array_class->GetComponentType();
3294 const RegType& component_type = reg_types_.FromClass(component_class);
3295 if (!array_class->IsArrayClass()) {
3296 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003297 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 } else if (component_class->IsPrimitive() && !is_primitive) {
3299 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003300 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 << " source for aput-object";
3302 } else if (!component_class->IsPrimitive() && is_primitive) {
3303 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003304 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 << " source for category 1 aput";
3306 } else if (is_primitive && !insn_type.Equals(component_type) &&
3307 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3308 (insn_type.IsLong() && component_type.IsDouble()))) {
3309 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003310 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 << " incompatible with aput of type " << insn_type;
3312 } else {
3313 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003314 // Note: we use the instruction type (rather than the component type) for aput-object as
3315 // incompatible classes will be caught at runtime as an array store exception
3316 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 }
3318 }
3319 }
3320 }
3321}
3322
3323Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003324 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3325 // Check access to class
3326 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3327 if (failure_ != VERIFY_ERROR_NONE) {
3328 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3329 << dex_file_->GetFieldName(field_id) << ") in "
3330 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3331 return NULL;
3332 }
3333 if(klass_type.IsUnresolvedTypes()) {
3334 return NULL; // Can't resolve Class so no more to do here
3335 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003336 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003338 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3339 << dex_file_->GetFieldName(field_id) << ") in "
3340 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 DCHECK(Thread::Current()->IsExceptionPending());
3342 Thread::Current()->ClearException();
3343 return NULL;
3344 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3345 field->GetAccessFlags())) {
3346 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3347 << " from " << PrettyClass(method_->GetDeclaringClass());
3348 return NULL;
3349 } else if (!field->IsStatic()) {
3350 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3351 return NULL;
3352 } else {
3353 return field;
3354 }
3355}
3356
Ian Rogersd81871c2011-10-03 13:57:23 -07003357Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003358 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3359 // Check access to class
3360 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3361 if (failure_ != VERIFY_ERROR_NONE) {
3362 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3363 << dex_file_->GetFieldName(field_id) << ") in "
3364 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3365 return NULL;
3366 }
3367 if(klass_type.IsUnresolvedTypes()) {
3368 return NULL; // Can't resolve Class so no more to do here
3369 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003370 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003372 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3373 << dex_file_->GetFieldName(field_id) << ") in "
3374 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 DCHECK(Thread::Current()->IsExceptionPending());
3376 Thread::Current()->ClearException();
3377 return NULL;
3378 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3379 field->GetAccessFlags())) {
3380 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3381 << " from " << PrettyClass(method_->GetDeclaringClass());
3382 return NULL;
3383 } else if (field->IsStatic()) {
3384 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3385 << " to not be static";
3386 return NULL;
3387 } else if (obj_type.IsZero()) {
3388 // Cannot infer and check type, however, access will cause null pointer exception
3389 return field;
3390 } else if(obj_type.IsUninitializedReference() &&
3391 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3392 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3393 // Field accesses through uninitialized references are only allowable for constructors where
3394 // the field is declared in this class
3395 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3396 << " of a not fully initialized object within the context of "
3397 << PrettyMethod(method_);
3398 return NULL;
3399 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3400 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3401 // of C1. For resolution to occur the declared class of the field must be compatible with
3402 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3403 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3404 << " from object of type " << PrettyClass(obj_type.GetClass());
3405 return NULL;
3406 } else {
3407 return field;
3408 }
3409}
3410
Ian Rogersb94a27b2011-10-26 00:33:41 -07003411void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3412 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003413 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003414 Field* field;
3415 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003416 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003417 } else {
3418 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003419 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003420 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003421 if (failure_ != VERIFY_ERROR_NONE) {
3422 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3423 } else {
3424 const char* descriptor;
3425 const ClassLoader* loader;
3426 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003427 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003428 loader = field->GetDeclaringClass()->GetClassLoader();
3429 } else {
3430 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3431 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3432 loader = method_->GetDeclaringClass()->GetClassLoader();
3433 }
3434 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003436 if (field_type.Equals(insn_type) ||
3437 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3438 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 // expected that read is of the correct primitive type or that int reads are reading
3440 // floats or long reads are reading doubles
3441 } else {
3442 // This is a global failure rather than a class change failure as the instructions and
3443 // the descriptors for the type should have been consistent within the same file at
3444 // compile time
3445 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003446 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003447 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 return;
3449 }
3450 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003451 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003453 << " to be compatible with type '" << insn_type
3454 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003455 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 return;
3457 }
3458 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003459 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 }
3461}
3462
Ian Rogersb94a27b2011-10-26 00:33:41 -07003463void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3464 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003465 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003466 Field* field;
3467 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003468 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003469 } else {
3470 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003471 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003472 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003473 if (failure_ != VERIFY_ERROR_NONE) {
3474 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3475 } else {
3476 const char* descriptor;
3477 const ClassLoader* loader;
3478 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003479 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003480 loader = field->GetDeclaringClass()->GetClassLoader();
3481 } else {
3482 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3483 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3484 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003486 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3487 if (field != NULL) {
3488 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3489 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3490 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3491 return;
3492 }
3493 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003494 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003495 // Primitive field assignability rules are weaker than regular assignability rules
3496 bool instruction_compatible;
3497 bool value_compatible;
3498 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3499 if (field_type.IsIntegralTypes()) {
3500 instruction_compatible = insn_type.IsIntegralTypes();
3501 value_compatible = value_type.IsIntegralTypes();
3502 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003503 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003504 value_compatible = value_type.IsFloatTypes();
3505 } else if (field_type.IsLong()) {
3506 instruction_compatible = insn_type.IsLong();
3507 value_compatible = value_type.IsLongTypes();
3508 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003509 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003510 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003512 instruction_compatible = false; // reference field with primitive store
3513 value_compatible = false; // unused
3514 }
3515 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003516 // This is a global failure rather than a class change failure as the instructions and
3517 // the descriptors for the type should have been consistent within the same file at
3518 // compile time
3519 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003520 << " to be of type '" << insn_type
3521 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003522 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003523 return;
3524 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003525 if (!value_compatible) {
3526 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3527 << " of type " << value_type
3528 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003529 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003530 return;
3531 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003533 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003534 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003535 << " to be compatible with type '" << insn_type
3536 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003537 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 return;
3539 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003540 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 }
3543}
3544
3545bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3546 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3547 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3548 return false;
3549 }
3550 return true;
3551}
3552
3553void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003554 const RegType& res_type, bool is_range) {
3555 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 /*
3557 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3558 * list and fail. It's legal, if silly, for arg_count to be zero.
3559 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003560 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3561 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 uint32_t arg_count = dec_insn.vA_;
3563 for (size_t ui = 0; ui < arg_count; ui++) {
3564 uint32_t get_reg;
3565
3566 if (is_range)
3567 get_reg = dec_insn.vC_ + ui;
3568 else
3569 get_reg = dec_insn.arg_[ui];
3570
3571 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3572 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3573 << ") not valid";
3574 return;
3575 }
3576 }
3577}
3578
3579void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003580 if (Runtime::Current()->IsStarted()) {
3581 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3582 << " " << fail_messages_.str();
3583 return;
3584 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3586 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3587 VerifyErrorRefType ref_type;
3588 switch (inst->Opcode()) {
3589 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003590 case Instruction::CHECK_CAST:
3591 case Instruction::INSTANCE_OF:
3592 case Instruction::NEW_INSTANCE:
3593 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003595 case Instruction::FILLED_NEW_ARRAY_RANGE:
3596 ref_type = VERIFY_ERROR_REF_CLASS;
3597 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003599 case Instruction::IGET_BOOLEAN:
3600 case Instruction::IGET_BYTE:
3601 case Instruction::IGET_CHAR:
3602 case Instruction::IGET_SHORT:
3603 case Instruction::IGET_WIDE:
3604 case Instruction::IGET_OBJECT:
3605 case Instruction::IPUT:
3606 case Instruction::IPUT_BOOLEAN:
3607 case Instruction::IPUT_BYTE:
3608 case Instruction::IPUT_CHAR:
3609 case Instruction::IPUT_SHORT:
3610 case Instruction::IPUT_WIDE:
3611 case Instruction::IPUT_OBJECT:
3612 case Instruction::SGET:
3613 case Instruction::SGET_BOOLEAN:
3614 case Instruction::SGET_BYTE:
3615 case Instruction::SGET_CHAR:
3616 case Instruction::SGET_SHORT:
3617 case Instruction::SGET_WIDE:
3618 case Instruction::SGET_OBJECT:
3619 case Instruction::SPUT:
3620 case Instruction::SPUT_BOOLEAN:
3621 case Instruction::SPUT_BYTE:
3622 case Instruction::SPUT_CHAR:
3623 case Instruction::SPUT_SHORT:
3624 case Instruction::SPUT_WIDE:
3625 case Instruction::SPUT_OBJECT:
3626 ref_type = VERIFY_ERROR_REF_FIELD;
3627 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003629 case Instruction::INVOKE_VIRTUAL_RANGE:
3630 case Instruction::INVOKE_SUPER:
3631 case Instruction::INVOKE_SUPER_RANGE:
3632 case Instruction::INVOKE_DIRECT:
3633 case Instruction::INVOKE_DIRECT_RANGE:
3634 case Instruction::INVOKE_STATIC:
3635 case Instruction::INVOKE_STATIC_RANGE:
3636 case Instruction::INVOKE_INTERFACE:
3637 case Instruction::INVOKE_INTERFACE_RANGE:
3638 ref_type = VERIFY_ERROR_REF_METHOD;
3639 break;
jeffhaobdb76512011-09-07 11:43:16 -07003640 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003641 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003642 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003643 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3645 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3646 // instruction, so assert it.
3647 size_t width = inst->SizeInCodeUnits();
3648 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003649 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 // NOPs
3651 for (size_t i = 2; i < width; i++) {
3652 insns[work_insn_idx_ + i] = Instruction::NOP;
3653 }
3654 // Encode the opcode, with the failure code in the high byte
3655 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3656 (failure_ << 8) | // AA - component
3657 (ref_type << (8 + kVerifyErrorRefTypeShift));
3658 insns[work_insn_idx_] = new_instruction;
3659 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3660 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003661 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3662 << fail_messages_.str();
3663 if (gDebugVerify) {
3664 std::cout << std::endl << info_messages_.str();
3665 Dump(std::cout);
3666 }
jeffhaobdb76512011-09-07 11:43:16 -07003667}
jeffhaoba5ebb92011-08-25 17:24:37 -07003668
Ian Rogersd81871c2011-10-03 13:57:23 -07003669bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3670 const bool merge_debug = true;
3671 bool changed = true;
3672 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3673 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003674 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 * We haven't processed this instruction before, and we haven't touched the registers here, so
3676 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3677 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003678 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003680 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3682 copy->CopyFromLine(target_line);
3683 changed = target_line->MergeRegisters(merge_line);
3684 if (failure_ != VERIFY_ERROR_NONE) {
3685 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003686 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003687 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003688 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3689 << *copy.get() << " MERGE" << std::endl
3690 << *merge_line << " ==" << std::endl
3691 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003692 }
3693 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 if (changed) {
3695 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003696 }
3697 return true;
3698}
3699
Ian Rogersd81871c2011-10-03 13:57:23 -07003700void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3701 size_t* log2_max_gc_pc) {
3702 size_t local_gc_points = 0;
3703 size_t max_insn = 0;
3704 size_t max_ref_reg = -1;
3705 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3706 if (insn_flags_[i].IsGcPoint()) {
3707 local_gc_points++;
3708 max_insn = i;
3709 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003710 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003711 }
3712 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003713 *gc_points = local_gc_points;
3714 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3715 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003716 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003717 i++;
3718 }
3719 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003720}
3721
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003722const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003723 size_t num_entries, ref_bitmap_bits, pc_bits;
3724 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3725 // There's a single byte to encode the size of each bitmap
3726 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3727 // TODO: either a better GC map format or per method failures
3728 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3729 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003730 return NULL;
3731 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3733 // There are 2 bytes to encode the number of entries
3734 if (num_entries >= 65536) {
3735 // TODO: either a better GC map format or per method failures
3736 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3737 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003738 return NULL;
3739 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003741 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003742 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003743 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003744 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003745 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003746 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003747 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003748 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003749 // TODO: either a better GC map format or per method failures
3750 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3751 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3752 return NULL;
3753 }
3754 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003755 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 if (table == NULL) {
3757 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3758 return NULL;
3759 }
3760 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003761 table->push_back(format);
3762 table->push_back(ref_bitmap_bytes);
3763 table->push_back(num_entries & 0xFF);
3764 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003765 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3767 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003768 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003770 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003771 }
3772 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003773 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 }
3775 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003776 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 return table;
3778}
jeffhaoa0a764a2011-09-16 10:43:38 -07003779
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003780void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3782 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003783 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 size_t map_index = 0;
3785 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3786 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3787 if (insn_flags_[i].IsGcPoint()) {
3788 CHECK_LT(map_index, map.NumEntries());
3789 CHECK_EQ(map.GetPC(map_index), i);
3790 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3791 map_index++;
3792 RegisterLine* line = reg_table_.GetLine(i);
3793 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003794 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 CHECK_LT(j / 8, map.RegWidth());
3796 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3797 } else if ((j / 8) < map.RegWidth()) {
3798 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3799 } else {
3800 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3801 }
3802 }
3803 } else {
3804 CHECK(reg_bitmap == NULL);
3805 }
3806 }
3807}
jeffhaoa0a764a2011-09-16 10:43:38 -07003808
Ian Rogersd81871c2011-10-03 13:57:23 -07003809const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3810 size_t num_entries = NumEntries();
3811 // Do linear or binary search?
3812 static const size_t kSearchThreshold = 8;
3813 if (num_entries < kSearchThreshold) {
3814 for (size_t i = 0; i < num_entries; i++) {
3815 if (GetPC(i) == dex_pc) {
3816 return GetBitMap(i);
3817 }
3818 }
3819 } else {
3820 int lo = 0;
3821 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003822 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 int mid = (hi + lo) / 2;
3824 int mid_pc = GetPC(mid);
3825 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003826 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003827 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003828 hi = mid - 1;
3829 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003831 }
3832 }
3833 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 if (error_if_not_present) {
3835 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3836 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003837 return NULL;
3838}
3839
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003840DexVerifier::GcMapTable DexVerifier::gc_maps_;
3841
3842void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
3843 CHECK(GetGcMap(ref) == NULL);
3844 gc_maps_[ref] = &gc_map;
3845 CHECK(GetGcMap(ref) != NULL);
3846}
3847
3848const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3849 GcMapTable::const_iterator it = gc_maps_.find(ref);
3850 if (it == gc_maps_.end()) {
3851 return NULL;
3852 }
3853 CHECK(it->second != NULL);
3854 return it->second;
3855}
3856
3857void DexVerifier::DeleteGcMaps() {
3858 STLDeleteValues(&gc_maps_);
3859}
3860
Ian Rogersd81871c2011-10-03 13:57:23 -07003861} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003862} // namespace art