blob: 00b226dee9ae4427dede036c9cbbd6759cfda725 [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);
979 Runtime* runtime = Runtime::Current();
980 // We should only be running the verifier at compile time
981 CHECK(!runtime->IsStarted());
jeffhaobdb76512011-09-07 11:43:16 -0700982 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800983 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 dex_file_ = &class_linker->FindDexFile(dex_cache);
985 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700986}
987
Ian Rogersd81871c2011-10-03 13:57:23 -0700988bool DexVerifier::Verify() {
989 // If there aren't any instructions, make sure that's expected, then exit successfully.
990 if (code_item_ == NULL) {
991 if (!method_->IsNative() && !method_->IsAbstract()) {
992 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700993 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 } else {
995 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
999 if (code_item_->ins_size_ > code_item_->registers_size_) {
1000 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1001 << " regs=" << code_item_->registers_size_;
1002 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001003 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 // Allocate and initialize an array to hold instruction data.
1005 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1006 // Run through the instructions and see if the width checks out.
1007 bool result = ComputeWidthsAndCountOps();
1008 // Flag instructions guarded by a "try" block and check exception handlers.
1009 result = result && ScanTryCatchBlocks();
1010 // Perform static instruction verification.
1011 result = result && VerifyInstructions();
1012 // Perform code flow analysis.
1013 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001014 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001015}
1016
Ian Rogersd81871c2011-10-03 13:57:23 -07001017bool DexVerifier::ComputeWidthsAndCountOps() {
1018 const uint16_t* insns = code_item_->insns_;
1019 size_t insns_size = code_item_->insns_size_in_code_units_;
1020 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001021 size_t new_instance_count = 0;
1022 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001024
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001026 Instruction::Code opcode = inst->Opcode();
1027 if (opcode == Instruction::NEW_INSTANCE) {
1028 new_instance_count++;
1029 } else if (opcode == Instruction::MONITOR_ENTER) {
1030 monitor_enter_count++;
1031 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 size_t inst_size = inst->SizeInCodeUnits();
1033 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1034 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001035 inst = inst->Next();
1036 }
1037
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 if (dex_pc != insns_size) {
1039 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1040 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001041 return false;
1042 }
1043
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 new_instance_count_ = new_instance_count;
1045 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001046 return true;
1047}
1048
Ian Rogersd81871c2011-10-03 13:57:23 -07001049bool DexVerifier::ScanTryCatchBlocks() {
1050 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001051 if (tries_size == 0) {
1052 return true;
1053 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001055 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001056
1057 for (uint32_t idx = 0; idx < tries_size; idx++) {
1058 const DexFile::TryItem* try_item = &tries[idx];
1059 uint32_t start = try_item->start_addr_;
1060 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001061 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1063 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001064 return false;
1065 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 if (!insn_flags_[start].IsOpcode()) {
1067 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001068 return false;
1069 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 for (uint32_t dex_pc = start; dex_pc < end;
1071 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1072 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001073 }
1074 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001075 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001076 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001077 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001078 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001079 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001080 CatchHandlerIterator iterator(handlers_ptr);
1081 for (; iterator.HasNext(); iterator.Next()) {
1082 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 if (!insn_flags_[dex_pc].IsOpcode()) {
1084 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001085 return false;
1086 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001088 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1089 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001090 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1091 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001092 if (exception_type == NULL) {
1093 DCHECK(Thread::Current()->IsExceptionPending());
1094 Thread::Current()->ClearException();
1095 }
1096 }
jeffhaobdb76512011-09-07 11:43:16 -07001097 }
Ian Rogers0571d352011-11-03 19:51:38 -07001098 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001099 }
jeffhaobdb76512011-09-07 11:43:16 -07001100 return true;
1101}
1102
Ian Rogersd81871c2011-10-03 13:57:23 -07001103bool DexVerifier::VerifyInstructions() {
1104 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001105
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 /* Flag the start of the method as a branch target. */
1107 insn_flags_[0].SetBranchTarget();
1108
1109 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1110 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1111 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001112 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1113 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 return false;
1115 }
1116 /* Flag instructions that are garbage collection points */
1117 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1118 insn_flags_[dex_pc].SetGcPoint();
1119 }
1120 dex_pc += inst->SizeInCodeUnits();
1121 inst = inst->Next();
1122 }
1123 return true;
1124}
1125
1126bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1127 Instruction::DecodedInstruction dec_insn(inst);
1128 bool result = true;
1129 switch (inst->GetVerifyTypeArgumentA()) {
1130 case Instruction::kVerifyRegA:
1131 result = result && CheckRegisterIndex(dec_insn.vA_);
1132 break;
1133 case Instruction::kVerifyRegAWide:
1134 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1135 break;
1136 }
1137 switch (inst->GetVerifyTypeArgumentB()) {
1138 case Instruction::kVerifyRegB:
1139 result = result && CheckRegisterIndex(dec_insn.vB_);
1140 break;
1141 case Instruction::kVerifyRegBField:
1142 result = result && CheckFieldIndex(dec_insn.vB_);
1143 break;
1144 case Instruction::kVerifyRegBMethod:
1145 result = result && CheckMethodIndex(dec_insn.vB_);
1146 break;
1147 case Instruction::kVerifyRegBNewInstance:
1148 result = result && CheckNewInstance(dec_insn.vB_);
1149 break;
1150 case Instruction::kVerifyRegBString:
1151 result = result && CheckStringIndex(dec_insn.vB_);
1152 break;
1153 case Instruction::kVerifyRegBType:
1154 result = result && CheckTypeIndex(dec_insn.vB_);
1155 break;
1156 case Instruction::kVerifyRegBWide:
1157 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1158 break;
1159 }
1160 switch (inst->GetVerifyTypeArgumentC()) {
1161 case Instruction::kVerifyRegC:
1162 result = result && CheckRegisterIndex(dec_insn.vC_);
1163 break;
1164 case Instruction::kVerifyRegCField:
1165 result = result && CheckFieldIndex(dec_insn.vC_);
1166 break;
1167 case Instruction::kVerifyRegCNewArray:
1168 result = result && CheckNewArray(dec_insn.vC_);
1169 break;
1170 case Instruction::kVerifyRegCType:
1171 result = result && CheckTypeIndex(dec_insn.vC_);
1172 break;
1173 case Instruction::kVerifyRegCWide:
1174 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1175 break;
1176 }
1177 switch (inst->GetVerifyExtraFlags()) {
1178 case Instruction::kVerifyArrayData:
1179 result = result && CheckArrayData(code_offset);
1180 break;
1181 case Instruction::kVerifyBranchTarget:
1182 result = result && CheckBranchTarget(code_offset);
1183 break;
1184 case Instruction::kVerifySwitchTargets:
1185 result = result && CheckSwitchTargets(code_offset);
1186 break;
1187 case Instruction::kVerifyVarArg:
1188 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1189 break;
1190 case Instruction::kVerifyVarArgRange:
1191 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1192 break;
1193 case Instruction::kVerifyError:
1194 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1195 result = false;
1196 break;
1197 }
1198 return result;
1199}
1200
1201bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1202 if (idx >= code_item_->registers_size_) {
1203 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1204 << code_item_->registers_size_ << ")";
1205 return false;
1206 }
1207 return true;
1208}
1209
1210bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1211 if (idx + 1 >= code_item_->registers_size_) {
1212 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1213 << "+1 >= " << code_item_->registers_size_ << ")";
1214 return false;
1215 }
1216 return true;
1217}
1218
1219bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1220 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1221 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1222 << dex_file_->GetHeader().field_ids_size_ << ")";
1223 return false;
1224 }
1225 return true;
1226}
1227
1228bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1229 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1230 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1231 << dex_file_->GetHeader().method_ids_size_ << ")";
1232 return false;
1233 }
1234 return true;
1235}
1236
1237bool DexVerifier::CheckNewInstance(uint32_t idx) {
1238 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1239 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1240 << dex_file_->GetHeader().type_ids_size_ << ")";
1241 return false;
1242 }
1243 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001244 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (descriptor[0] != 'L') {
1246 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1247 return false;
1248 }
1249 return true;
1250}
1251
1252bool DexVerifier::CheckStringIndex(uint32_t idx) {
1253 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1254 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1255 << dex_file_->GetHeader().string_ids_size_ << ")";
1256 return false;
1257 }
1258 return true;
1259}
1260
1261bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1262 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1263 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1264 << dex_file_->GetHeader().type_ids_size_ << ")";
1265 return false;
1266 }
1267 return true;
1268}
1269
1270bool DexVerifier::CheckNewArray(uint32_t idx) {
1271 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1272 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1273 << dex_file_->GetHeader().type_ids_size_ << ")";
1274 return false;
1275 }
1276 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001277 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 const char* cp = descriptor;
1279 while (*cp++ == '[') {
1280 bracket_count++;
1281 }
1282 if (bracket_count == 0) {
1283 /* The given class must be an array type. */
1284 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1285 return false;
1286 } else if (bracket_count > 255) {
1287 /* It is illegal to create an array of more than 255 dimensions. */
1288 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1289 return false;
1290 }
1291 return true;
1292}
1293
1294bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1295 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1296 const uint16_t* insns = code_item_->insns_ + cur_offset;
1297 const uint16_t* array_data;
1298 int32_t array_data_offset;
1299
1300 DCHECK_LT(cur_offset, insn_count);
1301 /* make sure the start of the array data table is in range */
1302 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1303 if ((int32_t) cur_offset + array_data_offset < 0 ||
1304 cur_offset + array_data_offset + 2 >= insn_count) {
1305 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1306 << ", data offset " << array_data_offset << ", count " << insn_count;
1307 return false;
1308 }
1309 /* offset to array data table is a relative branch-style offset */
1310 array_data = insns + array_data_offset;
1311 /* make sure the table is 32-bit aligned */
1312 if ((((uint32_t) array_data) & 0x03) != 0) {
1313 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1314 << ", data offset " << array_data_offset;
1315 return false;
1316 }
1317 uint32_t value_width = array_data[1];
1318 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1319 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1320 /* make sure the end of the switch is in range */
1321 if (cur_offset + array_data_offset + table_size > insn_count) {
1322 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1323 << ", data offset " << array_data_offset << ", end "
1324 << cur_offset + array_data_offset + table_size
1325 << ", count " << insn_count;
1326 return false;
1327 }
1328 return true;
1329}
1330
1331bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1332 int32_t offset;
1333 bool isConditional, selfOkay;
1334 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1335 return false;
1336 }
1337 if (!selfOkay && offset == 0) {
1338 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1339 return false;
1340 }
1341 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1342 // identical "wrap-around" behavior, but it's unwise to depend on that.
1343 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1344 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1345 return false;
1346 }
1347 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1348 int32_t abs_offset = cur_offset + offset;
1349 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1350 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1351 << (void*) abs_offset << ") at " << (void*) cur_offset;
1352 return false;
1353 }
1354 insn_flags_[abs_offset].SetBranchTarget();
1355 return true;
1356}
1357
1358bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1359 bool* selfOkay) {
1360 const uint16_t* insns = code_item_->insns_ + cur_offset;
1361 *pConditional = false;
1362 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 switch (*insns & 0xff) {
1364 case Instruction::GOTO:
1365 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 break;
1367 case Instruction::GOTO_32:
1368 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001369 *selfOkay = true;
1370 break;
1371 case Instruction::GOTO_16:
1372 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 break;
1374 case Instruction::IF_EQ:
1375 case Instruction::IF_NE:
1376 case Instruction::IF_LT:
1377 case Instruction::IF_GE:
1378 case Instruction::IF_GT:
1379 case Instruction::IF_LE:
1380 case Instruction::IF_EQZ:
1381 case Instruction::IF_NEZ:
1382 case Instruction::IF_LTZ:
1383 case Instruction::IF_GEZ:
1384 case Instruction::IF_GTZ:
1385 case Instruction::IF_LEZ:
1386 *pOffset = (int16_t) insns[1];
1387 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001388 break;
1389 default:
1390 return false;
1391 break;
1392 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 return true;
1394}
1395
Ian Rogersd81871c2011-10-03 13:57:23 -07001396bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1397 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001398 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1402 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1403 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1404 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001405 return false;
1406 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001407 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 /* make sure the table is 32-bit aligned */
1410 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1412 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001413 return false;
1414 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 uint32_t switch_count = switch_insns[1];
1416 int32_t keys_offset, targets_offset;
1417 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001418 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1419 /* 0=sig, 1=count, 2/3=firstKey */
1420 targets_offset = 4;
1421 keys_offset = -1;
1422 expected_signature = Instruction::kPackedSwitchSignature;
1423 } else {
1424 /* 0=sig, 1=count, 2..count*2 = keys */
1425 keys_offset = 2;
1426 targets_offset = 2 + 2 * switch_count;
1427 expected_signature = Instruction::kSparseSwitchSignature;
1428 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001431 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1432 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 return false;
1434 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 /* make sure the end of the switch is in range */
1436 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1438 << switch_offset << ", end "
1439 << (cur_offset + switch_offset + table_size)
1440 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001441 return false;
1442 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 /* for a sparse switch, verify the keys are in ascending order */
1444 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1446 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001447 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1448 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1449 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1451 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 return false;
1453 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001454 last_key = key;
1455 }
1456 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 for (uint32_t targ = 0; targ < switch_count; targ++) {
1459 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1460 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1461 int32_t abs_offset = cur_offset + offset;
1462 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1463 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1464 << (void*) abs_offset << ") at "
1465 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001466 return false;
1467 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 insn_flags_[abs_offset].SetBranchTarget();
1469 }
1470 return true;
1471}
1472
1473bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1474 if (vA > 5) {
1475 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1476 return false;
1477 }
1478 uint16_t registers_size = code_item_->registers_size_;
1479 for (uint32_t idx = 0; idx < vA; idx++) {
1480 if (arg[idx] > registers_size) {
1481 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1482 << ") in non-range invoke (> " << registers_size << ")";
1483 return false;
1484 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 }
1486
1487 return true;
1488}
1489
Ian Rogersd81871c2011-10-03 13:57:23 -07001490bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1491 uint16_t registers_size = code_item_->registers_size_;
1492 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1493 // integer overflow when adding them here.
1494 if (vA + vC > registers_size) {
1495 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1496 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001497 return false;
1498 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 return true;
1500}
1501
Ian Rogersd81871c2011-10-03 13:57:23 -07001502bool DexVerifier::VerifyCodeFlow() {
1503 uint16_t registers_size = code_item_->registers_size_;
1504 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001505
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 if (registers_size * insns_size > 4*1024*1024) {
1507 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1508 << " insns_size=" << insns_size << ")";
1509 }
1510 /* Create and initialize table holding register status */
1511 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1512 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 work_line_.reset(new RegisterLine(registers_size, this));
1515 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001516
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 /* Initialize register types of method arguments. */
1518 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001519 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1520 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 return false;
1522 }
1523 /* Perform code flow verification. */
1524 if (!CodeFlowVerifyMethod()) {
1525 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001526 }
1527
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 /* Generate a register map and add it to the method. */
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001529 const std::vector<uint8_t>* map = GenerateGcMap();
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 if (map == NULL) {
1531 return false; // Not a real failure, but a failure to encode
1532 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001533 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1534 verifier::DexVerifier::SetGcMap(ref, *map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001535#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001536 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001537#endif
jeffhaobdb76512011-09-07 11:43:16 -07001538 return true;
1539}
1540
Ian Rogersd81871c2011-10-03 13:57:23 -07001541void DexVerifier::Dump(std::ostream& os) {
1542 if (method_->IsNative()) {
1543 os << "Native method" << std::endl;
1544 return;
jeffhaobdb76512011-09-07 11:43:16 -07001545 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 DCHECK(code_item_ != NULL);
1547 const Instruction* inst = Instruction::At(code_item_->insns_);
1548 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1549 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001550 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1551 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1553 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001554 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001556 inst = inst->Next();
1557 }
jeffhaobdb76512011-09-07 11:43:16 -07001558}
1559
Ian Rogersd81871c2011-10-03 13:57:23 -07001560static bool IsPrimitiveDescriptor(char descriptor) {
1561 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001562 case 'I':
1563 case 'C':
1564 case 'S':
1565 case 'B':
1566 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001567 case 'F':
1568 case 'D':
1569 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001571 default:
1572 return false;
1573 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001574}
1575
Ian Rogersd81871c2011-10-03 13:57:23 -07001576bool DexVerifier::SetTypesFromSignature() {
1577 RegisterLine* reg_line = reg_table_.GetLine(0);
1578 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1579 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001580
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1582 //Include the "this" pointer.
1583 size_t cur_arg = 0;
1584 if (!method_->IsStatic()) {
1585 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1586 // argument as uninitialized. This restricts field access until the superclass constructor is
1587 // called.
1588 Class* declaring_class = method_->GetDeclaringClass();
1589 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1590 reg_line->SetRegisterType(arg_start + cur_arg,
1591 reg_types_.UninitializedThisArgument(declaring_class));
1592 } else {
1593 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001594 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001596 }
1597
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001598 const DexFile::ProtoId& proto_id =
1599 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001600 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001601
1602 for (; iterator.HasNext(); iterator.Next()) {
1603 const char* descriptor = iterator.GetDescriptor();
1604 if (descriptor == NULL) {
1605 LOG(FATAL) << "Null descriptor";
1606 }
1607 if (cur_arg >= expected_args) {
1608 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1609 << " args, found more (" << descriptor << ")";
1610 return false;
1611 }
1612 switch (descriptor[0]) {
1613 case 'L':
1614 case '[':
1615 // We assume that reference arguments are initialized. The only way it could be otherwise
1616 // (assuming the caller was verified) is if the current method is <init>, but in that case
1617 // it's effectively considered initialized the instant we reach here (in the sense that we
1618 // can return without doing anything or call virtual methods).
1619 {
1620 const RegType& reg_type =
1621 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001622 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 }
1624 break;
1625 case 'Z':
1626 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1627 break;
1628 case 'C':
1629 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1630 break;
1631 case 'B':
1632 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1633 break;
1634 case 'I':
1635 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1636 break;
1637 case 'S':
1638 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1639 break;
1640 case 'F':
1641 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1642 break;
1643 case 'J':
1644 case 'D': {
1645 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1646 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1647 cur_arg++;
1648 break;
1649 }
1650 default:
1651 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1652 return false;
1653 }
1654 cur_arg++;
1655 }
1656 if (cur_arg != expected_args) {
1657 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1658 return false;
1659 }
1660 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1661 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1662 // format. Only major difference from the method argument format is that 'V' is supported.
1663 bool result;
1664 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1665 result = descriptor[1] == '\0';
1666 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1667 size_t i = 0;
1668 do {
1669 i++;
1670 } while (descriptor[i] == '['); // process leading [
1671 if (descriptor[i] == 'L') { // object array
1672 do {
1673 i++; // find closing ;
1674 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1675 result = descriptor[i] == ';';
1676 } else { // primitive array
1677 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1678 }
1679 } else if (descriptor[0] == 'L') {
1680 // could be more thorough here, but shouldn't be required
1681 size_t i = 0;
1682 do {
1683 i++;
1684 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1685 result = descriptor[i] == ';';
1686 } else {
1687 result = false;
1688 }
1689 if (!result) {
1690 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1691 << descriptor << "'";
1692 }
1693 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001694}
1695
Ian Rogersd81871c2011-10-03 13:57:23 -07001696bool DexVerifier::CodeFlowVerifyMethod() {
1697 const uint16_t* insns = code_item_->insns_;
1698 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001699
jeffhaobdb76512011-09-07 11:43:16 -07001700 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 insn_flags_[0].SetChanged();
1702 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001703
jeffhaobdb76512011-09-07 11:43:16 -07001704 /* Continue until no instructions are marked "changed". */
1705 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1707 uint32_t insn_idx = start_guess;
1708 for (; insn_idx < insns_size; insn_idx++) {
1709 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
1711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 if (insn_idx == insns_size) {
1713 if (start_guess != 0) {
1714 /* try again, starting from the top */
1715 start_guess = 0;
1716 continue;
1717 } else {
1718 /* all flags are clear */
1719 break;
1720 }
1721 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 // We carry the working set of registers from instruction to instruction. If this address can
1723 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1724 // "changed" flags, we need to load the set of registers from the table.
1725 // Because we always prefer to continue on to the next instruction, we should never have a
1726 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1727 // target.
1728 work_insn_idx_ = insn_idx;
1729 if (insn_flags_[insn_idx].IsBranchTarget()) {
1730 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001731 } else {
1732#ifndef NDEBUG
1733 /*
1734 * Sanity check: retrieve the stored register line (assuming
1735 * a full table) and make sure it actually matches.
1736 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1738 if (register_line != NULL) {
1739 if (work_line_->CompareLine(register_line) != 0) {
1740 Dump(std::cout);
1741 std::cout << info_messages_.str();
1742 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1743 << "@" << (void*)work_insn_idx_ << std::endl
1744 << " work_line=" << *work_line_ << std::endl
1745 << " expected=" << *register_line;
1746 }
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
1748#endif
1749 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 if (!CodeFlowVerifyInstruction(&start_guess)) {
1751 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001752 return false;
1753 }
jeffhaobdb76512011-09-07 11:43:16 -07001754 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 insn_flags_[insn_idx].SetVisited();
1756 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001757 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001758
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001760 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001761 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001762 * (besides the wasted space), but it indicates a flaw somewhere
1763 * down the line, possibly in the verifier.
1764 *
1765 * If we've substituted "always throw" instructions into the stream,
1766 * we are almost certainly going to have some dead code.
1767 */
1768 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 uint32_t insn_idx = 0;
1770 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001771 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001772 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001773 * may or may not be preceded by a padding NOP (for alignment).
1774 */
1775 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1776 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1777 insns[insn_idx] == Instruction::kArrayDataSignature ||
1778 (insns[insn_idx] == Instruction::NOP &&
1779 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1780 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1781 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001783 }
1784
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001786 if (dead_start < 0)
1787 dead_start = insn_idx;
1788 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001790 dead_start = -1;
1791 }
1792 }
1793 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001795 }
1796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 return true;
1798}
1799
Ian Rogersd81871c2011-10-03 13:57:23 -07001800bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001801#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001803 gDvm.verifierStats.instrsReexamined++;
1804 } else {
1805 gDvm.verifierStats.instrsExamined++;
1806 }
1807#endif
1808
1809 /*
1810 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001811 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001812 * control to another statement:
1813 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001814 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001815 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001816 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001817 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001818 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001819 * throw an exception that is handled by an encompassing "try"
1820 * block.
1821 *
1822 * We can also return, in which case there is no successor instruction
1823 * from this point.
1824 *
1825 * The behavior can be determined from the OpcodeFlags.
1826 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1828 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001829 Instruction::DecodedInstruction dec_insn(inst);
1830 int opcode_flag = inst->Flag();
1831
jeffhaobdb76512011-09-07 11:43:16 -07001832 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001833 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001834 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001836 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1837 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 }
jeffhaobdb76512011-09-07 11:43:16 -07001839
1840 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001841 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001842 * can throw an exception, we will copy/merge this into the "catch"
1843 * address rather than work_line, because we don't want the result
1844 * from the "successful" code path (e.g. a check-cast that "improves"
1845 * a type) to be visible to the exception handler.
1846 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1848 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001849 } else {
1850#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001852#endif
1853 }
1854
1855 switch (dec_insn.opcode_) {
1856 case Instruction::NOP:
1857 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001858 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001859 * a signature that looks like a NOP; if we see one of these in
1860 * the course of executing code then we have a problem.
1861 */
1862 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001864 }
1865 break;
1866
1867 case Instruction::MOVE:
1868 case Instruction::MOVE_FROM16:
1869 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001871 break;
1872 case Instruction::MOVE_WIDE:
1873 case Instruction::MOVE_WIDE_FROM16:
1874 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001876 break;
1877 case Instruction::MOVE_OBJECT:
1878 case Instruction::MOVE_OBJECT_FROM16:
1879 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882
1883 /*
1884 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001886 * might want to hold the result in an actual CPU register, so the
1887 * Dalvik spec requires that these only appear immediately after an
1888 * invoke or filled-new-array.
1889 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001890 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001891 * redundant with the reset done below, but it can make the debug info
1892 * easier to read in some cases.)
1893 */
1894 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
1897 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001905 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 * This statement can only appear as the first instruction in an exception handler (though not
1907 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001908 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001909 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001910 const RegType& res_type = GetCaughtExceptionType();
1911 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 }
jeffhaobdb76512011-09-07 11:43:16 -07001914 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1916 if (!GetMethodReturnType().IsUnknown()) {
1917 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1918 }
jeffhaobdb76512011-09-07 11:43:16 -07001919 }
1920 break;
1921 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001923 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 const RegType& return_type = GetMethodReturnType();
1925 if (!return_type.IsCategory1Types()) {
1926 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1927 } else {
1928 // Compilers may generate synthetic functions that write byte values into boolean fields.
1929 // Also, it may use integer values for boolean, byte, short, and character return types.
1930 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1931 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1932 ((return_type.IsBoolean() || return_type.IsByte() ||
1933 return_type.IsShort() || return_type.IsChar()) &&
1934 src_type.IsInteger()));
1935 /* check the register contents */
1936 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1937 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001938 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
1941 }
1942 break;
1943 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001945 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 const RegType& return_type = GetMethodReturnType();
1947 if (!return_type.IsCategory2Types()) {
1948 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1949 } else {
1950 /* check the register contents */
1951 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1952 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001953 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 }
jeffhaobdb76512011-09-07 11:43:16 -07001955 }
1956 }
1957 break;
1958 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1960 const RegType& return_type = GetMethodReturnType();
1961 if (!return_type.IsReferenceTypes()) {
1962 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1963 } else {
1964 /* return_type is the *expected* return type, not register value */
1965 DCHECK(!return_type.IsZero());
1966 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001967 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1968 // Disallow returning uninitialized values and verify that the reference in vAA is an
1969 // instance of the "return_type"
1970 if (reg_type.IsUninitializedTypes()) {
1971 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1972 } else if (!return_type.IsAssignableFrom(reg_type)) {
1973 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1974 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001975 }
1976 }
1977 }
1978 break;
1979
1980 case Instruction::CONST_4:
1981 case Instruction::CONST_16:
1982 case Instruction::CONST:
1983 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
1986 case Instruction::CONST_HIGH16:
1987 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 work_line_->SetRegisterType(dec_insn.vA_,
1989 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
1991 case Instruction::CONST_WIDE_16:
1992 case Instruction::CONST_WIDE_32:
1993 case Instruction::CONST_WIDE:
1994 case Instruction::CONST_WIDE_HIGH16:
1995 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001997 break;
1998 case Instruction::CONST_STRING:
1999 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002003 // Get type from instruction if unresolved then we need an access check
2004 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2005 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2006 // Register holds class, ie its type is class, but on error we keep it Unknown
2007 work_line_->SetRegisterType(dec_insn.vA_,
2008 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 }
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014 case Instruction::MONITOR_EXIT:
2015 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002016 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002017 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002018 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002019 * to the need to handle asynchronous exceptions, a now-deprecated
2020 * feature that Dalvik doesn't support.)
2021 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002022 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002023 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002024 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002025 * structured locking checks are working, the former would have
2026 * failed on the -enter instruction, and the latter is impossible.
2027 *
2028 * This is fortunate, because issue 3221411 prevents us from
2029 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002030 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002031 * some catch blocks (which will show up as "dead" code when
2032 * we skip them here); if we can't, then the code path could be
2033 * "live" so we still need to check it.
2034 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 opcode_flag &= ~Instruction::kThrow;
2036 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038
Ian Rogers28ad40d2011-10-27 15:19:26 -07002039 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002041 /*
2042 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2043 * could be a "upcast" -- not expected, so we don't try to address it.)
2044 *
2045 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2046 * dec_insn.vA_ when branching to a handler.
2047 */
2048 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2049 const RegType& res_type =
2050 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002051 if (res_type.IsUnknown()) {
2052 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2053 break; // couldn't resolve class
2054 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002055 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2056 const RegType& orig_type =
2057 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2058 if (!res_type.IsNonZeroReferenceTypes()) {
2059 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2060 } else if (!orig_type.IsReferenceTypes()) {
2061 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002062 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002063 if (is_checkcast) {
2064 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002067 }
jeffhaobdb76512011-09-07 11:43:16 -07002068 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
2071 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002072 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2073 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002074 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002075 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 } else {
2077 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2078 }
2079 }
2080 break;
2081 }
2082 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002083 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2084 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2085 // can't create an instance of an interface or abstract class */
2086 if (!res_type.IsInstantiableTypes()) {
2087 Fail(VERIFY_ERROR_INSTANTIATION)
2088 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002090 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2091 // Any registers holding previous allocations from this address that have not yet been
2092 // initialized must be marked invalid.
2093 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2094 // add the new uninitialized reference to the register state
2095 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 }
2097 break;
2098 }
2099 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002100 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2101 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2102 if (!res_type.IsArrayClass()) {
2103 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002104 } else {
2105 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002107 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002108 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002109 }
2110 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 }
jeffhaobdb76512011-09-07 11:43:16 -07002112 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002114 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2115 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2116 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002118 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002119 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002120 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002121 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002122 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002123 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002124 just_set_result = true;
2125 }
2126 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 }
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::CMPL_FLOAT:
2129 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2131 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2132 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::CMPL_DOUBLE:
2135 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2137 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2138 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2142 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2143 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002146 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2147 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2148 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002149 }
2150 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 }
jeffhaobdb76512011-09-07 11:43:16 -07002152 case Instruction::GOTO:
2153 case Instruction::GOTO_16:
2154 case Instruction::GOTO_32:
2155 /* no effect on or use of registers */
2156 break;
2157
2158 case Instruction::PACKED_SWITCH:
2159 case Instruction::SPARSE_SWITCH:
2160 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::FILL_ARRAY_DATA: {
2165 /* Similar to the verification done for APUT */
2166 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2167 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002168 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 if (res_class != NULL) {
2170 Class* component_type = res_class->GetComponentType();
2171 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2172 component_type->IsPrimitiveVoid()) {
2173 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002174 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 } else {
2176 const RegType& value_type = reg_types_.FromClass(component_type);
2177 DCHECK(!value_type.IsUnknown());
2178 // Now verify if the element width in the table matches the element width declared in
2179 // the array
2180 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2181 if (array_data[0] != Instruction::kArrayDataSignature) {
2182 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2183 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002184 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 // Since we don't compress the data in Dex, expect to see equal width of data stored
2186 // in the table and expected from the array class.
2187 if (array_data[1] != elem_width) {
2188 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2189 << " vs " << elem_width << ")";
2190 }
2191 }
2192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 }
2194 }
2195 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 }
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 case Instruction::IF_NE: {
2199 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2200 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2201 bool mismatch = false;
2202 if (reg_type1.IsZero()) { // zero then integral or reference expected
2203 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2204 } else if (reg_type1.IsReferenceTypes()) { // both references?
2205 mismatch = !reg_type2.IsReferenceTypes();
2206 } else { // both integral?
2207 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2208 }
2209 if (mismatch) {
2210 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2211 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002212 }
2213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 }
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::IF_LT:
2216 case Instruction::IF_GE:
2217 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 case Instruction::IF_LE: {
2219 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2220 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2221 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2222 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2223 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002224 }
2225 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 }
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 case Instruction::IF_NEZ: {
2229 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2230 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2231 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2232 }
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 }
jeffhaobdb76512011-09-07 11:43:16 -07002235 case Instruction::IF_LTZ:
2236 case Instruction::IF_GEZ:
2237 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 case Instruction::IF_LEZ: {
2239 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2240 if (!reg_type.IsIntegralTypes()) {
2241 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2242 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2243 }
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 }
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2251 break;
jeffhaobdb76512011-09-07 11:43:16 -07002252 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 VerifyAGet(dec_insn, reg_types_.Char(), true);
2254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 case Instruction::AGET:
2259 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2260 break;
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 VerifyAGet(dec_insn, reg_types_.Long(), true);
2263 break;
2264 case Instruction::AGET_OBJECT:
2265 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 case Instruction::APUT_BOOLEAN:
2269 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2270 break;
2271 case Instruction::APUT_BYTE:
2272 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2273 break;
2274 case Instruction::APUT_CHAR:
2275 VerifyAPut(dec_insn, reg_types_.Char(), true);
2276 break;
2277 case Instruction::APUT_SHORT:
2278 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002300 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
2302 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002303 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002306 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002309 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
2315 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 break;
2321 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002322 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002325 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002328 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 break;
jeffhaobdb76512011-09-07 11:43:16 -07002330 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002331 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002335 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002338 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002341 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002344 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
2346 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002347 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002350 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002353 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 break;
2355
2356 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002357 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 break;
2359 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002360 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 break;
2362 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002363 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 break;
2365 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002366 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002369 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002372 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002375 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377
2378 case Instruction::INVOKE_VIRTUAL:
2379 case Instruction::INVOKE_VIRTUAL_RANGE:
2380 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 case Instruction::INVOKE_SUPER_RANGE: {
2382 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2383 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2384 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2385 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2386 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2387 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002388 const char* descriptor;
2389 if (called_method == NULL) {
2390 uint32_t method_idx = dec_insn.vB_;
2391 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2392 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002393 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002394 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002395 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002396 }
Ian Rogers9074b992011-10-26 17:41:55 -07002397 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002398 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002400 just_set_result = true;
2401 }
2402 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 }
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 case Instruction::INVOKE_DIRECT_RANGE: {
2406 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2407 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2408 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002409 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 * Some additional checks when calling a constructor. We know from the invocation arg check
2411 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2412 * that to require that called_method->klass is the same as this->klass or this->super,
2413 * allowing the latter only if the "this" argument is the same as the "this" argument to
2414 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002415 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 bool is_constructor;
2417 if (called_method != NULL) {
2418 is_constructor = called_method->IsConstructor();
2419 } else {
2420 uint32_t method_idx = dec_insn.vB_;
2421 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2422 const char* name = dex_file_->GetMethodName(method_id);
2423 is_constructor = strcmp(name, "<init>") == 0;
2424 }
2425 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2427 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429
2430 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 if (this_type.IsZero()) {
2432 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002435 if (called_method != NULL) {
2436 Class* this_class = this_type.GetClass();
2437 DCHECK(this_class != NULL);
2438 /* must be in same class or in superclass */
2439 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2440 if (this_class != method_->GetDeclaringClass()) {
2441 Fail(VERIFY_ERROR_GENERIC)
2442 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2443 break;
2444 }
2445 } else if (called_method->GetDeclaringClass() != this_class) {
2446 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 }
2450
2451 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002452 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2454 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 }
2457
2458 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002459 * Replace the uninitialized reference with an initialized one. We need to do this for all
2460 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002461 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 work_line_->MarkRefsAsInitialized(this_type);
2463 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002465 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 const char* descriptor;
2467 if (called_method == NULL) {
2468 uint32_t method_idx = dec_insn.vB_;
2469 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2470 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002471 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002473 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 }
Ian Rogers9074b992011-10-26 17:41:55 -07002475 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002478 just_set_result = true;
2479 }
2480 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 }
jeffhaobdb76512011-09-07 11:43:16 -07002482 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 case Instruction::INVOKE_STATIC_RANGE: {
2484 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2485 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2486 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 const char* descriptor;
2488 if (called_method == NULL) {
2489 uint32_t method_idx = dec_insn.vB_;
2490 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2491 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002492 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002493 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002494 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 }
Ian Rogers9074b992011-10-26 17:41:55 -07002496 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002497 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->SetResultRegisterType(return_type);
2499 just_set_result = true;
2500 }
jeffhaobdb76512011-09-07 11:43:16 -07002501 }
2502 break;
2503 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 case Instruction::INVOKE_INTERFACE_RANGE: {
2505 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2506 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2507 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002508 if (abs_method != NULL) {
2509 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002510 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002511 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2512 << PrettyMethod(abs_method) << "'";
2513 break;
2514 }
2515 }
2516 /* Get the type of the "this" arg, which should either be a sub-interface of called
2517 * interface or Object (see comments in RegType::JoinClass).
2518 */
2519 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2520 if (failure_ == VERIFY_ERROR_NONE) {
2521 if (this_type.IsZero()) {
2522 /* null pointer always passes (and always fails at runtime) */
2523 } else {
2524 if (this_type.IsUninitializedTypes()) {
2525 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2526 << this_type;
2527 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002529 // In the past we have tried to assert that "called_interface" is assignable
2530 // from "this_type.GetClass()", however, as we do an imprecise Join
2531 // (RegType::JoinClass) we don't have full information on what interfaces are
2532 // implemented by "this_type". For example, two classes may implement the same
2533 // interfaces and have a common parent that doesn't implement the interface. The
2534 // join will set "this_type" to the parent class and a test that this implements
2535 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002536 }
2537 }
jeffhaobdb76512011-09-07 11:43:16 -07002538 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 * We don't have an object instance, so we can't find the concrete method. However, all of
2540 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002541 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002542 const char* descriptor;
2543 if (abs_method == NULL) {
2544 uint32_t method_idx = dec_insn.vB_;
2545 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2546 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002547 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002548 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002549 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002550 }
Ian Rogers9074b992011-10-26 17:41:55 -07002551 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002552 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2553 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002555 just_set_result = true;
2556 }
2557 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 }
jeffhaobdb76512011-09-07 11:43:16 -07002559 case Instruction::NEG_INT:
2560 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::NEG_LONG:
2564 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618
2619 case Instruction::ADD_INT:
2620 case Instruction::SUB_INT:
2621 case Instruction::MUL_INT:
2622 case Instruction::REM_INT:
2623 case Instruction::DIV_INT:
2624 case Instruction::SHL_INT:
2625 case Instruction::SHR_INT:
2626 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::AND_INT:
2630 case Instruction::OR_INT:
2631 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::ADD_LONG:
2635 case Instruction::SUB_LONG:
2636 case Instruction::MUL_LONG:
2637 case Instruction::DIV_LONG:
2638 case Instruction::REM_LONG:
2639 case Instruction::AND_LONG:
2640 case Instruction::OR_LONG:
2641 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::SHL_LONG:
2645 case Instruction::SHR_LONG:
2646 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 /* shift distance is Int, making these different from other binary operations */
2648 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::ADD_FLOAT:
2651 case Instruction::SUB_FLOAT:
2652 case Instruction::MUL_FLOAT:
2653 case Instruction::DIV_FLOAT:
2654 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::ADD_DOUBLE:
2658 case Instruction::SUB_DOUBLE:
2659 case Instruction::MUL_DOUBLE:
2660 case Instruction::DIV_DOUBLE:
2661 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::ADD_INT_2ADDR:
2665 case Instruction::SUB_INT_2ADDR:
2666 case Instruction::MUL_INT_2ADDR:
2667 case Instruction::REM_INT_2ADDR:
2668 case Instruction::SHL_INT_2ADDR:
2669 case Instruction::SHR_INT_2ADDR:
2670 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::AND_INT_2ADDR:
2674 case Instruction::OR_INT_2ADDR:
2675 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::ADD_LONG_2ADDR:
2682 case Instruction::SUB_LONG_2ADDR:
2683 case Instruction::MUL_LONG_2ADDR:
2684 case Instruction::DIV_LONG_2ADDR:
2685 case Instruction::REM_LONG_2ADDR:
2686 case Instruction::AND_LONG_2ADDR:
2687 case Instruction::OR_LONG_2ADDR:
2688 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::SHL_LONG_2ADDR:
2692 case Instruction::SHR_LONG_2ADDR:
2693 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
2696 case Instruction::ADD_FLOAT_2ADDR:
2697 case Instruction::SUB_FLOAT_2ADDR:
2698 case Instruction::MUL_FLOAT_2ADDR:
2699 case Instruction::DIV_FLOAT_2ADDR:
2700 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::ADD_DOUBLE_2ADDR:
2704 case Instruction::SUB_DOUBLE_2ADDR:
2705 case Instruction::MUL_DOUBLE_2ADDR:
2706 case Instruction::DIV_DOUBLE_2ADDR:
2707 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::ADD_INT_LIT16:
2711 case Instruction::RSUB_INT:
2712 case Instruction::MUL_INT_LIT16:
2713 case Instruction::DIV_INT_LIT16:
2714 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717 case Instruction::AND_INT_LIT16:
2718 case Instruction::OR_INT_LIT16:
2719 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
2722 case Instruction::ADD_INT_LIT8:
2723 case Instruction::RSUB_INT_LIT8:
2724 case Instruction::MUL_INT_LIT8:
2725 case Instruction::DIV_INT_LIT8:
2726 case Instruction::REM_INT_LIT8:
2727 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002728 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::AND_INT_LIT8:
2733 case Instruction::OR_INT_LIT8:
2734 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737
2738 /*
2739 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002740 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002741 * inserted in the course of verification, we can expect to see it here.
2742 */
jeffhaob4df5142011-09-19 20:25:32 -07002743 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002747 case Instruction::UNUSED_EE:
2748 case Instruction::UNUSED_EF:
2749 case Instruction::UNUSED_F2:
2750 case Instruction::UNUSED_F3:
2751 case Instruction::UNUSED_F4:
2752 case Instruction::UNUSED_F5:
2753 case Instruction::UNUSED_F6:
2754 case Instruction::UNUSED_F7:
2755 case Instruction::UNUSED_F8:
2756 case Instruction::UNUSED_F9:
2757 case Instruction::UNUSED_FA:
2758 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002759 case Instruction::UNUSED_F0:
2760 case Instruction::UNUSED_F1:
2761 case Instruction::UNUSED_E3:
2762 case Instruction::UNUSED_E8:
2763 case Instruction::UNUSED_E7:
2764 case Instruction::UNUSED_E4:
2765 case Instruction::UNUSED_E9:
2766 case Instruction::UNUSED_FC:
2767 case Instruction::UNUSED_E5:
2768 case Instruction::UNUSED_EA:
2769 case Instruction::UNUSED_FD:
2770 case Instruction::UNUSED_E6:
2771 case Instruction::UNUSED_EB:
2772 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002773 case Instruction::UNUSED_3E:
2774 case Instruction::UNUSED_3F:
2775 case Instruction::UNUSED_40:
2776 case Instruction::UNUSED_41:
2777 case Instruction::UNUSED_42:
2778 case Instruction::UNUSED_43:
2779 case Instruction::UNUSED_73:
2780 case Instruction::UNUSED_79:
2781 case Instruction::UNUSED_7A:
2782 case Instruction::UNUSED_EC:
2783 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002784 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786
2787 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002788 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002789 * complain if an instruction is missing (which is desirable).
2790 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002792
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 if (failure_ != VERIFY_ERROR_NONE) {
2794 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002795 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002796 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002797 return false;
2798 } else {
2799 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002800 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002802 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002804 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002806 opcode_flag = Instruction::kThrow;
2807 }
2808 }
jeffhaobdb76512011-09-07 11:43:16 -07002809 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 * If we didn't just set the result register, clear it out. This ensures that you can only use
2811 * "move-result" immediately after the result is set. (We could check this statically, but it's
2812 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002813 */
2814 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002816 }
2817
jeffhaoa0a764a2011-09-16 10:43:38 -07002818 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002819 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2821 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2822 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002823 return false;
2824 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2826 // next instruction isn't one.
2827 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002828 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 }
2830 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2831 if (next_line != NULL) {
2832 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2833 // needed.
2834 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002835 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 }
jeffhaobdb76512011-09-07 11:43:16 -07002837 } else {
2838 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 * We're not recording register data for the next instruction, so we don't know what the prior
2840 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002841 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002843 }
2844 }
2845
2846 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002847 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002848 *
2849 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002850 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002851 * somebody could get a reference field, check it for zero, and if the
2852 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002853 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002854 * that, and will reject the code.
2855 *
2856 * TODO: avoid re-fetching the branch target
2857 */
2858 if ((opcode_flag & Instruction::kBranch) != 0) {
2859 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002861 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002863 return false;
2864 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002865 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002867 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 }
jeffhaobdb76512011-09-07 11:43:16 -07002869 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002871 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 }
jeffhaobdb76512011-09-07 11:43:16 -07002873 }
2874
2875 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002876 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002877 *
2878 * We've already verified that the table is structurally sound, so we
2879 * just need to walk through and tag the targets.
2880 */
2881 if ((opcode_flag & Instruction::kSwitch) != 0) {
2882 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2883 const uint16_t* switch_insns = insns + offset_to_switch;
2884 int switch_count = switch_insns[1];
2885 int offset_to_targets, targ;
2886
2887 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2888 /* 0 = sig, 1 = count, 2/3 = first key */
2889 offset_to_targets = 4;
2890 } else {
2891 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002892 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002893 offset_to_targets = 2 + 2 * switch_count;
2894 }
2895
2896 /* verify each switch target */
2897 for (targ = 0; targ < switch_count; targ++) {
2898 int offset;
2899 uint32_t abs_offset;
2900
2901 /* offsets are 32-bit, and only partly endian-swapped */
2902 offset = switch_insns[offset_to_targets + targ * 2] |
2903 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 abs_offset = work_insn_idx_ + offset;
2905 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2906 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002907 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 }
2909 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002910 return false;
2911 }
2912 }
2913
2914 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2916 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002917 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2919 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002920 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002921
Ian Rogers0571d352011-11-03 19:51:38 -07002922 for (; iterator.HasNext(); iterator.Next()) {
2923 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 within_catch_all = true;
2925 }
jeffhaobdb76512011-09-07 11:43:16 -07002926 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2928 * "work_regs", because at runtime the exception will be thrown before the instruction
2929 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002930 */
Ian Rogers0571d352011-11-03 19:51:38 -07002931 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002932 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
jeffhaobdb76512011-09-07 11:43:16 -07002934 }
2935
2936 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2938 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002939 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002941 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 * The state in work_line reflects the post-execution state. If the current instruction is a
2943 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002944 * it will do so before grabbing the lock).
2945 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2947 Fail(VERIFY_ERROR_GENERIC)
2948 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002949 return false;
2950 }
2951 }
2952 }
2953
jeffhaod1f0fde2011-09-08 17:25:33 -07002954 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 if ((opcode_flag & Instruction::kReturn) != 0) {
2956 if(!work_line_->VerifyMonitorStackEmpty()) {
2957 return false;
2958 }
jeffhaobdb76512011-09-07 11:43:16 -07002959 }
2960
2961 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002962 * Update start_guess. Advance to the next instruction of that's
2963 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002964 * neither of those exists we're in a return or throw; leave start_guess
2965 * alone and let the caller sort it out.
2966 */
2967 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002969 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2970 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002972 }
2973
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2975 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002976
2977 return true;
2978}
2979
Ian Rogers28ad40d2011-10-27 15:19:26 -07002980const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002981 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002982 Class* referrer = method_->GetDeclaringClass();
2983 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2984 const RegType& result =
2985 klass != NULL ? reg_types_.FromClass(klass)
2986 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2987 if (klass == NULL && !result.IsUnresolvedTypes()) {
2988 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002990 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2991 // check at runtime if access is allowed and so pass here.
2992 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2993 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002994 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002995 << result << "'";
2996 return reg_types_.Unknown();
2997 } else {
2998 return result;
2999 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003000}
3001
Ian Rogers28ad40d2011-10-27 15:19:26 -07003002const RegType& DexVerifier::GetCaughtExceptionType() {
3003 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003005 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3007 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003008 CatchHandlerIterator iterator(handlers_ptr);
3009 for (; iterator.HasNext(); iterator.Next()) {
3010 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3011 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003012 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003014 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3016 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3017 * test, so is essentially harmless.
3018 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003019 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3020 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3021 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003023 common_super = &exception;
3024 } else if (common_super->Equals(exception)) {
3025 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003027 common_super = &common_super->Merge(exception, &reg_types_);
3028 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 }
3030 }
3031 }
3032 }
Ian Rogers0571d352011-11-03 19:51:38 -07003033 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 }
3035 }
3036 if (common_super == NULL) {
3037 /* no catch blocks, or no catches with classes we can find */
3038 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3039 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003040 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003041}
3042
3043Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003044 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3045 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3046 if (failure_ != VERIFY_ERROR_NONE) {
3047 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3048 return NULL;
3049 }
3050 if(klass_type.IsUnresolvedTypes()) {
3051 return NULL; // Can't resolve Class so no more to do here
3052 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 Class* referrer = method_->GetDeclaringClass();
3054 DexCache* dex_cache = referrer->GetDexCache();
3055 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3056 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003057 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003058 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003059 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 if (is_direct) {
3061 res_method = klass->FindDirectMethod(name, signature);
3062 } else if (klass->IsInterface()) {
3063 res_method = klass->FindInterfaceMethod(name, signature);
3064 } else {
3065 res_method = klass->FindVirtualMethod(name, signature);
3066 }
3067 if (res_method != NULL) {
3068 dex_cache->SetResolvedMethod(method_idx, res_method);
3069 } else {
3070 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003071 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 << " " << signature;
3073 return NULL;
3074 }
3075 }
3076 /* Check if access is allowed. */
3077 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3078 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003079 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 return NULL;
3081 }
3082 return res_method;
3083}
3084
3085Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3086 MethodType method_type, bool is_range, bool is_super) {
3087 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3088 // we're making.
3089 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3090 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003091 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 return NULL;
3093 }
3094 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3095 // enforce them here.
3096 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3097 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3098 << PrettyMethod(res_method);
3099 return NULL;
3100 }
3101 // See if the method type implied by the invoke instruction matches the access flags for the
3102 // target method.
3103 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3104 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3105 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3106 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003107 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3108 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 return NULL;
3110 }
3111 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3112 // has a vtable entry for the target method.
3113 if (is_super) {
3114 DCHECK(method_type == METHOD_VIRTUAL);
3115 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3116 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3117 if (super == NULL) { // Only Object has no super class
3118 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3119 << " to super " << PrettyMethod(res_method);
3120 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003121 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003123 << " to super " << PrettyDescriptor(super)
3124 << "." << mh.GetName()
3125 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 }
3127 return NULL;
3128 }
3129 }
3130 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3131 // match the call to the signature. Also, we might might be calling through an abstract method
3132 // definition (which doesn't have register count values).
3133 int expected_args = dec_insn.vA_;
3134 /* caught by static verifier */
3135 DCHECK(is_range || expected_args <= 5);
3136 if (expected_args > code_item_->outs_size_) {
3137 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3138 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3139 return NULL;
3140 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003141 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 if (sig[0] != '(') {
3143 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3144 << " as descriptor doesn't start with '(': " << sig;
3145 return NULL;
3146 }
jeffhaobdb76512011-09-07 11:43:16 -07003147 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003148 * Check the "this" argument, which must be an instance of the class
3149 * that declared the method. For an interface class, we don't do the
3150 * full interface merge, so we can't do a rigorous check here (which
3151 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003152 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 int actual_args = 0;
3154 if (!res_method->IsStatic()) {
3155 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3156 if (failure_ != VERIFY_ERROR_NONE) {
3157 return NULL;
3158 }
3159 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3160 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3161 return NULL;
3162 }
3163 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003164 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3165 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3166 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3167 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 return NULL;
3169 }
3170 }
3171 actual_args++;
3172 }
3173 /*
3174 * Process the target method's signature. This signature may or may not
3175 * have been verified, so we can't assume it's properly formed.
3176 */
3177 size_t sig_offset = 0;
3178 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3179 if (actual_args >= expected_args) {
3180 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3181 << "'. Expected " << expected_args << " args, found more ("
3182 << sig.substr(sig_offset) << ")";
3183 return NULL;
3184 }
3185 std::string descriptor;
3186 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3187 size_t end;
3188 if (sig[sig_offset] == 'L') {
3189 end = sig.find(';', sig_offset);
3190 } else {
3191 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3192 if (sig[end] == 'L') {
3193 end = sig.find(';', end);
3194 }
3195 }
3196 if (end == std::string::npos) {
3197 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3198 << "bad signature component '" << sig << "' (missing ';')";
3199 return NULL;
3200 }
3201 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3202 sig_offset = end;
3203 } else {
3204 descriptor = sig[sig_offset];
3205 }
3206 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003207 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3208 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003209 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3210 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3211 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 }
3213 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3214 }
3215 if (sig[sig_offset] != ')') {
3216 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3217 return NULL;
3218 }
3219 if (actual_args != expected_args) {
3220 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3221 << " expected " << expected_args << " args, found " << actual_args;
3222 return NULL;
3223 } else {
3224 return res_method;
3225 }
3226}
3227
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003228const RegType& DexVerifier::GetMethodReturnType() {
3229 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3230 MethodHelper(method_).GetReturnTypeDescriptor());
3231}
3232
Ian Rogersd81871c2011-10-03 13:57:23 -07003233void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3234 const RegType& insn_type, bool is_primitive) {
3235 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3236 if (!index_type.IsArrayIndexTypes()) {
3237 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3238 } else {
3239 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3240 if (failure_ == VERIFY_ERROR_NONE) {
3241 if (array_class == NULL) {
3242 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3243 // instruction type. TODO: have a proper notion of bottom here.
3244 if (!is_primitive || insn_type.IsCategory1Types()) {
3245 // Reference or category 1
3246 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3247 } else {
3248 // Category 2
3249 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3250 }
3251 } else {
3252 /* verify the class */
3253 Class* component_class = array_class->GetComponentType();
3254 const RegType& component_type = reg_types_.FromClass(component_class);
3255 if (!array_class->IsArrayClass()) {
3256 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003257 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 } else if (component_class->IsPrimitive() && !is_primitive) {
3259 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003260 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 << " source for aget-object";
3262 } else if (!component_class->IsPrimitive() && is_primitive) {
3263 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003264 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 << " source for category 1 aget";
3266 } else if (is_primitive && !insn_type.Equals(component_type) &&
3267 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3268 (insn_type.IsLong() && component_type.IsDouble()))) {
3269 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003270 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 << " incompatible with aget of type " << insn_type;
3272 } else {
3273 // Use knowledge of the field type which is stronger than the type inferred from the
3274 // instruction, which can't differentiate object types and ints from floats, longs from
3275 // doubles.
3276 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3277 }
3278 }
3279 }
3280 }
3281}
3282
3283void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3284 const RegType& insn_type, bool is_primitive) {
3285 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3286 if (!index_type.IsArrayIndexTypes()) {
3287 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3288 } else {
3289 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3290 if (failure_ == VERIFY_ERROR_NONE) {
3291 if (array_class == NULL) {
3292 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3293 // instruction type.
3294 } else {
3295 /* verify the class */
3296 Class* component_class = array_class->GetComponentType();
3297 const RegType& component_type = reg_types_.FromClass(component_class);
3298 if (!array_class->IsArrayClass()) {
3299 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003300 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 } else if (component_class->IsPrimitive() && !is_primitive) {
3302 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003303 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 << " source for aput-object";
3305 } else if (!component_class->IsPrimitive() && is_primitive) {
3306 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003307 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 << " source for category 1 aput";
3309 } else if (is_primitive && !insn_type.Equals(component_type) &&
3310 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3311 (insn_type.IsLong() && component_type.IsDouble()))) {
3312 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003313 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 << " incompatible with aput of type " << insn_type;
3315 } else {
3316 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003317 // Note: we use the instruction type (rather than the component type) for aput-object as
3318 // incompatible classes will be caught at runtime as an array store exception
3319 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
3321 }
3322 }
3323 }
3324}
3325
3326Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003327 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3328 // Check access to class
3329 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3330 if (failure_ != VERIFY_ERROR_NONE) {
3331 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3332 << dex_file_->GetFieldName(field_id) << ") in "
3333 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3334 return NULL;
3335 }
3336 if(klass_type.IsUnresolvedTypes()) {
3337 return NULL; // Can't resolve Class so no more to do here
3338 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003339 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003341 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3342 << dex_file_->GetFieldName(field_id) << ") in "
3343 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 DCHECK(Thread::Current()->IsExceptionPending());
3345 Thread::Current()->ClearException();
3346 return NULL;
3347 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3348 field->GetAccessFlags())) {
3349 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3350 << " from " << PrettyClass(method_->GetDeclaringClass());
3351 return NULL;
3352 } else if (!field->IsStatic()) {
3353 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3354 return NULL;
3355 } else {
3356 return field;
3357 }
3358}
3359
Ian Rogersd81871c2011-10-03 13:57:23 -07003360Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003361 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3362 // Check access to class
3363 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3364 if (failure_ != VERIFY_ERROR_NONE) {
3365 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3366 << dex_file_->GetFieldName(field_id) << ") in "
3367 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3368 return NULL;
3369 }
3370 if(klass_type.IsUnresolvedTypes()) {
3371 return NULL; // Can't resolve Class so no more to do here
3372 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003373 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003375 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3376 << dex_file_->GetFieldName(field_id) << ") in "
3377 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 DCHECK(Thread::Current()->IsExceptionPending());
3379 Thread::Current()->ClearException();
3380 return NULL;
3381 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3382 field->GetAccessFlags())) {
3383 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3384 << " from " << PrettyClass(method_->GetDeclaringClass());
3385 return NULL;
3386 } else if (field->IsStatic()) {
3387 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3388 << " to not be static";
3389 return NULL;
3390 } else if (obj_type.IsZero()) {
3391 // Cannot infer and check type, however, access will cause null pointer exception
3392 return field;
3393 } else if(obj_type.IsUninitializedReference() &&
3394 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3395 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3396 // Field accesses through uninitialized references are only allowable for constructors where
3397 // the field is declared in this class
3398 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3399 << " of a not fully initialized object within the context of "
3400 << PrettyMethod(method_);
3401 return NULL;
3402 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3403 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3404 // of C1. For resolution to occur the declared class of the field must be compatible with
3405 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3406 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3407 << " from object of type " << PrettyClass(obj_type.GetClass());
3408 return NULL;
3409 } else {
3410 return field;
3411 }
3412}
3413
Ian Rogersb94a27b2011-10-26 00:33:41 -07003414void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3415 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003416 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003417 Field* field;
3418 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003419 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003420 } else {
3421 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003422 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003423 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003424 if (failure_ != VERIFY_ERROR_NONE) {
3425 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3426 } else {
3427 const char* descriptor;
3428 const ClassLoader* loader;
3429 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003430 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003431 loader = field->GetDeclaringClass()->GetClassLoader();
3432 } else {
3433 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3434 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3435 loader = method_->GetDeclaringClass()->GetClassLoader();
3436 }
3437 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003439 if (field_type.Equals(insn_type) ||
3440 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3441 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 // expected that read is of the correct primitive type or that int reads are reading
3443 // floats or long reads are reading doubles
3444 } else {
3445 // This is a global failure rather than a class change failure as the instructions and
3446 // the descriptors for the type should have been consistent within the same file at
3447 // compile time
3448 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003449 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003450 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003451 return;
3452 }
3453 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003454 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003456 << " to be compatible with type '" << insn_type
3457 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003458 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 return;
3460 }
3461 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003462 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 }
3464}
3465
Ian Rogersb94a27b2011-10-26 00:33:41 -07003466void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3467 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003468 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003469 Field* field;
3470 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003471 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003472 } else {
3473 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003474 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003475 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003476 if (failure_ != VERIFY_ERROR_NONE) {
3477 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3478 } else {
3479 const char* descriptor;
3480 const ClassLoader* loader;
3481 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003482 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003483 loader = field->GetDeclaringClass()->GetClassLoader();
3484 } else {
3485 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3486 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3487 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003489 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3490 if (field != NULL) {
3491 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3492 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3493 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3494 return;
3495 }
3496 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003498 // Primitive field assignability rules are weaker than regular assignability rules
3499 bool instruction_compatible;
3500 bool value_compatible;
3501 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3502 if (field_type.IsIntegralTypes()) {
3503 instruction_compatible = insn_type.IsIntegralTypes();
3504 value_compatible = value_type.IsIntegralTypes();
3505 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003506 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003507 value_compatible = value_type.IsFloatTypes();
3508 } else if (field_type.IsLong()) {
3509 instruction_compatible = insn_type.IsLong();
3510 value_compatible = value_type.IsLongTypes();
3511 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003512 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003513 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003514 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003515 instruction_compatible = false; // reference field with primitive store
3516 value_compatible = false; // unused
3517 }
3518 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003519 // This is a global failure rather than a class change failure as the instructions and
3520 // the descriptors for the type should have been consistent within the same file at
3521 // compile time
3522 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003523 << " to be of type '" << insn_type
3524 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003525 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 return;
3527 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003528 if (!value_compatible) {
3529 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3530 << " of type " << value_type
3531 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003532 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003533 return;
3534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003536 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003538 << " to be compatible with type '" << insn_type
3539 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003540 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 return;
3542 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003543 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 }
3546}
3547
3548bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3549 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3550 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3551 return false;
3552 }
3553 return true;
3554}
3555
3556void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003557 const RegType& res_type, bool is_range) {
3558 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003559 /*
3560 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3561 * list and fail. It's legal, if silly, for arg_count to be zero.
3562 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003563 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3564 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 uint32_t arg_count = dec_insn.vA_;
3566 for (size_t ui = 0; ui < arg_count; ui++) {
3567 uint32_t get_reg;
3568
3569 if (is_range)
3570 get_reg = dec_insn.vC_ + ui;
3571 else
3572 get_reg = dec_insn.arg_[ui];
3573
3574 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3575 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3576 << ") not valid";
3577 return;
3578 }
3579 }
3580}
3581
3582void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003583 if (Runtime::Current()->IsStarted()) {
3584 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3585 << " " << fail_messages_.str();
3586 return;
3587 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3589 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3590 VerifyErrorRefType ref_type;
3591 switch (inst->Opcode()) {
3592 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003593 case Instruction::CHECK_CAST:
3594 case Instruction::INSTANCE_OF:
3595 case Instruction::NEW_INSTANCE:
3596 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003598 case Instruction::FILLED_NEW_ARRAY_RANGE:
3599 ref_type = VERIFY_ERROR_REF_CLASS;
3600 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003602 case Instruction::IGET_BOOLEAN:
3603 case Instruction::IGET_BYTE:
3604 case Instruction::IGET_CHAR:
3605 case Instruction::IGET_SHORT:
3606 case Instruction::IGET_WIDE:
3607 case Instruction::IGET_OBJECT:
3608 case Instruction::IPUT:
3609 case Instruction::IPUT_BOOLEAN:
3610 case Instruction::IPUT_BYTE:
3611 case Instruction::IPUT_CHAR:
3612 case Instruction::IPUT_SHORT:
3613 case Instruction::IPUT_WIDE:
3614 case Instruction::IPUT_OBJECT:
3615 case Instruction::SGET:
3616 case Instruction::SGET_BOOLEAN:
3617 case Instruction::SGET_BYTE:
3618 case Instruction::SGET_CHAR:
3619 case Instruction::SGET_SHORT:
3620 case Instruction::SGET_WIDE:
3621 case Instruction::SGET_OBJECT:
3622 case Instruction::SPUT:
3623 case Instruction::SPUT_BOOLEAN:
3624 case Instruction::SPUT_BYTE:
3625 case Instruction::SPUT_CHAR:
3626 case Instruction::SPUT_SHORT:
3627 case Instruction::SPUT_WIDE:
3628 case Instruction::SPUT_OBJECT:
3629 ref_type = VERIFY_ERROR_REF_FIELD;
3630 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003632 case Instruction::INVOKE_VIRTUAL_RANGE:
3633 case Instruction::INVOKE_SUPER:
3634 case Instruction::INVOKE_SUPER_RANGE:
3635 case Instruction::INVOKE_DIRECT:
3636 case Instruction::INVOKE_DIRECT_RANGE:
3637 case Instruction::INVOKE_STATIC:
3638 case Instruction::INVOKE_STATIC_RANGE:
3639 case Instruction::INVOKE_INTERFACE:
3640 case Instruction::INVOKE_INTERFACE_RANGE:
3641 ref_type = VERIFY_ERROR_REF_METHOD;
3642 break;
jeffhaobdb76512011-09-07 11:43:16 -07003643 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003644 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003645 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3648 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3649 // instruction, so assert it.
3650 size_t width = inst->SizeInCodeUnits();
3651 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003652 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 // NOPs
3654 for (size_t i = 2; i < width; i++) {
3655 insns[work_insn_idx_ + i] = Instruction::NOP;
3656 }
3657 // Encode the opcode, with the failure code in the high byte
3658 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3659 (failure_ << 8) | // AA - component
3660 (ref_type << (8 + kVerifyErrorRefTypeShift));
3661 insns[work_insn_idx_] = new_instruction;
3662 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3663 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003664 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3665 << fail_messages_.str();
3666 if (gDebugVerify) {
3667 std::cout << std::endl << info_messages_.str();
3668 Dump(std::cout);
3669 }
jeffhaobdb76512011-09-07 11:43:16 -07003670}
jeffhaoba5ebb92011-08-25 17:24:37 -07003671
Ian Rogersd81871c2011-10-03 13:57:23 -07003672bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3673 const bool merge_debug = true;
3674 bool changed = true;
3675 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3676 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003677 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 * We haven't processed this instruction before, and we haven't touched the registers here, so
3679 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3680 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003681 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003683 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3685 copy->CopyFromLine(target_line);
3686 changed = target_line->MergeRegisters(merge_line);
3687 if (failure_ != VERIFY_ERROR_NONE) {
3688 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003689 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003690 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003691 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3692 << *copy.get() << " MERGE" << std::endl
3693 << *merge_line << " ==" << std::endl
3694 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003695 }
3696 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 if (changed) {
3698 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003699 }
3700 return true;
3701}
3702
Ian Rogersd81871c2011-10-03 13:57:23 -07003703void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3704 size_t* log2_max_gc_pc) {
3705 size_t local_gc_points = 0;
3706 size_t max_insn = 0;
3707 size_t max_ref_reg = -1;
3708 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3709 if (insn_flags_[i].IsGcPoint()) {
3710 local_gc_points++;
3711 max_insn = i;
3712 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003713 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003714 }
3715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 *gc_points = local_gc_points;
3717 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3718 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003719 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003720 i++;
3721 }
3722 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003723}
3724
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003725const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003726 size_t num_entries, ref_bitmap_bits, pc_bits;
3727 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3728 // There's a single byte to encode the size of each bitmap
3729 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3730 // TODO: either a better GC map format or per method failures
3731 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3732 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003733 return NULL;
3734 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003735 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3736 // There are 2 bytes to encode the number of entries
3737 if (num_entries >= 65536) {
3738 // TODO: either a better GC map format or per method failures
3739 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3740 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003741 return NULL;
3742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003744 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003745 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003746 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003747 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003748 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003749 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003751 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003752 // TODO: either a better GC map format or per method failures
3753 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3754 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3755 return NULL;
3756 }
3757 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003758 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 if (table == NULL) {
3760 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3761 return NULL;
3762 }
3763 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003764 table->push_back(format);
3765 table->push_back(ref_bitmap_bytes);
3766 table->push_back(num_entries & 0xFF);
3767 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3770 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003771 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003773 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 }
3775 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003776 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 }
3778 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003779 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 return table;
3781}
jeffhaoa0a764a2011-09-16 10:43:38 -07003782
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003783void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3785 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003786 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003787 size_t map_index = 0;
3788 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3789 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3790 if (insn_flags_[i].IsGcPoint()) {
3791 CHECK_LT(map_index, map.NumEntries());
3792 CHECK_EQ(map.GetPC(map_index), i);
3793 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3794 map_index++;
3795 RegisterLine* line = reg_table_.GetLine(i);
3796 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003797 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 CHECK_LT(j / 8, map.RegWidth());
3799 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3800 } else if ((j / 8) < map.RegWidth()) {
3801 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3802 } else {
3803 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3804 }
3805 }
3806 } else {
3807 CHECK(reg_bitmap == NULL);
3808 }
3809 }
3810}
jeffhaoa0a764a2011-09-16 10:43:38 -07003811
Ian Rogersd81871c2011-10-03 13:57:23 -07003812const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3813 size_t num_entries = NumEntries();
3814 // Do linear or binary search?
3815 static const size_t kSearchThreshold = 8;
3816 if (num_entries < kSearchThreshold) {
3817 for (size_t i = 0; i < num_entries; i++) {
3818 if (GetPC(i) == dex_pc) {
3819 return GetBitMap(i);
3820 }
3821 }
3822 } else {
3823 int lo = 0;
3824 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003825 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 int mid = (hi + lo) / 2;
3827 int mid_pc = GetPC(mid);
3828 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003829 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003831 hi = mid - 1;
3832 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003834 }
3835 }
3836 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003837 if (error_if_not_present) {
3838 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3839 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003840 return NULL;
3841}
3842
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003843DexVerifier::GcMapTable DexVerifier::gc_maps_;
3844
3845void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
3846 CHECK(GetGcMap(ref) == NULL);
3847 gc_maps_[ref] = &gc_map;
3848 CHECK(GetGcMap(ref) != NULL);
3849}
3850
3851const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3852 GcMapTable::const_iterator it = gc_maps_.find(ref);
3853 if (it == gc_maps_.end()) {
3854 return NULL;
3855 }
3856 CHECK(it->second != NULL);
3857 return it->second;
3858}
3859
3860void DexVerifier::DeleteGcMaps() {
3861 STLDeleteValues(&gc_maps_);
3862}
3863
Ian Rogersd81871c2011-10-03 13:57:23 -07003864} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003865} // namespace art