blob: 6cb7104a69863db6afd5838eaa7ce9bc2848f5cd [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
Elliott Hughes1f359b02011-07-17 14:27:17 -07005#include <iostream>
6
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_linker.h"
jeffhaob4df5142011-09-19 20:25:32 -07008#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
10#include "dex_instruction.h"
11#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070012#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070013#include "intern_table.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070014#include "logging.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070016#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017
18namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070019namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
Ian Rogers2c8a8572011-10-24 17:11:36 -070021static const bool gDebugVerify = false;
22
Ian Rogersd81871c2011-10-03 13:57:23 -070023std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
24 return os << (int)rhs;
25}
jeffhaobdb76512011-09-07 11:43:16 -070026
Ian Rogers84fa0742011-10-25 18:13:30 -070027static const char* type_strings[] = {
28 "Unknown",
29 "Conflict",
30 "Boolean",
31 "Byte",
32 "Short",
33 "Char",
34 "Integer",
35 "Float",
36 "Long (Low Half)",
37 "Long (High Half)",
38 "Double (Low Half)",
39 "Double (High Half)",
40 "64-bit Constant (Low Half)",
41 "64-bit Constant (High Half)",
42 "32-bit Constant",
43 "Unresolved Reference",
44 "Uninitialized Reference",
45 "Uninitialized This Reference",
46 "Unresolved And Uninitialized This Reference",
47 "Reference",
48};
Ian Rogersd81871c2011-10-03 13:57:23 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070051 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
52 std::string result;
53 if (IsConstant()) {
54 uint32_t val = ConstantValue();
55 if (val == 0) {
56 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070057 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070058 if(IsConstantShort()) {
59 result = StringPrintf("32-bit Constant: %d", val);
60 } else {
61 result = StringPrintf("32-bit Constant: 0x%x", val);
62 }
63 }
64 } else {
65 result = type_strings[type_];
66 if (IsReferenceTypes()) {
67 result += ": ";
68 if (IsUnresolvedReference()) {
69 result += PrettyDescriptor(GetDescriptor());
70 } else {
71 result += PrettyDescriptor(GetClass()->GetDescriptor());
72 }
Ian Rogersd81871c2011-10-03 13:57:23 -070073 }
74 }
Ian Rogers84fa0742011-10-25 18:13:30 -070075 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070076}
77
78const RegType& RegType::HighHalf(RegTypeCache* cache) const {
79 CHECK(IsLowHalf());
80 if (type_ == kRegTypeLongLo) {
81 return cache->FromType(kRegTypeLongHi);
82 } else if (type_ == kRegTypeDoubleLo) {
83 return cache->FromType(kRegTypeDoubleHi);
84 } else {
85 return cache->FromType(kRegTypeConstHi);
86 }
87}
88
89/*
90 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
91 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
92 * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J
93 * is the deepest (lowest upper bound) parent of S and T).
94 *
95 * This operation applies for regular classes and arrays, however, for interface types there needn't
96 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
97 * introducing sets of types, however, the only operation permissible on an interface is
98 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
99 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700100 * the perversion of any Object being assignable to an interface type (note, however, that we don't
101 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
102 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700103 */
104Class* RegType::ClassJoin(Class* s, Class* t) {
105 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
106 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
107 if (s == t) {
108 return s;
109 } else if (s->IsAssignableFrom(t)) {
110 return s;
111 } else if (t->IsAssignableFrom(s)) {
112 return t;
113 } else if (s->IsArrayClass() && t->IsArrayClass()) {
114 Class* s_ct = s->GetComponentType();
115 Class* t_ct = t->GetComponentType();
116 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
117 // Given the types aren't the same, if either array is of primitive types then the only
118 // common parent is java.lang.Object
119 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
120 DCHECK(result->IsObjectClass());
121 return result;
122 }
123 Class* common_elem = ClassJoin(s_ct, t_ct);
124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
125 const ClassLoader* class_loader = s->GetClassLoader();
126 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
127 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
128 DCHECK(array_class != NULL);
129 return array_class;
130 } else {
131 size_t s_depth = s->Depth();
132 size_t t_depth = t->Depth();
133 // Get s and t to the same depth in the hierarchy
134 if (s_depth > t_depth) {
135 while (s_depth > t_depth) {
136 s = s->GetSuperClass();
137 s_depth--;
138 }
139 } else {
140 while (t_depth > s_depth) {
141 t = t->GetSuperClass();
142 t_depth--;
143 }
144 }
145 // Go up the hierarchy until we get to the common parent
146 while (s != t) {
147 s = s->GetSuperClass();
148 t = t->GetSuperClass();
149 }
150 return s;
151 }
152}
153
Ian Rogersb5e95b92011-10-25 23:28:55 -0700154bool RegType::IsAssignableFrom(const RegType& src) const {
155 if (Equals(src)) {
156 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700157 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700158 switch (GetType()) {
159 case RegType::kRegTypeBoolean: return IsBooleanTypes();
160 case RegType::kRegTypeByte: return IsByteTypes();
161 case RegType::kRegTypeShort: return IsShortTypes();
162 case RegType::kRegTypeChar: return IsCharTypes();
163 case RegType::kRegTypeInteger: return IsIntegralTypes();
164 case RegType::kRegTypeFloat: return IsFloatTypes();
165 case RegType::kRegTypeLongLo: return IsLongTypes();
166 case RegType::kRegTypeDoubleLo: return IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700167 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700168 if (!IsReferenceTypes()) {
169 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700170 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700171 if (src.IsZero()) {
172 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700173 } else if (IsUninitializedReference()) {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700174 return false; // Nonsensical to Join two uninitialized classes
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700175 } else if (GetClass()->IsInterface()) {
176 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogersb5e95b92011-10-25 23:28:55 -0700177 } else if (IsReference() && src.IsReference() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700178 GetClass()->IsAssignableFrom(src.GetClass())) {
179 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700180 return true;
181 } else if (src.IsUnresolvedReference() && IsReference() && GetClass()->IsObjectClass()) {
182 // We're an object being assigned an unresolved reference, which is ok
183 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700184 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700185 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 }
188 }
189}
190
Ian Rogers84fa0742011-10-25 18:13:30 -0700191static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
192 return a.IsConstant() ? b : a;
193}
jeffhaobdb76512011-09-07 11:43:16 -0700194
Ian Rogersd81871c2011-10-03 13:57:23 -0700195const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
196 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700197 if (IsUnknown() && incoming_type.IsUnknown()) {
198 return *this; // Unknown MERGE Unknown => Unknown
199 } else if (IsConflict()) {
200 return *this; // Conflict MERGE * => Conflict
201 } else if (incoming_type.IsConflict()) {
202 return incoming_type; // * MERGE Conflict => Conflict
203 } else if (IsUnknown() || incoming_type.IsUnknown()) {
204 return reg_types->Conflict(); // Unknown MERGE * => Conflict
205 } else if(IsConstant() && incoming_type.IsConstant()) {
206 int32_t val1 = ConstantValue();
207 int32_t val2 = incoming_type.ConstantValue();
208 if (val1 >= 0 && val2 >= 0) {
209 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
210 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700211 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700212 } else {
213 return incoming_type;
214 }
215 } else if (val1 < 0 && val2 < 0) {
216 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
217 if (val1 <= val2) {
218 return *this;
219 } else {
220 return incoming_type;
221 }
222 } else {
223 // Values are +ve and -ve, choose smallest signed type in which they both fit
224 if (IsConstantByte()) {
225 if (incoming_type.IsConstantByte()) {
226 return reg_types->ByteConstant();
227 } else if (incoming_type.IsConstantShort()) {
228 return reg_types->ShortConstant();
229 } else {
230 return reg_types->IntConstant();
231 }
232 } else if (IsConstantShort()) {
233 if (incoming_type.IsShort()) {
234 return reg_types->ShortConstant();
235 } else {
236 return reg_types->IntConstant();
237 }
238 } else {
239 return reg_types->IntConstant();
240 }
241 }
242 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
243 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
244 return reg_types->Boolean(); // boolean MERGE boolean => boolean
245 }
246 if (IsByteTypes() && incoming_type.IsByteTypes()) {
247 return reg_types->Byte(); // byte MERGE byte => byte
248 }
249 if (IsShortTypes() && incoming_type.IsShortTypes()) {
250 return reg_types->Short(); // short MERGE short => short
251 }
252 if (IsCharTypes() && incoming_type.IsCharTypes()) {
253 return reg_types->Char(); // char MERGE char => char
254 }
255 return reg_types->Integer(); // int MERGE * => int
256 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
257 (IsLongTypes() && incoming_type.IsLongTypes()) ||
258 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
259 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
260 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
261 // check constant case was handled prior to entry
262 DCHECK(!IsConstant() || !incoming_type.IsConstant());
263 // float/long/double MERGE float/long/double_constant => float/long/double
264 return SelectNonConstant(*this, incoming_type);
265 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
266 if (IsZero() | incoming_type.IsZero()) {
267 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
268 } else if (IsUnresolvedReference() || IsUninitializedReference() ||
269 IsUninitializedThisReference()) {
270 // Can only merge an uninitialized or unresolved type with itself or 0, we've already checked
271 // these so => Conflict
272 return reg_types->Conflict();
273 } else { // Two reference types, compute Join
274 Class* c1 = GetClass();
275 Class* c2 = incoming_type.GetClass();
276 DCHECK(c1 != NULL && !c1->IsPrimitive());
277 DCHECK(c2 != NULL && !c2->IsPrimitive());
278 Class* join_class = ClassJoin(c1, c2);
279 if (c1 == join_class) {
280 return *this;
281 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700282 return incoming_type;
283 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700284 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700285 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700287 } else {
288 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700289 }
290}
291
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700292static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700293 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700294 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
295 case Primitive::kPrimByte: return RegType::kRegTypeByte;
296 case Primitive::kPrimShort: return RegType::kRegTypeShort;
297 case Primitive::kPrimChar: return RegType::kRegTypeChar;
298 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
299 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
300 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
301 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
302 case Primitive::kPrimVoid:
303 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700304 }
305}
306
307static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
308 if (descriptor.length() == 1) {
309 switch (descriptor[0]) {
310 case 'Z': return RegType::kRegTypeBoolean;
311 case 'B': return RegType::kRegTypeByte;
312 case 'S': return RegType::kRegTypeShort;
313 case 'C': return RegType::kRegTypeChar;
314 case 'I': return RegType::kRegTypeInteger;
315 case 'J': return RegType::kRegTypeLongLo;
316 case 'F': return RegType::kRegTypeFloat;
317 case 'D': return RegType::kRegTypeDoubleLo;
318 case 'V':
319 default: return RegType::kRegTypeUnknown;
320 }
321 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
322 return RegType::kRegTypeReference;
323 } else {
324 return RegType::kRegTypeUnknown;
325 }
326}
327
328std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700329 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700330 return os;
331}
332
333const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
334 const std::string& descriptor) {
335 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
336}
337
338const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
339 const std::string& descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700340 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700341 // entries should be sized greater than primitive types
342 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
343 RegType* entry = entries_[type];
344 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700345 Class* klass = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -0700346 if (descriptor.size() != 0) {
347 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
348 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700349 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 entries_[type] = entry;
351 }
352 return *entry;
353 } else {
354 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers84fa0742011-10-25 18:13:30 -0700355 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700356 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700357 // check resolved and unresolved references, ignore uninitialized references
358 if (cur_entry->IsReference() && cur_entry->GetClass()->GetDescriptor()->Equals(descriptor)) {
359 return *cur_entry;
360 } else if (cur_entry->IsUnresolvedReference() &&
361 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 return *cur_entry;
363 }
364 }
365 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700366 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700367 // Able to resolve so create resolved register type
368 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700369 entries_.push_back(entry);
370 return *entry;
371 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700372 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700373 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700374 Thread::Current()->ClearException();
375 String* string_descriptor =
376 Runtime::Current()->GetInternTable()->InternStrong(descriptor.c_str());
377 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
378 entries_.size());
379 entries_.push_back(entry);
380 return *entry;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700381 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700382 }
383}
384
385const RegType& RegTypeCache::FromClass(Class* klass) {
386 if (klass->IsPrimitive()) {
387 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
388 // entries should be sized greater than primitive types
389 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
390 RegType* entry = entries_[type];
391 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700392 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 entries_[type] = entry;
394 }
395 return *entry;
396 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700397 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700398 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700399 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 return *cur_entry;
401 }
402 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700403 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 entries_.push_back(entry);
405 return *entry;
406 }
407}
408
409const RegType& RegTypeCache::Uninitialized(Class* klass, uint32_t allocation_pc) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700410 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700412 if (cur_entry->IsUninitializedReference() && cur_entry->GetAllocationPc() == allocation_pc &&
413 cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700414 return *cur_entry;
415 }
416 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700417 RegType* entry = new RegType(RegType::kRegTypeUninitializedReference, klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 entries_.push_back(entry);
419 return *entry;
420}
421
422const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700423 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 RegType* cur_entry = entries_[i];
425 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
426 return *cur_entry;
427 }
428 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700429 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 entries_.size());
431 entries_.push_back(entry);
432 return *entry;
433}
434
435const RegType& RegTypeCache::FromType(RegType::Type type) {
436 CHECK(type < RegType::kRegTypeReference);
437 switch (type) {
438 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
439 case RegType::kRegTypeByte: return From(type, NULL, "B");
440 case RegType::kRegTypeShort: return From(type, NULL, "S");
441 case RegType::kRegTypeChar: return From(type, NULL, "C");
442 case RegType::kRegTypeInteger: return From(type, NULL, "I");
443 case RegType::kRegTypeFloat: return From(type, NULL, "F");
444 case RegType::kRegTypeLongLo:
445 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
446 case RegType::kRegTypeDoubleLo:
447 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
448 default: return From(type, NULL, "");
449 }
450}
451
452const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700453 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
454 RegType* cur_entry = entries_[i];
455 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
456 return *cur_entry;
457 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700459 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
460 entries_.push_back(entry);
461 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700462}
463
464bool RegisterLine::CheckConstructorReturn() const {
465 for (size_t i = 0; i < num_regs_; i++) {
466 if (GetRegisterType(i).IsUninitializedThisReference()) {
467 verifier_->Fail(VERIFY_ERROR_GENERIC)
468 << "Constructor returning without calling superclass constructor";
469 return false;
470 }
471 }
472 return true;
473}
474
475void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
476 DCHECK(vdst < num_regs_);
477 if (new_type.IsLowHalf()) {
478 line_[vdst] = new_type.GetId();
479 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
480 } else if (new_type.IsHighHalf()) {
481 /* should never set these explicitly */
482 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
483 } else if (new_type.IsConflict()) { // should only be set during a merge
484 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
485 } else {
486 line_[vdst] = new_type.GetId();
487 }
488 // Clear the monitor entry bits for this register.
489 ClearAllRegToLockDepths(vdst);
490}
491
492void RegisterLine::SetResultTypeToUnknown() {
493 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
494 result_[0] = unknown_id;
495 result_[1] = unknown_id;
496}
497
498void RegisterLine::SetResultRegisterType(const RegType& new_type) {
499 result_[0] = new_type.GetId();
500 if(new_type.IsLowHalf()) {
501 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
502 result_[1] = new_type.GetId() + 1;
503 } else {
504 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
505 }
506}
507
508const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
509 // The register index was validated during the static pass, so we don't need to check it here.
510 DCHECK_LT(vsrc, num_regs_);
511 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
512}
513
514const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
515 if (dec_insn.vA_ < 1) {
516 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
517 return verifier_->GetRegTypeCache()->Unknown();
518 }
519 /* get the element type of the array held in vsrc */
520 const RegType& this_type = GetRegisterType(dec_insn.vC_);
521 if (!this_type.IsReferenceTypes()) {
522 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
523 << dec_insn.vC_ << " (type=" << this_type << ")";
524 return verifier_->GetRegTypeCache()->Unknown();
525 }
526 return this_type;
527}
528
529Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
530 /* get the element type of the array held in vsrc */
531 const RegType& type = GetRegisterType(vsrc);
532 /* if "always zero", we allow it to fail at runtime */
533 if (type.IsZero()) {
534 return NULL;
535 } else if (!type.IsReferenceTypes()) {
536 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
537 << " (type=" << type << ")";
538 return NULL;
539 } else if (type.IsUninitializedReference()) {
540 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
541 return NULL;
542 } else {
543 return type.GetClass();
544 }
545}
546
547bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
548 // Verify the src register type against the check type refining the type of the register
549 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700550 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
552 << " but expected " << check_type;
553 return false;
554 }
555 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
556 // precise than the subtype in vsrc so leave it for reference types. For primitive types
557 // if they are a defined type then they are as precise as we can get, however, for constant
558 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
559 return true;
560}
561
562void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
563 Class* klass = uninit_type.GetClass();
564 if (klass == NULL) {
565 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Unable to find type=" << uninit_type;
566 } else {
567 const RegType& init_type = verifier_->GetRegTypeCache()->FromClass(klass);
568 size_t changed = 0;
569 for (size_t i = 0; i < num_regs_; i++) {
570 if (GetRegisterType(i).Equals(uninit_type)) {
571 line_[i] = init_type.GetId();
572 changed++;
573 }
574 }
575 DCHECK_GT(changed, 0u);
576 }
577}
578
579void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
580 for (size_t i = 0; i < num_regs_; i++) {
581 if (GetRegisterType(i).Equals(uninit_type)) {
582 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
583 ClearAllRegToLockDepths(i);
584 }
585 }
586}
587
588void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
589 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
590 const RegType& type = GetRegisterType(vsrc);
591 SetRegisterType(vdst, type);
592 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
593 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
594 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
595 << " cat=" << static_cast<int>(cat);
596 } else if (cat == kTypeCategoryRef) {
597 CopyRegToLockDepth(vdst, vsrc);
598 }
599}
600
601void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
602 const RegType& type_l = GetRegisterType(vsrc);
603 const RegType& type_h = GetRegisterType(vsrc + 1);
604
605 if (!type_l.CheckWidePair(type_h)) {
606 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
607 << " type=" << type_l << "/" << type_h;
608 } else {
609 SetRegisterType(vdst, type_l); // implicitly sets the second half
610 }
611}
612
613void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
614 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
615 if ((!is_reference && !type.IsCategory1Types()) ||
616 (is_reference && !type.IsReferenceTypes())) {
617 verifier_->Fail(VERIFY_ERROR_GENERIC)
618 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
619 } else {
620 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
621 SetRegisterType(vdst, type);
622 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
623 }
624}
625
626/*
627 * Implement "move-result-wide". Copy the category-2 value from the result
628 * register to another register, and reset the result register.
629 */
630void RegisterLine::CopyResultRegister2(uint32_t vdst) {
631 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
632 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
633 if (!type_l.IsCategory2Types()) {
634 verifier_->Fail(VERIFY_ERROR_GENERIC)
635 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
636 } else {
637 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
638 SetRegisterType(vdst, type_l); // also sets the high
639 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
640 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
641 }
642}
643
644void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
645 const RegType& dst_type, const RegType& src_type) {
646 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
647 SetRegisterType(dec_insn.vA_, dst_type);
648 }
649}
650
651void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
652 const RegType& dst_type,
653 const RegType& src_type1, const RegType& src_type2,
654 bool check_boolean_op) {
655 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
656 VerifyRegisterType(dec_insn.vC_, src_type2)) {
657 if (check_boolean_op) {
658 DCHECK(dst_type.IsInteger());
659 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
660 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
661 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
662 return;
663 }
664 }
665 SetRegisterType(dec_insn.vA_, dst_type);
666 }
667}
668
669void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
670 const RegType& dst_type, const RegType& src_type1,
671 const RegType& src_type2, bool check_boolean_op) {
672 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
673 VerifyRegisterType(dec_insn.vB_, src_type2)) {
674 if (check_boolean_op) {
675 DCHECK(dst_type.IsInteger());
676 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
677 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
678 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
679 return;
680 }
681 }
682 SetRegisterType(dec_insn.vA_, dst_type);
683 }
684}
685
686void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
687 const RegType& dst_type, const RegType& src_type,
688 bool check_boolean_op) {
689 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
690 if (check_boolean_op) {
691 DCHECK(dst_type.IsInteger());
692 /* check vB with the call, then check the constant manually */
693 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
694 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
695 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
696 return;
697 }
698 }
699 SetRegisterType(dec_insn.vA_, dst_type);
700 }
701}
702
703void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
704 const RegType& reg_type = GetRegisterType(reg_idx);
705 if (!reg_type.IsReferenceTypes()) {
706 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
707 } else {
708 SetRegToLockDepth(reg_idx, monitors_.size());
709 monitors_.push(insn_idx);
710 }
711}
712
713void RegisterLine::PopMonitor(uint32_t reg_idx) {
714 const RegType& reg_type = GetRegisterType(reg_idx);
715 if (!reg_type.IsReferenceTypes()) {
716 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
717 } else if (monitors_.empty()) {
718 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
719 } else {
720 monitors_.pop();
721 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
722 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
723 // format "036" the constant collector may create unlocks on the same object but referenced
724 // via different registers.
725 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
726 : verifier_->LogVerifyInfo())
727 << "monitor-exit not unlocking the top of the monitor stack";
728 } else {
729 // Record the register was unlocked
730 ClearRegToLockDepth(reg_idx, monitors_.size());
731 }
732 }
733}
734
735bool RegisterLine::VerifyMonitorStackEmpty() {
736 if (MonitorStackDepth() != 0) {
737 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
738 return false;
739 } else {
740 return true;
741 }
742}
743
744bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
745 bool changed = false;
746 for (size_t idx = 0; idx < num_regs_; idx++) {
747 if (line_[idx] != incoming_line->line_[idx]) {
748 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
749 const RegType& cur_type = GetRegisterType(idx);
750 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
751 changed = changed || !cur_type.Equals(new_type);
752 line_[idx] = new_type.GetId();
753 }
754 }
755 if(monitors_ != incoming_line->monitors_) {
756 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
757 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
758 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
759 for (uint32_t idx = 0; idx < num_regs_; idx++) {
760 size_t depths = reg_to_lock_depths_.count(idx);
761 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
762 if (depths != incoming_depths) {
763 if (depths == 0 || incoming_depths == 0) {
764 reg_to_lock_depths_.erase(idx);
765 } else {
766 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
767 << ": " << depths << " != " << incoming_depths;
768 break;
769 }
770 }
771 }
772 }
773 return changed;
774}
775
776void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
777 for (size_t i = 0; i < num_regs_; i += 8) {
778 uint8_t val = 0;
779 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
780 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700781 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 val |= 1 << j;
783 }
784 }
785 if (val != 0) {
786 DCHECK_LT(i / 8, max_bytes);
787 data[i / 8] = val;
788 }
789 }
790}
791
792std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700793 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 return os;
795}
796
797
798void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
799 uint32_t insns_size, uint16_t registers_size,
800 DexVerifier* verifier) {
801 DCHECK_GT(insns_size, 0U);
802
803 for (uint32_t i = 0; i < insns_size; i++) {
804 bool interesting = false;
805 switch (mode) {
806 case kTrackRegsAll:
807 interesting = flags[i].IsOpcode();
808 break;
809 case kTrackRegsGcPoints:
810 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
811 break;
812 case kTrackRegsBranches:
813 interesting = flags[i].IsBranchTarget();
814 break;
815 default:
816 break;
817 }
818 if (interesting) {
819 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
820 }
821 }
822}
823
824bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700825 if (klass->IsVerified()) {
826 return true;
827 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 Class* super = klass->GetSuperClass();
829 if (super == NULL && !klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
830 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
831 return false;
832 }
833 if (super != NULL) {
834 if (!super->IsVerified() && !super->IsErroneous()) {
835 Runtime::Current()->GetClassLinker()->VerifyClass(super);
836 }
837 if (!super->IsVerified()) {
838 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
839 << " that attempts to sub-class corrupt class " << PrettyClass(super);
840 return false;
841 } else if (super->IsFinal()) {
842 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
843 << " that attempts to sub-class final class " << PrettyClass(super);
844 return false;
845 }
846 }
jeffhaobdb76512011-09-07 11:43:16 -0700847 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
848 Method* method = klass->GetDirectMethod(i);
849 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
851 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700852 return false;
853 }
854 }
855 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
856 Method* method = klass->GetVirtualMethod(i);
857 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
859 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700860 return false;
861 }
862 }
863 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700864}
865
jeffhaobdb76512011-09-07 11:43:16 -0700866bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 DexVerifier verifier(method);
868 bool success = verifier.Verify();
869 // We expect either success and no verification error, or failure and a generic failure to
870 // reject the class.
871 if (success) {
872 if (verifier.failure_ != VERIFY_ERROR_NONE) {
873 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
874 << verifier.fail_messages_;
875 }
876 } else {
877 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700878 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700879 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700880 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700881 verifier.Dump(std::cout);
882 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
884 }
885 return success;
886}
887
888DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
889 method_(method), failure_(VERIFY_ERROR_NONE),
890 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700891 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 dex_file_ = &class_linker->FindDexFile(dex_cache);
894 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700895}
896
Ian Rogersd81871c2011-10-03 13:57:23 -0700897bool DexVerifier::Verify() {
898 // If there aren't any instructions, make sure that's expected, then exit successfully.
899 if (code_item_ == NULL) {
900 if (!method_->IsNative() && !method_->IsAbstract()) {
901 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700902 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 } else {
904 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700905 }
jeffhaobdb76512011-09-07 11:43:16 -0700906 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
908 if (code_item_->ins_size_ > code_item_->registers_size_) {
909 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
910 << " regs=" << code_item_->registers_size_;
911 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700912 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 // Allocate and initialize an array to hold instruction data.
914 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
915 // Run through the instructions and see if the width checks out.
916 bool result = ComputeWidthsAndCountOps();
917 // Flag instructions guarded by a "try" block and check exception handlers.
918 result = result && ScanTryCatchBlocks();
919 // Perform static instruction verification.
920 result = result && VerifyInstructions();
921 // Perform code flow analysis.
922 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700923 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924}
925
Ian Rogersd81871c2011-10-03 13:57:23 -0700926bool DexVerifier::ComputeWidthsAndCountOps() {
927 const uint16_t* insns = code_item_->insns_;
928 size_t insns_size = code_item_->insns_size_in_code_units_;
929 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700930 size_t new_instance_count = 0;
931 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700933
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700935 Instruction::Code opcode = inst->Opcode();
936 if (opcode == Instruction::NEW_INSTANCE) {
937 new_instance_count++;
938 } else if (opcode == Instruction::MONITOR_ENTER) {
939 monitor_enter_count++;
940 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 size_t inst_size = inst->SizeInCodeUnits();
942 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
943 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700944 inst = inst->Next();
945 }
946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 if (dex_pc != insns_size) {
948 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
949 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700950 return false;
951 }
952
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 new_instance_count_ = new_instance_count;
954 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700955 return true;
956}
957
Ian Rogersd81871c2011-10-03 13:57:23 -0700958bool DexVerifier::ScanTryCatchBlocks() {
959 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700960 if (tries_size == 0) {
961 return true;
962 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 uint32_t insns_size = code_item_->insns_size_in_code_units_;
964 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700965
966 for (uint32_t idx = 0; idx < tries_size; idx++) {
967 const DexFile::TryItem* try_item = &tries[idx];
968 uint32_t start = try_item->start_addr_;
969 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700970 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
972 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700973 return false;
974 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 if (!insn_flags_[start].IsOpcode()) {
976 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700977 return false;
978 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 for (uint32_t dex_pc = start; dex_pc < end;
980 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
981 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700982 }
983 }
jeffhaobdb76512011-09-07 11:43:16 -0700984 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700986 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
987 for (uint32_t idx = 0; idx < handlers_size; idx++) {
988 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700989 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 uint32_t dex_pc= iterator.Get().address_;
991 if (!insn_flags_[dex_pc].IsOpcode()) {
992 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700993 return false;
994 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -0700996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997 handlers_ptr = iterator.GetData();
998 }
jeffhaobdb76512011-09-07 11:43:16 -0700999 return true;
1000}
1001
Ian Rogersd81871c2011-10-03 13:57:23 -07001002bool DexVerifier::VerifyInstructions() {
1003 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001004
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 /* Flag the start of the method as a branch target. */
1006 insn_flags_[0].SetBranchTarget();
1007
1008 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1009 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1010 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001011 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1012 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
1015 /* Flag instructions that are garbage collection points */
1016 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1017 insn_flags_[dex_pc].SetGcPoint();
1018 }
1019 dex_pc += inst->SizeInCodeUnits();
1020 inst = inst->Next();
1021 }
1022 return true;
1023}
1024
1025bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1026 Instruction::DecodedInstruction dec_insn(inst);
1027 bool result = true;
1028 switch (inst->GetVerifyTypeArgumentA()) {
1029 case Instruction::kVerifyRegA:
1030 result = result && CheckRegisterIndex(dec_insn.vA_);
1031 break;
1032 case Instruction::kVerifyRegAWide:
1033 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1034 break;
1035 }
1036 switch (inst->GetVerifyTypeArgumentB()) {
1037 case Instruction::kVerifyRegB:
1038 result = result && CheckRegisterIndex(dec_insn.vB_);
1039 break;
1040 case Instruction::kVerifyRegBField:
1041 result = result && CheckFieldIndex(dec_insn.vB_);
1042 break;
1043 case Instruction::kVerifyRegBMethod:
1044 result = result && CheckMethodIndex(dec_insn.vB_);
1045 break;
1046 case Instruction::kVerifyRegBNewInstance:
1047 result = result && CheckNewInstance(dec_insn.vB_);
1048 break;
1049 case Instruction::kVerifyRegBString:
1050 result = result && CheckStringIndex(dec_insn.vB_);
1051 break;
1052 case Instruction::kVerifyRegBType:
1053 result = result && CheckTypeIndex(dec_insn.vB_);
1054 break;
1055 case Instruction::kVerifyRegBWide:
1056 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1057 break;
1058 }
1059 switch (inst->GetVerifyTypeArgumentC()) {
1060 case Instruction::kVerifyRegC:
1061 result = result && CheckRegisterIndex(dec_insn.vC_);
1062 break;
1063 case Instruction::kVerifyRegCField:
1064 result = result && CheckFieldIndex(dec_insn.vC_);
1065 break;
1066 case Instruction::kVerifyRegCNewArray:
1067 result = result && CheckNewArray(dec_insn.vC_);
1068 break;
1069 case Instruction::kVerifyRegCType:
1070 result = result && CheckTypeIndex(dec_insn.vC_);
1071 break;
1072 case Instruction::kVerifyRegCWide:
1073 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1074 break;
1075 }
1076 switch (inst->GetVerifyExtraFlags()) {
1077 case Instruction::kVerifyArrayData:
1078 result = result && CheckArrayData(code_offset);
1079 break;
1080 case Instruction::kVerifyBranchTarget:
1081 result = result && CheckBranchTarget(code_offset);
1082 break;
1083 case Instruction::kVerifySwitchTargets:
1084 result = result && CheckSwitchTargets(code_offset);
1085 break;
1086 case Instruction::kVerifyVarArg:
1087 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1088 break;
1089 case Instruction::kVerifyVarArgRange:
1090 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1091 break;
1092 case Instruction::kVerifyError:
1093 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1094 result = false;
1095 break;
1096 }
1097 return result;
1098}
1099
1100bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1101 if (idx >= code_item_->registers_size_) {
1102 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1103 << code_item_->registers_size_ << ")";
1104 return false;
1105 }
1106 return true;
1107}
1108
1109bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1110 if (idx + 1 >= code_item_->registers_size_) {
1111 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1112 << "+1 >= " << code_item_->registers_size_ << ")";
1113 return false;
1114 }
1115 return true;
1116}
1117
1118bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1119 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1120 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1121 << dex_file_->GetHeader().field_ids_size_ << ")";
1122 return false;
1123 }
1124 return true;
1125}
1126
1127bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1128 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1129 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1130 << dex_file_->GetHeader().method_ids_size_ << ")";
1131 return false;
1132 }
1133 return true;
1134}
1135
1136bool DexVerifier::CheckNewInstance(uint32_t idx) {
1137 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1138 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1139 << dex_file_->GetHeader().type_ids_size_ << ")";
1140 return false;
1141 }
1142 // We don't need the actual class, just a pointer to the class name.
1143 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1144 if (descriptor[0] != 'L') {
1145 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1146 return false;
1147 }
1148 return true;
1149}
1150
1151bool DexVerifier::CheckStringIndex(uint32_t idx) {
1152 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1153 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1154 << dex_file_->GetHeader().string_ids_size_ << ")";
1155 return false;
1156 }
1157 return true;
1158}
1159
1160bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1161 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1162 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1163 << dex_file_->GetHeader().type_ids_size_ << ")";
1164 return false;
1165 }
1166 return true;
1167}
1168
1169bool DexVerifier::CheckNewArray(uint32_t idx) {
1170 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1171 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1172 << dex_file_->GetHeader().type_ids_size_ << ")";
1173 return false;
1174 }
1175 int bracket_count = 0;
1176 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1177 const char* cp = descriptor;
1178 while (*cp++ == '[') {
1179 bracket_count++;
1180 }
1181 if (bracket_count == 0) {
1182 /* The given class must be an array type. */
1183 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1184 return false;
1185 } else if (bracket_count > 255) {
1186 /* It is illegal to create an array of more than 255 dimensions. */
1187 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1188 return false;
1189 }
1190 return true;
1191}
1192
1193bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1194 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1195 const uint16_t* insns = code_item_->insns_ + cur_offset;
1196 const uint16_t* array_data;
1197 int32_t array_data_offset;
1198
1199 DCHECK_LT(cur_offset, insn_count);
1200 /* make sure the start of the array data table is in range */
1201 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1202 if ((int32_t) cur_offset + array_data_offset < 0 ||
1203 cur_offset + array_data_offset + 2 >= insn_count) {
1204 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1205 << ", data offset " << array_data_offset << ", count " << insn_count;
1206 return false;
1207 }
1208 /* offset to array data table is a relative branch-style offset */
1209 array_data = insns + array_data_offset;
1210 /* make sure the table is 32-bit aligned */
1211 if ((((uint32_t) array_data) & 0x03) != 0) {
1212 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1213 << ", data offset " << array_data_offset;
1214 return false;
1215 }
1216 uint32_t value_width = array_data[1];
1217 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1218 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1219 /* make sure the end of the switch is in range */
1220 if (cur_offset + array_data_offset + table_size > insn_count) {
1221 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1222 << ", data offset " << array_data_offset << ", end "
1223 << cur_offset + array_data_offset + table_size
1224 << ", count " << insn_count;
1225 return false;
1226 }
1227 return true;
1228}
1229
1230bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1231 int32_t offset;
1232 bool isConditional, selfOkay;
1233 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1234 return false;
1235 }
1236 if (!selfOkay && offset == 0) {
1237 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1238 return false;
1239 }
1240 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1241 // identical "wrap-around" behavior, but it's unwise to depend on that.
1242 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1243 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1244 return false;
1245 }
1246 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1247 int32_t abs_offset = cur_offset + offset;
1248 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1249 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1250 << (void*) abs_offset << ") at " << (void*) cur_offset;
1251 return false;
1252 }
1253 insn_flags_[abs_offset].SetBranchTarget();
1254 return true;
1255}
1256
1257bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1258 bool* selfOkay) {
1259 const uint16_t* insns = code_item_->insns_ + cur_offset;
1260 *pConditional = false;
1261 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 switch (*insns & 0xff) {
1263 case Instruction::GOTO:
1264 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 break;
1266 case Instruction::GOTO_32:
1267 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 *selfOkay = true;
1269 break;
1270 case Instruction::GOTO_16:
1271 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001272 break;
1273 case Instruction::IF_EQ:
1274 case Instruction::IF_NE:
1275 case Instruction::IF_LT:
1276 case Instruction::IF_GE:
1277 case Instruction::IF_GT:
1278 case Instruction::IF_LE:
1279 case Instruction::IF_EQZ:
1280 case Instruction::IF_NEZ:
1281 case Instruction::IF_LTZ:
1282 case Instruction::IF_GEZ:
1283 case Instruction::IF_GTZ:
1284 case Instruction::IF_LEZ:
1285 *pOffset = (int16_t) insns[1];
1286 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001287 break;
1288 default:
1289 return false;
1290 break;
1291 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 return true;
1293}
1294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1296 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001297 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001299 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1301 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1302 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1303 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001304 return false;
1305 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001306 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 /* make sure the table is 32-bit aligned */
1309 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1311 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 return false;
1313 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 uint32_t switch_count = switch_insns[1];
1315 int32_t keys_offset, targets_offset;
1316 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1318 /* 0=sig, 1=count, 2/3=firstKey */
1319 targets_offset = 4;
1320 keys_offset = -1;
1321 expected_signature = Instruction::kPackedSwitchSignature;
1322 } else {
1323 /* 0=sig, 1=count, 2..count*2 = keys */
1324 keys_offset = 2;
1325 targets_offset = 2 + 2 * switch_count;
1326 expected_signature = Instruction::kSparseSwitchSignature;
1327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1331 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001332 return false;
1333 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 /* make sure the end of the switch is in range */
1335 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1337 << switch_offset << ", end "
1338 << (cur_offset + switch_offset + table_size)
1339 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 return false;
1341 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 /* for a sparse switch, verify the keys are in ascending order */
1343 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1345 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1347 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1348 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1350 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001351 return false;
1352 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 last_key = key;
1354 }
1355 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001356 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 for (uint32_t targ = 0; targ < switch_count; targ++) {
1358 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1359 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1360 int32_t abs_offset = cur_offset + offset;
1361 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1362 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1363 << (void*) abs_offset << ") at "
1364 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 return false;
1366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 insn_flags_[abs_offset].SetBranchTarget();
1368 }
1369 return true;
1370}
1371
1372bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1373 if (vA > 5) {
1374 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1375 return false;
1376 }
1377 uint16_t registers_size = code_item_->registers_size_;
1378 for (uint32_t idx = 0; idx < vA; idx++) {
1379 if (arg[idx] > registers_size) {
1380 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1381 << ") in non-range invoke (> " << registers_size << ")";
1382 return false;
1383 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001384 }
1385
1386 return true;
1387}
1388
Ian Rogersd81871c2011-10-03 13:57:23 -07001389bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1390 uint16_t registers_size = code_item_->registers_size_;
1391 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1392 // integer overflow when adding them here.
1393 if (vA + vC > registers_size) {
1394 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1395 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001396 return false;
1397 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001398 return true;
1399}
1400
Ian Rogersd81871c2011-10-03 13:57:23 -07001401bool DexVerifier::VerifyCodeFlow() {
1402 uint16_t registers_size = code_item_->registers_size_;
1403 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001404
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 if (registers_size * insns_size > 4*1024*1024) {
1406 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1407 << " insns_size=" << insns_size << ")";
1408 }
1409 /* Create and initialize table holding register status */
1410 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1411 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001412
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 work_line_.reset(new RegisterLine(registers_size, this));
1414 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001415
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 /* Initialize register types of method arguments. */
1417 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001418 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1419 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 return false;
1421 }
1422 /* Perform code flow verification. */
1423 if (!CodeFlowVerifyMethod()) {
1424 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001425 }
1426
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 /* Generate a register map and add it to the method. */
1428 ByteArray* map = GenerateGcMap();
1429 if (map == NULL) {
1430 return false; // Not a real failure, but a failure to encode
1431 }
1432 method_->SetGcMap(map);
1433#ifndef NDEBUG
1434 VerifyGcMap();
1435#endif
jeffhaobdb76512011-09-07 11:43:16 -07001436 return true;
1437}
1438
Ian Rogersd81871c2011-10-03 13:57:23 -07001439void DexVerifier::Dump(std::ostream& os) {
1440 if (method_->IsNative()) {
1441 os << "Native method" << std::endl;
1442 return;
jeffhaobdb76512011-09-07 11:43:16 -07001443 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 DCHECK(code_item_ != NULL);
1445 const Instruction* inst = Instruction::At(code_item_->insns_);
1446 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1447 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001448 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1449 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1451 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001452 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001453 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001454 inst = inst->Next();
1455 }
jeffhaobdb76512011-09-07 11:43:16 -07001456}
1457
Ian Rogersd81871c2011-10-03 13:57:23 -07001458static bool IsPrimitiveDescriptor(char descriptor) {
1459 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001460 case 'I':
1461 case 'C':
1462 case 'S':
1463 case 'B':
1464 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001465 case 'F':
1466 case 'D':
1467 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001469 default:
1470 return false;
1471 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001472}
1473
Ian Rogersd81871c2011-10-03 13:57:23 -07001474bool DexVerifier::SetTypesFromSignature() {
1475 RegisterLine* reg_line = reg_table_.GetLine(0);
1476 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1477 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001478
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1480 //Include the "this" pointer.
1481 size_t cur_arg = 0;
1482 if (!method_->IsStatic()) {
1483 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1484 // argument as uninitialized. This restricts field access until the superclass constructor is
1485 // called.
1486 Class* declaring_class = method_->GetDeclaringClass();
1487 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1488 reg_line->SetRegisterType(arg_start + cur_arg,
1489 reg_types_.UninitializedThisArgument(declaring_class));
1490 } else {
1491 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001492 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001494 }
1495
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1497 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1498
1499 for (; iterator.HasNext(); iterator.Next()) {
1500 const char* descriptor = iterator.GetDescriptor();
1501 if (descriptor == NULL) {
1502 LOG(FATAL) << "Null descriptor";
1503 }
1504 if (cur_arg >= expected_args) {
1505 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1506 << " args, found more (" << descriptor << ")";
1507 return false;
1508 }
1509 switch (descriptor[0]) {
1510 case 'L':
1511 case '[':
1512 // We assume that reference arguments are initialized. The only way it could be otherwise
1513 // (assuming the caller was verified) is if the current method is <init>, but in that case
1514 // it's effectively considered initialized the instant we reach here (in the sense that we
1515 // can return without doing anything or call virtual methods).
1516 {
1517 const RegType& reg_type =
1518 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001519 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 }
1521 break;
1522 case 'Z':
1523 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1524 break;
1525 case 'C':
1526 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1527 break;
1528 case 'B':
1529 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1530 break;
1531 case 'I':
1532 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1533 break;
1534 case 'S':
1535 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1536 break;
1537 case 'F':
1538 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1539 break;
1540 case 'J':
1541 case 'D': {
1542 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1543 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1544 cur_arg++;
1545 break;
1546 }
1547 default:
1548 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1549 return false;
1550 }
1551 cur_arg++;
1552 }
1553 if (cur_arg != expected_args) {
1554 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1555 return false;
1556 }
1557 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1558 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1559 // format. Only major difference from the method argument format is that 'V' is supported.
1560 bool result;
1561 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1562 result = descriptor[1] == '\0';
1563 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1564 size_t i = 0;
1565 do {
1566 i++;
1567 } while (descriptor[i] == '['); // process leading [
1568 if (descriptor[i] == 'L') { // object array
1569 do {
1570 i++; // find closing ;
1571 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1572 result = descriptor[i] == ';';
1573 } else { // primitive array
1574 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1575 }
1576 } else if (descriptor[0] == 'L') {
1577 // could be more thorough here, but shouldn't be required
1578 size_t i = 0;
1579 do {
1580 i++;
1581 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1582 result = descriptor[i] == ';';
1583 } else {
1584 result = false;
1585 }
1586 if (!result) {
1587 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1588 << descriptor << "'";
1589 }
1590 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001591}
1592
Ian Rogersd81871c2011-10-03 13:57:23 -07001593bool DexVerifier::CodeFlowVerifyMethod() {
1594 const uint16_t* insns = code_item_->insns_;
1595 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001596
jeffhaobdb76512011-09-07 11:43:16 -07001597 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 insn_flags_[0].SetChanged();
1599 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001600
jeffhaobdb76512011-09-07 11:43:16 -07001601 /* Continue until no instructions are marked "changed". */
1602 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1604 uint32_t insn_idx = start_guess;
1605 for (; insn_idx < insns_size; insn_idx++) {
1606 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
1608 }
jeffhaobdb76512011-09-07 11:43:16 -07001609 if (insn_idx == insns_size) {
1610 if (start_guess != 0) {
1611 /* try again, starting from the top */
1612 start_guess = 0;
1613 continue;
1614 } else {
1615 /* all flags are clear */
1616 break;
1617 }
1618 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 // We carry the working set of registers from instruction to instruction. If this address can
1620 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1621 // "changed" flags, we need to load the set of registers from the table.
1622 // Because we always prefer to continue on to the next instruction, we should never have a
1623 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1624 // target.
1625 work_insn_idx_ = insn_idx;
1626 if (insn_flags_[insn_idx].IsBranchTarget()) {
1627 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001628 } else {
1629#ifndef NDEBUG
1630 /*
1631 * Sanity check: retrieve the stored register line (assuming
1632 * a full table) and make sure it actually matches.
1633 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1635 if (register_line != NULL) {
1636 if (work_line_->CompareLine(register_line) != 0) {
1637 Dump(std::cout);
1638 std::cout << info_messages_.str();
1639 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1640 << "@" << (void*)work_insn_idx_ << std::endl
1641 << " work_line=" << *work_line_ << std::endl
1642 << " expected=" << *register_line;
1643 }
jeffhaobdb76512011-09-07 11:43:16 -07001644 }
1645#endif
1646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 if (!CodeFlowVerifyInstruction(&start_guess)) {
1648 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001649 return false;
1650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 insn_flags_[insn_idx].SetVisited();
1653 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001654 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001655
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001657 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001658 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001659 * (besides the wasted space), but it indicates a flaw somewhere
1660 * down the line, possibly in the verifier.
1661 *
1662 * If we've substituted "always throw" instructions into the stream,
1663 * we are almost certainly going to have some dead code.
1664 */
1665 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 uint32_t insn_idx = 0;
1667 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001668 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001670 * may or may not be preceded by a padding NOP (for alignment).
1671 */
1672 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1673 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1674 insns[insn_idx] == Instruction::kArrayDataSignature ||
1675 (insns[insn_idx] == Instruction::NOP &&
1676 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1677 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1678 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
1681
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001683 if (dead_start < 0)
1684 dead_start = insn_idx;
1685 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001687 dead_start = -1;
1688 }
1689 }
1690 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001692 }
1693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 return true;
1695}
1696
Ian Rogersd81871c2011-10-03 13:57:23 -07001697bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001698#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001700 gDvm.verifierStats.instrsReexamined++;
1701 } else {
1702 gDvm.verifierStats.instrsExamined++;
1703 }
1704#endif
1705
1706 /*
1707 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001708 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001709 * control to another statement:
1710 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001711 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001712 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001713 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001714 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001715 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001716 * throw an exception that is handled by an encompassing "try"
1717 * block.
1718 *
1719 * We can also return, in which case there is no successor instruction
1720 * from this point.
1721 *
1722 * The behavior can be determined from the OpcodeFlags.
1723 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1725 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001726 Instruction::DecodedInstruction dec_insn(inst);
1727 int opcode_flag = inst->Flag();
1728
jeffhaobdb76512011-09-07 11:43:16 -07001729 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001730 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001731 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001733 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1734 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 }
jeffhaobdb76512011-09-07 11:43:16 -07001736
1737 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001738 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001739 * can throw an exception, we will copy/merge this into the "catch"
1740 * address rather than work_line, because we don't want the result
1741 * from the "successful" code path (e.g. a check-cast that "improves"
1742 * a type) to be visible to the exception handler.
1743 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1745 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001746 } else {
1747#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001749#endif
1750 }
1751
1752 switch (dec_insn.opcode_) {
1753 case Instruction::NOP:
1754 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001755 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001756 * a signature that looks like a NOP; if we see one of these in
1757 * the course of executing code then we have a problem.
1758 */
1759 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001761 }
1762 break;
1763
1764 case Instruction::MOVE:
1765 case Instruction::MOVE_FROM16:
1766 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
1769 case Instruction::MOVE_WIDE:
1770 case Instruction::MOVE_WIDE_FROM16:
1771 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::MOVE_OBJECT:
1775 case Instruction::MOVE_OBJECT_FROM16:
1776 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001778 break;
1779
1780 /*
1781 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001782 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001783 * might want to hold the result in an actual CPU register, so the
1784 * Dalvik spec requires that these only appear immediately after an
1785 * invoke or filled-new-array.
1786 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001787 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001788 * redundant with the reset done below, but it can make the debug info
1789 * easier to read in some cases.)
1790 */
1791 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
1800
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001802 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 * This statement can only appear as the first instruction in an exception handler (though not
1804 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001805 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001806 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001808 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001810 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001812 }
1813 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 }
jeffhaobdb76512011-09-07 11:43:16 -07001815 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1817 if (!GetMethodReturnType().IsUnknown()) {
1818 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1819 }
jeffhaobdb76512011-09-07 11:43:16 -07001820 }
1821 break;
1822 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001824 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 const RegType& return_type = GetMethodReturnType();
1826 if (!return_type.IsCategory1Types()) {
1827 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1828 } else {
1829 // Compilers may generate synthetic functions that write byte values into boolean fields.
1830 // Also, it may use integer values for boolean, byte, short, and character return types.
1831 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1832 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1833 ((return_type.IsBoolean() || return_type.IsByte() ||
1834 return_type.IsShort() || return_type.IsChar()) &&
1835 src_type.IsInteger()));
1836 /* check the register contents */
1837 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1838 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001839 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 }
jeffhaobdb76512011-09-07 11:43:16 -07001841 }
1842 }
1843 break;
1844 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001846 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 const RegType& return_type = GetMethodReturnType();
1848 if (!return_type.IsCategory2Types()) {
1849 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1850 } else {
1851 /* check the register contents */
1852 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1853 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001854 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 }
1857 }
1858 break;
1859 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1861 const RegType& return_type = GetMethodReturnType();
1862 if (!return_type.IsReferenceTypes()) {
1863 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1864 } else {
1865 /* return_type is the *expected* return type, not register value */
1866 DCHECK(!return_type.IsZero());
1867 DCHECK(!return_type.IsUninitializedReference());
1868 // Verify that the reference in vAA is an instance of the type in "return_type". The Zero
1869 // type is allowed here. If the method is declared to return an interface, then any
1870 // initialized reference is acceptable.
1871 // Note GetClassFromRegister fails if the register holds an uninitialized reference, so
1872 // we do not allow them to be returned.
1873 Class* decl_class = return_type.GetClass();
1874 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
1875 if (res_class != NULL && failure_ == VERIFY_ERROR_NONE) {
1876 if (!decl_class->IsInterface() && !decl_class->IsAssignableFrom(res_class)) {
1877 Fail(VERIFY_ERROR_GENERIC) << "returning " << PrettyClassAndClassLoader(res_class)
1878 << ", declared " << PrettyClassAndClassLoader(res_class);
1879 }
jeffhaobdb76512011-09-07 11:43:16 -07001880 }
1881 }
1882 }
1883 break;
1884
1885 case Instruction::CONST_4:
1886 case Instruction::CONST_16:
1887 case Instruction::CONST:
1888 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001890 break;
1891 case Instruction::CONST_HIGH16:
1892 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 work_line_->SetRegisterType(dec_insn.vA_,
1894 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
1896 case Instruction::CONST_WIDE_16:
1897 case Instruction::CONST_WIDE_32:
1898 case Instruction::CONST_WIDE:
1899 case Instruction::CONST_WIDE_HIGH16:
1900 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903 case Instruction::CONST_STRING:
1904 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001908 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001910 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001912 fail_messages_ << "unable to resolve const-class " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 << " (" << bad_class_desc << ") in "
1914 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1915 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001916 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001918 }
1919 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 }
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
1924 case Instruction::MONITOR_EXIT:
1925 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001926 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001927 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001928 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001929 * to the need to handle asynchronous exceptions, a now-deprecated
1930 * feature that Dalvik doesn't support.)
1931 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001932 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001933 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001934 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001935 * structured locking checks are working, the former would have
1936 * failed on the -enter instruction, and the latter is impossible.
1937 *
1938 * This is fortunate, because issue 3221411 prevents us from
1939 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001941 * some catch blocks (which will show up as "dead" code when
1942 * we skip them here); if we can't, then the code path could be
1943 * "live" so we still need to check it.
1944 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 opcode_flag &= ~Instruction::kThrow;
1946 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001950 /*
1951 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001953 * we don't try to address it.)
1954 *
1955 * If it fails, an exception is thrown, which we deal with later
1956 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1957 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001959 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001961 fail_messages_ << "unable to resolve check-cast " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 << " (" << bad_class_desc << ") in "
1963 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1964 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001965 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1967 if (!orig_type.IsReferenceTypes()) {
1968 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1969 } else {
1970 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001971 }
jeffhaobdb76512011-09-07 11:43:16 -07001972 }
1973 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 }
1975 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001976 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1978 if (!tmp_type.IsReferenceTypes()) {
1979 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001980 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 /* make sure we can resolve the class; access check is important */
1982 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1983 if (res_class == NULL) {
1984 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001985 fail_messages_ << "unable to resolve instance of " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 << " (" << bad_class_desc << ") in "
1987 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1988 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1989 } else {
1990 /* result is boolean */
1991 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001994 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 }
1996 case Instruction::ARRAY_LENGTH: {
1997 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
1998 if (failure_ == VERIFY_ERROR_NONE) {
1999 if (res_class != NULL && !res_class->IsArrayClass()) {
2000 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
2001 } else {
2002 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2003 }
2004 }
2005 break;
2006 }
2007 case Instruction::NEW_INSTANCE: {
2008 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002009 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002011 fail_messages_ << "unable to resolve new-instance " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 << " (" << bad_class_desc << ") in "
2013 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2014 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2015 } else {
2016 /* can't create an instance of an interface or abstract class */
2017 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2018 Fail(VERIFY_ERROR_INSTANTIATION)
2019 << "new-instance on primitive, interface or abstract class"
2020 << PrettyDescriptor(res_class->GetDescriptor());
2021 } else {
2022 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2023 // Any registers holding previous allocations from this address that have not yet been
2024 // initialized must be marked invalid.
2025 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2026
2027 /* add the new uninitialized reference to the register state */
2028 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2029 }
2030 }
2031 break;
2032 }
2033 case Instruction::NEW_ARRAY: {
2034 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2035 if (res_class == NULL) {
2036 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002037 fail_messages_ << "unable to resolve new-array " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 << " (" << bad_class_desc << ") in "
2039 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2040 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002041 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002043 } else {
2044 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002046 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002048 }
2049 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 }
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2053 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002054 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002056 fail_messages_ << "unable to resolve filled-array " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 << " (" << bad_class_desc << ") in "
2058 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2059 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002060 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002062 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002063 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002064 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002066 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002068 just_set_result = true;
2069 }
2070 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 }
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::CMPL_FLOAT:
2073 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2075 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2076 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::CMPL_DOUBLE:
2079 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2081 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2082 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2086 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2087 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::THROW: {
2090 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2091 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2092 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2093 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2094 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002095 }
2096 }
2097 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 }
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::GOTO:
2100 case Instruction::GOTO_16:
2101 case Instruction::GOTO_32:
2102 /* no effect on or use of registers */
2103 break;
2104
2105 case Instruction::PACKED_SWITCH:
2106 case Instruction::SPARSE_SWITCH:
2107 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 case Instruction::FILL_ARRAY_DATA: {
2112 /* Similar to the verification done for APUT */
2113 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2114 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002115 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 if (res_class != NULL) {
2117 Class* component_type = res_class->GetComponentType();
2118 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2119 component_type->IsPrimitiveVoid()) {
2120 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2121 << PrettyDescriptor(res_class->GetDescriptor());
2122 } else {
2123 const RegType& value_type = reg_types_.FromClass(component_type);
2124 DCHECK(!value_type.IsUnknown());
2125 // Now verify if the element width in the table matches the element width declared in
2126 // the array
2127 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2128 if (array_data[0] != Instruction::kArrayDataSignature) {
2129 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2130 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002131 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 // Since we don't compress the data in Dex, expect to see equal width of data stored
2133 // in the table and expected from the array class.
2134 if (array_data[1] != elem_width) {
2135 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2136 << " vs " << elem_width << ")";
2137 }
2138 }
2139 }
jeffhaobdb76512011-09-07 11:43:16 -07002140 }
2141 }
2142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 }
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 case Instruction::IF_NE: {
2146 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2147 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2148 bool mismatch = false;
2149 if (reg_type1.IsZero()) { // zero then integral or reference expected
2150 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2151 } else if (reg_type1.IsReferenceTypes()) { // both references?
2152 mismatch = !reg_type2.IsReferenceTypes();
2153 } else { // both integral?
2154 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2155 }
2156 if (mismatch) {
2157 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2158 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002159 }
2160 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::IF_LT:
2163 case Instruction::IF_GE:
2164 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 case Instruction::IF_LE: {
2166 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2167 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2168 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2169 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2170 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002171 }
2172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 case Instruction::IF_NEZ: {
2176 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2177 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2178 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2179 }
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 }
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::IF_LTZ:
2183 case Instruction::IF_GEZ:
2184 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::IF_LEZ: {
2186 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2187 if (!reg_type.IsIntegralTypes()) {
2188 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2189 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2195 break;
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2198 break;
jeffhaobdb76512011-09-07 11:43:16 -07002199 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 VerifyAGet(dec_insn, reg_types_.Char(), true);
2201 break;
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 case Instruction::AGET:
2206 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2207 break;
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 VerifyAGet(dec_insn, reg_types_.Long(), true);
2210 break;
2211 case Instruction::AGET_OBJECT:
2212 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 case Instruction::APUT_BOOLEAN:
2216 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2217 break;
2218 case Instruction::APUT_BYTE:
2219 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2220 break;
2221 case Instruction::APUT_CHAR:
2222 VerifyAPut(dec_insn, reg_types_.Char(), true);
2223 break;
2224 case Instruction::APUT_SHORT:
2225 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
2236
jeffhaobdb76512011-09-07 11:43:16 -07002237 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002238 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 break;
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002241 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 break;
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002244 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 break;
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002247 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 break;
2249 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002250 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002253 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
2255 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002256 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 break;
jeffhaobdb76512011-09-07 11:43:16 -07002258
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002260 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 break;
2262 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002263 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 break;
2265 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002266 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 break;
2268 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002269 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002272 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
2274 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002275 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002278 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002282 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002285 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002288 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
2293 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002300 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
2302
2303 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002304 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
2306 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002307 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
2309 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002310 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
2312 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002322 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324
2325 case Instruction::INVOKE_VIRTUAL:
2326 case Instruction::INVOKE_VIRTUAL_RANGE:
2327 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 case Instruction::INVOKE_SUPER_RANGE: {
2329 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2330 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2331 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2332 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2333 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2334 if (failure_ == VERIFY_ERROR_NONE) {
2335 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2336 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002337 just_set_result = true;
2338 }
2339 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 }
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 case Instruction::INVOKE_DIRECT_RANGE: {
2343 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2344 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2345 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002346 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 * Some additional checks when calling a constructor. We know from the invocation arg check
2348 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2349 * that to require that called_method->klass is the same as this->klass or this->super,
2350 * allowing the latter only if the "this" argument is the same as the "this" argument to
2351 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002352 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 if (called_method->IsConstructor()) {
2354 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2355 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357
2358 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 if (this_type.IsZero()) {
2360 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002364 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002365
2366 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2368 if (this_class != method_->GetDeclaringClass()) {
2369 Fail(VERIFY_ERROR_GENERIC)
2370 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 }
2373 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 }
2377
2378 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002379 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2381 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383 }
2384
2385 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002386 * Replace the uninitialized reference with an initialized one. We need to do this for all
2387 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002388 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 work_line_->MarkRefsAsInitialized(this_type);
2390 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2394 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002395 just_set_result = true;
2396 }
2397 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 }
jeffhaobdb76512011-09-07 11:43:16 -07002399 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::INVOKE_STATIC_RANGE: {
2401 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2402 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2403 if (failure_ == VERIFY_ERROR_NONE) {
2404 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2405 work_line_->SetResultRegisterType(return_type);
2406 just_set_result = true;
2407 }
jeffhaobdb76512011-09-07 11:43:16 -07002408 }
2409 break;
2410 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 case Instruction::INVOKE_INTERFACE_RANGE: {
2412 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2413 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2414 if (failure_ == VERIFY_ERROR_NONE) {
2415 Class* called_interface = abs_method->GetDeclaringClass();
2416 if (!called_interface->IsInterface()) {
2417 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2418 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
jeffhaobdb76512011-09-07 11:43:16 -07002420 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 /* Get the type of the "this" arg, which should either be a sub-interface of called
2422 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002423 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2425 if (failure_ == VERIFY_ERROR_NONE) {
2426 if (this_type.IsZero()) {
2427 /* null pointer always passes (and always fails at runtime) */
2428 } else {
Ian Rogersb94a27b2011-10-26 00:33:41 -07002429 if (this_type.IsUninitializedTypes()) {
2430 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2431 << this_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 break;
2433 }
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002434 // In the past we have tried to assert that "called_interface" is assignable
2435 // from "this_type.GetClass()", however, as we do an imprecise Join
2436 // (RegType::JoinClass) we don't have full information on what interfaces are
2437 // implemented by "this_type". For example, two classes may implement the same
2438 // interfaces and have a common parent that doesn't implement the interface. The
2439 // join will set "this_type" to the parent class and a test that this implements
2440 // the interface will incorrectly fail.
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 }
jeffhaobdb76512011-09-07 11:43:16 -07002442 }
2443 }
jeffhaobdb76512011-09-07 11:43:16 -07002444 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 * We don't have an object instance, so we can't find the concrete method. However, all of
2446 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002447 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 const RegType& return_type = reg_types_.FromClass(abs_method->GetReturnType());
2449 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002450 just_set_result = true;
2451 }
2452 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 }
jeffhaobdb76512011-09-07 11:43:16 -07002454 case Instruction::NEG_INT:
2455 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::NEG_LONG:
2459 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002482 break;
2483 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002500 break;
2501 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002512 break;
2513
2514 case Instruction::ADD_INT:
2515 case Instruction::SUB_INT:
2516 case Instruction::MUL_INT:
2517 case Instruction::REM_INT:
2518 case Instruction::DIV_INT:
2519 case Instruction::SHL_INT:
2520 case Instruction::SHR_INT:
2521 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::AND_INT:
2525 case Instruction::OR_INT:
2526 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::ADD_LONG:
2530 case Instruction::SUB_LONG:
2531 case Instruction::MUL_LONG:
2532 case Instruction::DIV_LONG:
2533 case Instruction::REM_LONG:
2534 case Instruction::AND_LONG:
2535 case Instruction::OR_LONG:
2536 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
2539 case Instruction::SHL_LONG:
2540 case Instruction::SHR_LONG:
2541 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 /* shift distance is Int, making these different from other binary operations */
2543 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::ADD_FLOAT:
2546 case Instruction::SUB_FLOAT:
2547 case Instruction::MUL_FLOAT:
2548 case Instruction::DIV_FLOAT:
2549 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::ADD_DOUBLE:
2553 case Instruction::SUB_DOUBLE:
2554 case Instruction::MUL_DOUBLE:
2555 case Instruction::DIV_DOUBLE:
2556 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559 case Instruction::ADD_INT_2ADDR:
2560 case Instruction::SUB_INT_2ADDR:
2561 case Instruction::MUL_INT_2ADDR:
2562 case Instruction::REM_INT_2ADDR:
2563 case Instruction::SHL_INT_2ADDR:
2564 case Instruction::SHR_INT_2ADDR:
2565 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::AND_INT_2ADDR:
2569 case Instruction::OR_INT_2ADDR:
2570 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::ADD_LONG_2ADDR:
2577 case Instruction::SUB_LONG_2ADDR:
2578 case Instruction::MUL_LONG_2ADDR:
2579 case Instruction::DIV_LONG_2ADDR:
2580 case Instruction::REM_LONG_2ADDR:
2581 case Instruction::AND_LONG_2ADDR:
2582 case Instruction::OR_LONG_2ADDR:
2583 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::SHL_LONG_2ADDR:
2587 case Instruction::SHR_LONG_2ADDR:
2588 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::ADD_FLOAT_2ADDR:
2592 case Instruction::SUB_FLOAT_2ADDR:
2593 case Instruction::MUL_FLOAT_2ADDR:
2594 case Instruction::DIV_FLOAT_2ADDR:
2595 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
2598 case Instruction::ADD_DOUBLE_2ADDR:
2599 case Instruction::SUB_DOUBLE_2ADDR:
2600 case Instruction::MUL_DOUBLE_2ADDR:
2601 case Instruction::DIV_DOUBLE_2ADDR:
2602 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::ADD_INT_LIT16:
2606 case Instruction::RSUB_INT:
2607 case Instruction::MUL_INT_LIT16:
2608 case Instruction::DIV_INT_LIT16:
2609 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::AND_INT_LIT16:
2613 case Instruction::OR_INT_LIT16:
2614 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::ADD_INT_LIT8:
2618 case Instruction::RSUB_INT_LIT8:
2619 case Instruction::MUL_INT_LIT8:
2620 case Instruction::DIV_INT_LIT8:
2621 case Instruction::REM_INT_LIT8:
2622 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002624 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::AND_INT_LIT8:
2628 case Instruction::OR_INT_LIT8:
2629 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632
2633 /*
2634 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002635 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002636 * inserted in the course of verification, we can expect to see it here.
2637 */
jeffhaob4df5142011-09-19 20:25:32 -07002638 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::UNUSED_EE:
2643 case Instruction::UNUSED_EF:
2644 case Instruction::UNUSED_F2:
2645 case Instruction::UNUSED_F3:
2646 case Instruction::UNUSED_F4:
2647 case Instruction::UNUSED_F5:
2648 case Instruction::UNUSED_F6:
2649 case Instruction::UNUSED_F7:
2650 case Instruction::UNUSED_F8:
2651 case Instruction::UNUSED_F9:
2652 case Instruction::UNUSED_FA:
2653 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002654 case Instruction::UNUSED_F0:
2655 case Instruction::UNUSED_F1:
2656 case Instruction::UNUSED_E3:
2657 case Instruction::UNUSED_E8:
2658 case Instruction::UNUSED_E7:
2659 case Instruction::UNUSED_E4:
2660 case Instruction::UNUSED_E9:
2661 case Instruction::UNUSED_FC:
2662 case Instruction::UNUSED_E5:
2663 case Instruction::UNUSED_EA:
2664 case Instruction::UNUSED_FD:
2665 case Instruction::UNUSED_E6:
2666 case Instruction::UNUSED_EB:
2667 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002668 case Instruction::UNUSED_3E:
2669 case Instruction::UNUSED_3F:
2670 case Instruction::UNUSED_40:
2671 case Instruction::UNUSED_41:
2672 case Instruction::UNUSED_42:
2673 case Instruction::UNUSED_43:
2674 case Instruction::UNUSED_73:
2675 case Instruction::UNUSED_79:
2676 case Instruction::UNUSED_7A:
2677 case Instruction::UNUSED_EC:
2678 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002679 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681
2682 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002683 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002684 * complain if an instruction is missing (which is desirable).
2685 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002687
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 if (failure_ != VERIFY_ERROR_NONE) {
2689 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002690 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002691 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002692 return false;
2693 } else {
2694 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002695 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002697 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002699 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002701 opcode_flag = Instruction::kThrow;
2702 }
2703 }
jeffhaobdb76512011-09-07 11:43:16 -07002704 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 * If we didn't just set the result register, clear it out. This ensures that you can only use
2706 * "move-result" immediately after the result is set. (We could check this statically, but it's
2707 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002708 */
2709 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002711 }
2712
jeffhaoa0a764a2011-09-16 10:43:38 -07002713 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002714 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2716 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2717 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002718 return false;
2719 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2721 // next instruction isn't one.
2722 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002723 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 }
2725 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2726 if (next_line != NULL) {
2727 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2728 // needed.
2729 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002730 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 }
jeffhaobdb76512011-09-07 11:43:16 -07002732 } else {
2733 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 * We're not recording register data for the next instruction, so we don't know what the prior
2735 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002736 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002738 }
2739 }
2740
2741 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002742 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002743 *
2744 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002745 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002746 * somebody could get a reference field, check it for zero, and if the
2747 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002748 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002749 * that, and will reject the code.
2750 *
2751 * TODO: avoid re-fetching the branch target
2752 */
2753 if ((opcode_flag & Instruction::kBranch) != 0) {
2754 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002756 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002758 return false;
2759 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002760 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002762 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 }
jeffhaobdb76512011-09-07 11:43:16 -07002764 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002766 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 }
jeffhaobdb76512011-09-07 11:43:16 -07002768 }
2769
2770 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002771 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002772 *
2773 * We've already verified that the table is structurally sound, so we
2774 * just need to walk through and tag the targets.
2775 */
2776 if ((opcode_flag & Instruction::kSwitch) != 0) {
2777 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2778 const uint16_t* switch_insns = insns + offset_to_switch;
2779 int switch_count = switch_insns[1];
2780 int offset_to_targets, targ;
2781
2782 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2783 /* 0 = sig, 1 = count, 2/3 = first key */
2784 offset_to_targets = 4;
2785 } else {
2786 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002787 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002788 offset_to_targets = 2 + 2 * switch_count;
2789 }
2790
2791 /* verify each switch target */
2792 for (targ = 0; targ < switch_count; targ++) {
2793 int offset;
2794 uint32_t abs_offset;
2795
2796 /* offsets are 32-bit, and only partly endian-swapped */
2797 offset = switch_insns[offset_to_targets + targ * 2] |
2798 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 abs_offset = work_insn_idx_ + offset;
2800 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2801 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002802 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 }
2804 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002805 return false;
2806 }
2807 }
2808
2809 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2811 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002812 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002813 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2814 bool within_catch_all = false;
2815 DexFile::CatchHandlerIterator iterator =
2816 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002817
2818 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2820 within_catch_all = true;
2821 }
jeffhaobdb76512011-09-07 11:43:16 -07002822 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2824 * "work_regs", because at runtime the exception will be thrown before the instruction
2825 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002826 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002828 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 }
jeffhaobdb76512011-09-07 11:43:16 -07002830 }
2831
2832 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2834 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002835 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002837 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 * The state in work_line reflects the post-execution state. If the current instruction is a
2839 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002840 * it will do so before grabbing the lock).
2841 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2843 Fail(VERIFY_ERROR_GENERIC)
2844 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002845 return false;
2846 }
2847 }
2848 }
2849
jeffhaod1f0fde2011-09-08 17:25:33 -07002850 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 if ((opcode_flag & Instruction::kReturn) != 0) {
2852 if(!work_line_->VerifyMonitorStackEmpty()) {
2853 return false;
2854 }
jeffhaobdb76512011-09-07 11:43:16 -07002855 }
2856
2857 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002858 * Update start_guess. Advance to the next instruction of that's
2859 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002860 * neither of those exists we're in a return or throw; leave start_guess
2861 * alone and let the caller sort it out.
2862 */
2863 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002865 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2866 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002868 }
2869
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2871 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002872
2873 return true;
2874}
2875
Ian Rogersd81871c2011-10-03 13:57:23 -07002876Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2877 const Class* referrer = method_->GetDeclaringClass();
2878 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2879 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002880
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 if (res_class == NULL) {
2882 Thread::Current()->ClearException();
2883 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2884 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2885 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2886 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2887 << res_class->GetDescriptor()->ToModifiedUtf8();
2888 }
2889 return res_class;
2890}
2891
2892Class* DexVerifier::GetCaughtExceptionType() {
2893 Class* common_super = NULL;
2894 if (code_item_->tries_size_ != 0) {
2895 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2896 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2897 for (uint32_t i = 0; i < handlers_size; i++) {
2898 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2899 for (; !iterator.HasNext(); iterator.Next()) {
2900 DexFile::CatchHandlerItem handler = iterator.Get();
2901 if (handler.address_ == (uint32_t) work_insn_idx_) {
2902 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2903 common_super = JavaLangThrowable();
2904 } else {
2905 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2906 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2907 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2908 * test, so is essentially harmless.
2909 */
2910 if (klass == NULL) {
2911 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2912 << handler.type_idx_ << " ("
2913 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2914 return NULL;
2915 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2916 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2917 return NULL;
2918 } else if (common_super == NULL) {
2919 common_super = klass;
2920 } else {
2921 common_super = RegType::ClassJoin(common_super, klass);
2922 }
2923 }
2924 }
2925 }
2926 handlers_ptr = iterator.GetData();
2927 }
2928 }
2929 if (common_super == NULL) {
2930 /* no catch blocks, or no catches with classes we can find */
2931 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2932 }
2933 return common_super;
2934}
2935
2936Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2937 Class* referrer = method_->GetDeclaringClass();
2938 DexCache* dex_cache = referrer->GetDexCache();
2939 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2940 if (res_method == NULL) {
2941 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2942 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2943 if (klass == NULL) {
2944 DCHECK(failure_ != VERIFY_ERROR_NONE);
2945 return NULL;
2946 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002947 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2949 if (is_direct) {
2950 res_method = klass->FindDirectMethod(name, signature);
2951 } else if (klass->IsInterface()) {
2952 res_method = klass->FindInterfaceMethod(name, signature);
2953 } else {
2954 res_method = klass->FindVirtualMethod(name, signature);
2955 }
2956 if (res_method != NULL) {
2957 dex_cache->SetResolvedMethod(method_idx, res_method);
2958 } else {
2959 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2960 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2961 << " " << signature;
2962 return NULL;
2963 }
2964 }
2965 /* Check if access is allowed. */
2966 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2967 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2968 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2969 return NULL;
2970 }
2971 return res_method;
2972}
2973
2974Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2975 MethodType method_type, bool is_range, bool is_super) {
2976 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2977 // we're making.
2978 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2979 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2980 if (res_method == NULL) {
2981 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2982 const char* method_name = dex_file_->GetMethodName(method_id);
2983 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002984 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002985 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
2986 fail_messages_ << "unable to resolve method " << dec_insn.vB_ << ": "
2987 << class_descriptor << "." << method_name << " " << method_signature;
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 return NULL;
2989 }
2990 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2991 // enforce them here.
2992 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
2993 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
2994 << PrettyMethod(res_method);
2995 return NULL;
2996 }
2997 // See if the method type implied by the invoke instruction matches the access flags for the
2998 // target method.
2999 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3000 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3001 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3002 ) {
3003 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3004 << PrettyMethod(res_method);
3005 return NULL;
3006 }
3007 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3008 // has a vtable entry for the target method.
3009 if (is_super) {
3010 DCHECK(method_type == METHOD_VIRTUAL);
3011 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3012 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3013 if (super == NULL) { // Only Object has no super class
3014 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3015 << " to super " << PrettyMethod(res_method);
3016 } else {
3017 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3018 << " to super " << PrettyDescriptor(super->GetDescriptor())
3019 << "." << res_method->GetName()->ToModifiedUtf8()
3020 << " " << res_method->GetSignature()->ToModifiedUtf8();
3021
3022 }
3023 return NULL;
3024 }
3025 }
3026 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3027 // match the call to the signature. Also, we might might be calling through an abstract method
3028 // definition (which doesn't have register count values).
3029 int expected_args = dec_insn.vA_;
3030 /* caught by static verifier */
3031 DCHECK(is_range || expected_args <= 5);
3032 if (expected_args > code_item_->outs_size_) {
3033 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3034 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3035 return NULL;
3036 }
3037 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3038 if (sig[0] != '(') {
3039 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3040 << " as descriptor doesn't start with '(': " << sig;
3041 return NULL;
3042 }
jeffhaobdb76512011-09-07 11:43:16 -07003043 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 * Check the "this" argument, which must be an instance of the class
3045 * that declared the method. For an interface class, we don't do the
3046 * full interface merge, so we can't do a rigorous check here (which
3047 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003048 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 int actual_args = 0;
3050 if (!res_method->IsStatic()) {
3051 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3052 if (failure_ != VERIFY_ERROR_NONE) {
3053 return NULL;
3054 }
3055 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3056 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3057 return NULL;
3058 }
3059 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3060 Class* actual_this_ref = actual_arg_type.GetClass();
3061 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
3062 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '"
3063 << PrettyDescriptor(actual_this_ref->GetDescriptor()) << "' not instance of '"
3064 << PrettyDescriptor(res_method->GetDeclaringClass()->GetDescriptor()) << "'";
3065 return NULL;
3066 }
3067 }
3068 actual_args++;
3069 }
3070 /*
3071 * Process the target method's signature. This signature may or may not
3072 * have been verified, so we can't assume it's properly formed.
3073 */
3074 size_t sig_offset = 0;
3075 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3076 if (actual_args >= expected_args) {
3077 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3078 << "'. Expected " << expected_args << " args, found more ("
3079 << sig.substr(sig_offset) << ")";
3080 return NULL;
3081 }
3082 std::string descriptor;
3083 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3084 size_t end;
3085 if (sig[sig_offset] == 'L') {
3086 end = sig.find(';', sig_offset);
3087 } else {
3088 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3089 if (sig[end] == 'L') {
3090 end = sig.find(';', end);
3091 }
3092 }
3093 if (end == std::string::npos) {
3094 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3095 << "bad signature component '" << sig << "' (missing ';')";
3096 return NULL;
3097 }
3098 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3099 sig_offset = end;
3100 } else {
3101 descriptor = sig[sig_offset];
3102 }
3103 const RegType& reg_type =
3104 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003105 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3106 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3107 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
3109 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3110 }
3111 if (sig[sig_offset] != ')') {
3112 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3113 return NULL;
3114 }
3115 if (actual_args != expected_args) {
3116 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3117 << " expected " << expected_args << " args, found " << actual_args;
3118 return NULL;
3119 } else {
3120 return res_method;
3121 }
3122}
3123
3124void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3125 const RegType& insn_type, bool is_primitive) {
3126 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3127 if (!index_type.IsArrayIndexTypes()) {
3128 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3129 } else {
3130 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3131 if (failure_ == VERIFY_ERROR_NONE) {
3132 if (array_class == NULL) {
3133 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3134 // instruction type. TODO: have a proper notion of bottom here.
3135 if (!is_primitive || insn_type.IsCategory1Types()) {
3136 // Reference or category 1
3137 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3138 } else {
3139 // Category 2
3140 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3141 }
3142 } else {
3143 /* verify the class */
3144 Class* component_class = array_class->GetComponentType();
3145 const RegType& component_type = reg_types_.FromClass(component_class);
3146 if (!array_class->IsArrayClass()) {
3147 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3148 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3149 } else if (component_class->IsPrimitive() && !is_primitive) {
3150 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3151 << PrettyDescriptor(array_class->GetDescriptor())
3152 << " source for aget-object";
3153 } else if (!component_class->IsPrimitive() && is_primitive) {
3154 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3155 << PrettyDescriptor(array_class->GetDescriptor())
3156 << " source for category 1 aget";
3157 } else if (is_primitive && !insn_type.Equals(component_type) &&
3158 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3159 (insn_type.IsLong() && component_type.IsDouble()))) {
3160 Fail(VERIFY_ERROR_GENERIC) << "array type "
3161 << PrettyDescriptor(array_class->GetDescriptor())
3162 << " incompatible with aget of type " << insn_type;
3163 } else {
3164 // Use knowledge of the field type which is stronger than the type inferred from the
3165 // instruction, which can't differentiate object types and ints from floats, longs from
3166 // doubles.
3167 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3168 }
3169 }
3170 }
3171 }
3172}
3173
3174void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3175 const RegType& insn_type, bool is_primitive) {
3176 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3177 if (!index_type.IsArrayIndexTypes()) {
3178 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3179 } else {
3180 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3181 if (failure_ == VERIFY_ERROR_NONE) {
3182 if (array_class == NULL) {
3183 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3184 // instruction type.
3185 } else {
3186 /* verify the class */
3187 Class* component_class = array_class->GetComponentType();
3188 const RegType& component_type = reg_types_.FromClass(component_class);
3189 if (!array_class->IsArrayClass()) {
3190 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3191 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3192 } else if (component_class->IsPrimitive() && !is_primitive) {
3193 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3194 << PrettyDescriptor(array_class->GetDescriptor())
3195 << " source for aput-object";
3196 } else if (!component_class->IsPrimitive() && is_primitive) {
3197 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3198 << PrettyDescriptor(array_class->GetDescriptor())
3199 << " source for category 1 aput";
3200 } else if (is_primitive && !insn_type.Equals(component_type) &&
3201 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3202 (insn_type.IsLong() && component_type.IsDouble()))) {
3203 Fail(VERIFY_ERROR_GENERIC) << "array type "
3204 << PrettyDescriptor(array_class->GetDescriptor())
3205 << " incompatible with aput of type " << insn_type;
3206 } else {
3207 // The instruction agrees with the type of array, confirm the value to be stored does too
3208 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3209 }
3210 }
3211 }
3212 }
3213}
3214
3215Field* DexVerifier::GetStaticField(int field_idx) {
3216 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3217 if (field == NULL) {
3218 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3219 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3220 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003221 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 DCHECK(Thread::Current()->IsExceptionPending());
3223 Thread::Current()->ClearException();
3224 return NULL;
3225 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3226 field->GetAccessFlags())) {
3227 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3228 << " from " << PrettyClass(method_->GetDeclaringClass());
3229 return NULL;
3230 } else if (!field->IsStatic()) {
3231 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3232 return NULL;
3233 } else {
3234 return field;
3235 }
3236}
3237
Ian Rogersd81871c2011-10-03 13:57:23 -07003238Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3239 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3240 if (field == NULL) {
3241 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3242 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3243 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003244 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 DCHECK(Thread::Current()->IsExceptionPending());
3246 Thread::Current()->ClearException();
3247 return NULL;
3248 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3249 field->GetAccessFlags())) {
3250 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3251 << " from " << PrettyClass(method_->GetDeclaringClass());
3252 return NULL;
3253 } else if (field->IsStatic()) {
3254 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3255 << " to not be static";
3256 return NULL;
3257 } else if (obj_type.IsZero()) {
3258 // Cannot infer and check type, however, access will cause null pointer exception
3259 return field;
3260 } else if(obj_type.IsUninitializedReference() &&
3261 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3262 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3263 // Field accesses through uninitialized references are only allowable for constructors where
3264 // the field is declared in this class
3265 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3266 << " of a not fully initialized object within the context of "
3267 << PrettyMethod(method_);
3268 return NULL;
3269 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3270 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3271 // of C1. For resolution to occur the declared class of the field must be compatible with
3272 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3273 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3274 << " from object of type " << PrettyClass(obj_type.GetClass());
3275 return NULL;
3276 } else {
3277 return field;
3278 }
3279}
3280
Ian Rogersb94a27b2011-10-26 00:33:41 -07003281void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3282 const RegType& insn_type, bool is_primitive, bool is_static) {
3283 Field* field;
3284 if (is_static) {
3285 field = GetStaticField(dec_insn.vB_);
3286 } else {
3287 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3288 field = GetInstanceField(object_type, dec_insn.vC_);
3289 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 if (field != NULL) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003291 const RegType& field_type =
3292 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3293 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003295 if (field_type.Equals(insn_type) ||
3296 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3297 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 // expected that read is of the correct primitive type or that int reads are reading
3299 // floats or long reads are reading doubles
3300 } else {
3301 // This is a global failure rather than a class change failure as the instructions and
3302 // the descriptors for the type should have been consistent within the same file at
3303 // compile time
3304 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003305 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003306 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 return;
3308 }
3309 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003310 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003312 << " to be compatible with type '" << insn_type
3313 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003314 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 return;
3316 }
3317 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003318 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 }
3320}
3321
Ian Rogersb94a27b2011-10-26 00:33:41 -07003322void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3323 const RegType& insn_type, bool is_primitive, bool is_static) {
3324 Field* field;
3325 if (is_static) {
3326 field = GetStaticField(dec_insn.vB_);
3327 } else {
3328 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3329 field = GetInstanceField(object_type, dec_insn.vC_);
3330 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3333 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3334 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3335 return;
3336 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003337 const RegType& field_type =
3338 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3339 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003341 // Primitive field assignability rules are weaker than regular assignability rules
3342 bool instruction_compatible;
3343 bool value_compatible;
3344 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3345 if (field_type.IsIntegralTypes()) {
3346 instruction_compatible = insn_type.IsIntegralTypes();
3347 value_compatible = value_type.IsIntegralTypes();
3348 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003349 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003350 value_compatible = value_type.IsFloatTypes();
3351 } else if (field_type.IsLong()) {
3352 instruction_compatible = insn_type.IsLong();
3353 value_compatible = value_type.IsLongTypes();
3354 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003355 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003356 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003357 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003358 instruction_compatible = false; // reference field with primitive store
3359 value_compatible = false; // unused
3360 }
3361 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 // This is a global failure rather than a class change failure as the instructions and
3363 // the descriptors for the type should have been consistent within the same file at
3364 // compile time
3365 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003366 << " to be of type '" << insn_type
3367 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003368 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 return;
3370 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003371 if (!value_compatible) {
3372 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3373 << " of type " << value_type
3374 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003375 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003376 return;
3377 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003379 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003381 << " to be compatible with type '" << insn_type
3382 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003383 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 return;
3385 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003386 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 }
3389}
3390
3391bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3392 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3393 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3394 return false;
3395 }
3396 return true;
3397}
3398
3399void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3400 Class* res_class, bool is_range) {
3401 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3402 /*
3403 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3404 * list and fail. It's legal, if silly, for arg_count to be zero.
3405 */
3406 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3407 uint32_t arg_count = dec_insn.vA_;
3408 for (size_t ui = 0; ui < arg_count; ui++) {
3409 uint32_t get_reg;
3410
3411 if (is_range)
3412 get_reg = dec_insn.vC_ + ui;
3413 else
3414 get_reg = dec_insn.arg_[ui];
3415
3416 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3417 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3418 << ") not valid";
3419 return;
3420 }
3421 }
3422}
3423
3424void DexVerifier::ReplaceFailingInstruction() {
3425 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3426 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3427 VerifyErrorRefType ref_type;
3428 switch (inst->Opcode()) {
3429 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003430 case Instruction::CHECK_CAST:
3431 case Instruction::INSTANCE_OF:
3432 case Instruction::NEW_INSTANCE:
3433 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003435 case Instruction::FILLED_NEW_ARRAY_RANGE:
3436 ref_type = VERIFY_ERROR_REF_CLASS;
3437 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003439 case Instruction::IGET_BOOLEAN:
3440 case Instruction::IGET_BYTE:
3441 case Instruction::IGET_CHAR:
3442 case Instruction::IGET_SHORT:
3443 case Instruction::IGET_WIDE:
3444 case Instruction::IGET_OBJECT:
3445 case Instruction::IPUT:
3446 case Instruction::IPUT_BOOLEAN:
3447 case Instruction::IPUT_BYTE:
3448 case Instruction::IPUT_CHAR:
3449 case Instruction::IPUT_SHORT:
3450 case Instruction::IPUT_WIDE:
3451 case Instruction::IPUT_OBJECT:
3452 case Instruction::SGET:
3453 case Instruction::SGET_BOOLEAN:
3454 case Instruction::SGET_BYTE:
3455 case Instruction::SGET_CHAR:
3456 case Instruction::SGET_SHORT:
3457 case Instruction::SGET_WIDE:
3458 case Instruction::SGET_OBJECT:
3459 case Instruction::SPUT:
3460 case Instruction::SPUT_BOOLEAN:
3461 case Instruction::SPUT_BYTE:
3462 case Instruction::SPUT_CHAR:
3463 case Instruction::SPUT_SHORT:
3464 case Instruction::SPUT_WIDE:
3465 case Instruction::SPUT_OBJECT:
3466 ref_type = VERIFY_ERROR_REF_FIELD;
3467 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003469 case Instruction::INVOKE_VIRTUAL_RANGE:
3470 case Instruction::INVOKE_SUPER:
3471 case Instruction::INVOKE_SUPER_RANGE:
3472 case Instruction::INVOKE_DIRECT:
3473 case Instruction::INVOKE_DIRECT_RANGE:
3474 case Instruction::INVOKE_STATIC:
3475 case Instruction::INVOKE_STATIC_RANGE:
3476 case Instruction::INVOKE_INTERFACE:
3477 case Instruction::INVOKE_INTERFACE_RANGE:
3478 ref_type = VERIFY_ERROR_REF_METHOD;
3479 break;
jeffhaobdb76512011-09-07 11:43:16 -07003480 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003481 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003482 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3485 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3486 // instruction, so assert it.
3487 size_t width = inst->SizeInCodeUnits();
3488 CHECK_GT(width, 1u);
3489 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3490 // NOPs
3491 for (size_t i = 2; i < width; i++) {
3492 insns[work_insn_idx_ + i] = Instruction::NOP;
3493 }
3494 // Encode the opcode, with the failure code in the high byte
3495 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3496 (failure_ << 8) | // AA - component
3497 (ref_type << (8 + kVerifyErrorRefTypeShift));
3498 insns[work_insn_idx_] = new_instruction;
3499 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3500 // rewrote, so nothing to do here.
jeffhaobdb76512011-09-07 11:43:16 -07003501}
jeffhaoba5ebb92011-08-25 17:24:37 -07003502
Ian Rogersd81871c2011-10-03 13:57:23 -07003503bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3504 const bool merge_debug = true;
3505 bool changed = true;
3506 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3507 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003508 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 * We haven't processed this instruction before, and we haven't touched the registers here, so
3510 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3511 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003512 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003514 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3516 copy->CopyFromLine(target_line);
3517 changed = target_line->MergeRegisters(merge_line);
3518 if (failure_ != VERIFY_ERROR_NONE) {
3519 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003520 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003521 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3523 << *copy.get() << " MERGE" << std::endl
3524 << *merge_line << " ==" << std::endl
3525 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003526 }
3527 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003528 if (changed) {
3529 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003530 }
3531 return true;
3532}
3533
Ian Rogersd81871c2011-10-03 13:57:23 -07003534void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3535 size_t* log2_max_gc_pc) {
3536 size_t local_gc_points = 0;
3537 size_t max_insn = 0;
3538 size_t max_ref_reg = -1;
3539 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3540 if (insn_flags_[i].IsGcPoint()) {
3541 local_gc_points++;
3542 max_insn = i;
3543 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003544 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003545 }
3546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 *gc_points = local_gc_points;
3548 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3549 size_t i = 0;
3550 while ((1U << i) < max_insn) {
3551 i++;
3552 }
3553 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003554}
3555
Ian Rogersd81871c2011-10-03 13:57:23 -07003556ByteArray* DexVerifier::GenerateGcMap() {
3557 size_t num_entries, ref_bitmap_bits, pc_bits;
3558 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3559 // There's a single byte to encode the size of each bitmap
3560 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3561 // TODO: either a better GC map format or per method failures
3562 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3563 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003564 return NULL;
3565 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003566 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3567 // There are 2 bytes to encode the number of entries
3568 if (num_entries >= 65536) {
3569 // TODO: either a better GC map format or per method failures
3570 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3571 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003572 return NULL;
3573 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003574 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003575 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003577 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 pc_bytes = 1;
3579 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003580 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003582 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 // TODO: either a better GC map format or per method failures
3584 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3585 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3586 return NULL;
3587 }
3588 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3589 ByteArray* table = ByteArray::Alloc(table_size);
3590 if (table == NULL) {
3591 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3592 return NULL;
3593 }
3594 // Write table header
3595 table->Set(0, format);
3596 table->Set(1, ref_bitmap_bytes);
3597 table->Set(2, num_entries & 0xFF);
3598 table->Set(3, (num_entries >> 8) & 0xFF);
3599 // Write table data
3600 size_t offset = 4;
3601 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3602 if (insn_flags_[i].IsGcPoint()) {
3603 table->Set(offset, i & 0xFF);
3604 offset++;
3605 if (pc_bytes == 2) {
3606 table->Set(offset, (i >> 8) & 0xFF);
3607 offset++;
3608 }
3609 RegisterLine* line = reg_table_.GetLine(i);
3610 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3611 offset += ref_bitmap_bytes;
3612 }
3613 }
3614 DCHECK(offset == table_size);
3615 return table;
3616}
jeffhaoa0a764a2011-09-16 10:43:38 -07003617
Ian Rogersd81871c2011-10-03 13:57:23 -07003618void DexVerifier::VerifyGcMap() {
3619 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3620 // that the table data is well formed and all references are marked (or not) in the bitmap
3621 PcToReferenceMap map(method_);
3622 size_t map_index = 0;
3623 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3624 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3625 if (insn_flags_[i].IsGcPoint()) {
3626 CHECK_LT(map_index, map.NumEntries());
3627 CHECK_EQ(map.GetPC(map_index), i);
3628 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3629 map_index++;
3630 RegisterLine* line = reg_table_.GetLine(i);
3631 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003632 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003633 CHECK_LT(j / 8, map.RegWidth());
3634 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3635 } else if ((j / 8) < map.RegWidth()) {
3636 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3637 } else {
3638 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3639 }
3640 }
3641 } else {
3642 CHECK(reg_bitmap == NULL);
3643 }
3644 }
3645}
jeffhaoa0a764a2011-09-16 10:43:38 -07003646
Ian Rogersd81871c2011-10-03 13:57:23 -07003647Class* DexVerifier::JavaLangThrowable() {
3648 if (java_lang_throwable_ == NULL) {
3649 java_lang_throwable_ =
3650 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3651 DCHECK(java_lang_throwable_ != NULL);
3652 }
3653 return java_lang_throwable_;
3654}
3655
3656const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3657 size_t num_entries = NumEntries();
3658 // Do linear or binary search?
3659 static const size_t kSearchThreshold = 8;
3660 if (num_entries < kSearchThreshold) {
3661 for (size_t i = 0; i < num_entries; i++) {
3662 if (GetPC(i) == dex_pc) {
3663 return GetBitMap(i);
3664 }
3665 }
3666 } else {
3667 int lo = 0;
3668 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003669 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 int mid = (hi + lo) / 2;
3671 int mid_pc = GetPC(mid);
3672 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003673 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003674 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003675 hi = mid - 1;
3676 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003678 }
3679 }
3680 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 if (error_if_not_present) {
3682 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3683 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003684 return NULL;
3685}
3686
Ian Rogersd81871c2011-10-03 13:57:23 -07003687} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003688} // namespace art