blob: e4d9f8426884a1db270dcf015928619c714b448d [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"
Elliott Hughes1f359b02011-07-17 14:27:17 -070013#include "logging.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070014#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070015#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
17namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070018namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
Ian Rogers2c8a8572011-10-24 17:11:36 -070020static const bool gDebugVerify = false;
21
Ian Rogersd81871c2011-10-03 13:57:23 -070022std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
23 return os << (int)rhs;
24}
jeffhaobdb76512011-09-07 11:43:16 -070025
Ian Rogersd81871c2011-10-03 13:57:23 -070026static const char type_chars[RegType::kRegTypeMAX + 1 /* for '\0' */ ] = "UX01ZyYhHcibBsSCIFNnJjDdL";
27
Ian Rogers2c8a8572011-10-24 17:11:36 -070028std::string RegType::Dump() const {
Ian Rogersd81871c2011-10-03 13:57:23 -070029 DCHECK(type_ < kRegTypeMAX);
Ian Rogers2c8a8572011-10-24 17:11:36 -070030 std::ostringstream os;
Ian Rogersd81871c2011-10-03 13:57:23 -070031 os << type_chars[type_];
32 if (type_ == kRegTypeReference) {
33 if (IsUninitializedReference()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -070034 os << "[uninitialized-" << PrettyDescriptor(GetClass()->GetDescriptor())
Ian Rogersd81871c2011-10-03 13:57:23 -070035 << ", allocated@" << allocation_pc_ <<"]";
36 } else {
37 os << "[" << PrettyDescriptor(GetClass()->GetDescriptor()) << "]";
38 }
39 }
Ian Rogers2c8a8572011-10-24 17:11:36 -070040 return os.str();
Ian Rogersd81871c2011-10-03 13:57:23 -070041}
42
43const RegType& RegType::HighHalf(RegTypeCache* cache) const {
44 CHECK(IsLowHalf());
45 if (type_ == kRegTypeLongLo) {
46 return cache->FromType(kRegTypeLongHi);
47 } else if (type_ == kRegTypeDoubleLo) {
48 return cache->FromType(kRegTypeDoubleHi);
49 } else {
50 return cache->FromType(kRegTypeConstHi);
51 }
52}
53
54/*
55 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
56 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
57 * 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
58 * is the deepest (lowest upper bound) parent of S and T).
59 *
60 * This operation applies for regular classes and arrays, however, for interface types there needn't
61 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
62 * introducing sets of types, however, the only operation permissible on an interface is
63 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
64 * types until an invoke-interface call on the interface typed reference at runtime and allow
65 * the perversion of Object being assignable to an interface type (note, however, that we don't
66 * allow assignment of Object or Interface to any concrete class and are therefore type safe;
67 * further the Join on a Object cannot result in a sub-class by definition).
68 */
69Class* RegType::ClassJoin(Class* s, Class* t) {
70 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
71 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
72 if (s == t) {
73 return s;
74 } else if (s->IsAssignableFrom(t)) {
75 return s;
76 } else if (t->IsAssignableFrom(s)) {
77 return t;
78 } else if (s->IsArrayClass() && t->IsArrayClass()) {
79 Class* s_ct = s->GetComponentType();
80 Class* t_ct = t->GetComponentType();
81 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
82 // Given the types aren't the same, if either array is of primitive types then the only
83 // common parent is java.lang.Object
84 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
85 DCHECK(result->IsObjectClass());
86 return result;
87 }
88 Class* common_elem = ClassJoin(s_ct, t_ct);
89 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
90 const ClassLoader* class_loader = s->GetClassLoader();
91 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
92 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
93 DCHECK(array_class != NULL);
94 return array_class;
95 } else {
96 size_t s_depth = s->Depth();
97 size_t t_depth = t->Depth();
98 // Get s and t to the same depth in the hierarchy
99 if (s_depth > t_depth) {
100 while (s_depth > t_depth) {
101 s = s->GetSuperClass();
102 s_depth--;
103 }
104 } else {
105 while (t_depth > s_depth) {
106 t = t->GetSuperClass();
107 t_depth--;
108 }
109 }
110 // Go up the hierarchy until we get to the common parent
111 while (s != t) {
112 s = s->GetSuperClass();
113 t = t->GetSuperClass();
114 }
115 return s;
116 }
117}
118
119const RegType& RegType::VerifyAgainst(const RegType& check_type, RegTypeCache* reg_types) const {
120 if (Equals(check_type)) {
121 return *this;
122 } else {
123 switch (check_type.GetType()) {
124 case RegType::kRegTypeBoolean:
125 return IsBooleanTypes() ? check_type : reg_types->Conflict();
126 break;
127 case RegType::kRegTypeByte:
128 return IsByteTypes() ? check_type : reg_types->Conflict();
129 break;
130 case RegType::kRegTypeShort:
131 return IsShortTypes() ? check_type : reg_types->Conflict();
132 break;
133 case RegType::kRegTypeChar:
134 return IsCharTypes() ? check_type : reg_types->Conflict();
135 break;
136 case RegType::kRegTypeInteger:
137 return IsIntegralTypes() ? check_type : reg_types->Conflict();
138 break;
139 case RegType::kRegTypeFloat:
140 return IsFloatTypes() ? check_type : reg_types->Conflict();
141 break;
142 case RegType::kRegTypeLongLo:
143 return IsLongTypes() ? check_type : reg_types->Conflict();
144 break;
145 case RegType::kRegTypeDoubleLo:
146 return IsDoubleTypes() ? check_type : reg_types->Conflict();
147 break;
148 case RegType::kRegTypeReference:
149 if (IsZero()) {
150 return check_type;
151 } else if (IsUninitializedReference()) {
152 return reg_types->Conflict(); // Nonsensical to Join two uninitialized classes
Ian Rogers2c8a8572011-10-24 17:11:36 -0700153 } else if (IsReference() && (check_type.GetClass()->IsAssignableFrom(GetClass()) ||
154 (GetClass()->IsObjectClass() && check_type.GetClass()->IsInterface()))) {
155 // Either we're assignable or this is trying to assign Object to an Interface, which
156 // is allowed (see comment in ClassJoin)
Ian Rogersd81871c2011-10-03 13:57:23 -0700157 return check_type;
158 } else {
159 return reg_types->Conflict();
160 }
161 default:
162 LOG(FATAL) << "Unexpected register type verification against " << check_type;
163 return reg_types->Conflict();
164 }
165 }
166}
167
168#define k_ RegType::kRegTypeUnknown
169#define kX RegType::kRegTypeConflict
170#define k0 RegType::kRegTypeZero
171#define k1 RegType::kRegTypeOne
172#define kZ RegType::kRegTypeBoolean
173#define ky RegType::kRegTypeConstPosByte
174#define kY RegType::kRegTypeConstByte
175#define kh RegType::kRegTypeConstPosShort
176#define kH RegType::kRegTypeConstShort
177#define kc RegType::kRegTypeConstChar
178#define ki RegType::kRegTypeConstInteger
179#define kb RegType::kRegTypePosByte
180#define kB RegType::kRegTypeByte
181#define ks RegType::kRegTypePosShort
182#define kS RegType::kRegTypeShort
183#define kC RegType::kRegTypeChar
184#define kI RegType::kRegTypeInteger
185#define kF RegType::kRegTypeFloat
186#define kN RegType::kRegTypeConstLo
187#define kn RegType::kRegTypeConstHi
188#define kJ RegType::kRegTypeLongLo
189#define kj RegType::kRegTypeLongHi
190#define kD RegType::kRegTypeDoubleLo
191#define kd RegType::kRegTypeDoubleHi
192
193const RegType::Type RegType::merge_table_[kRegTypeReference][kRegTypeReference] =
jeffhaobdb76512011-09-07 11:43:16 -0700194 {
Ian Rogersd81871c2011-10-03 13:57:23 -0700195 /* chk: _ X 0 1 Z y Y h H c i b B s S C I F N n J j D d */
196 { /*_*/ k_,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
197 { /*X*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
198 { /*0*/ kX,kX,k0,kZ,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
199 { /*1*/ kX,kX,kZ,k1,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
200 { /*Z*/ kX,kX,kZ,kZ,kZ,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
201 { /*y*/ kX,kX,ky,ky,ky,ky,kY,kh,kH,kc,ki,kb,kB,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
202 { /*Y*/ kX,kX,kY,kY,kY,kY,kY,kh,kH,kc,ki,kB,kB,kS,kS,kI,kI,kF,kX,kX,kX,kX,kX,kX },
203 { /*h*/ kX,kX,kh,kh,kh,kh,kh,kh,kH,kc,ki,ks,kS,ks,kS,kC,kI,kF,kX,kX,kX,kX,kX,kX },
204 { /*H*/ kX,kX,kH,kH,kH,kH,kH,kH,kH,kc,ki,kS,kS,kS,kS,kI,kI,kF,kX,kX,kX,kX,kX,kX },
205 { /*c*/ kX,kX,kc,kc,kc,kc,kc,kc,kc,kc,ki,kC,kI,kC,kI,kC,kI,kF,kX,kX,kX,kX,kX,kX },
206 { /*i*/ kX,kX,ki,ki,ki,ki,ki,ki,ki,ki,ki,kI,kI,kI,kI,kI,kI,kF,kX,kX,kX,kX,kX,kX },
207 { /*b*/ kX,kX,kb,kb,kb,kb,kB,ks,kS,kC,kI,kb,kB,ks,kS,kC,kI,kX,kX,kX,kX,kX,kX,kX },
208 { /*B*/ kX,kX,kB,kB,kB,kB,kB,kS,kS,kI,kI,kB,kB,kS,kS,kI,kI,kX,kX,kX,kX,kX,kX,kX },
209 { /*s*/ kX,kX,ks,ks,ks,ks,kS,ks,kS,kC,kI,ks,kS,ks,kS,kC,kI,kX,kX,kX,kX,kX,kX,kX },
210 { /*S*/ kX,kX,kS,kS,kS,kS,kS,kS,kS,kI,kI,kS,kS,kS,kS,kI,kI,kX,kX,kX,kX,kX,kX,kX },
211 { /*C*/ kX,kX,kC,kC,kC,kC,kI,kC,kI,kC,kI,kC,kI,kC,kI,kC,kI,kX,kX,kX,kX,kX,kX,kX },
212 { /*I*/ kX,kX,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kI,kX,kX,kX,kX,kX,kX,kX },
213 { /*F*/ kX,kX,kF,kF,kF,kF,kF,kF,kF,kF,kF,kX,kX,kX,kX,kX,kX,kF,kX,kX,kX,kX,kX,kX },
214 { /*N*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kN,kX,kJ,kX,kD,kX },
215 { /*n*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kn,kX,kj,kX,kd },
216 { /*J*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kJ,kX,kJ,kX,kX,kX },
217 { /*j*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kj,kX,kj,kX,kX },
218 { /*D*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kD,kX,kX,kX,kD,kX },
219 { /*d*/ kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kd,kX,kX,kX,kd },
jeffhaobdb76512011-09-07 11:43:16 -0700220 };
221
222#undef k_
jeffhaobdb76512011-09-07 11:43:16 -0700223#undef kX
224#undef k0
225#undef k1
226#undef kZ
227#undef ky
228#undef kY
229#undef kh
230#undef kH
231#undef kc
232#undef ki
233#undef kb
234#undef kB
235#undef ks
236#undef kS
237#undef kC
238#undef kI
239#undef kF
240#undef kN
241#undef kn
242#undef kJ
243#undef kj
244#undef kD
245#undef kd
246
Ian Rogersd81871c2011-10-03 13:57:23 -0700247const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
248 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
249 if (type_ < kRegTypeReference) {
250 if (incoming_type.type_ < kRegTypeReference) {
251 // Merge of primitive types, unknown or conflict use lookup table
252 RegType::Type table_type = merge_table_[type_][incoming_type.type_];
253 // Check if we got an identity result before hitting the cache
254 if (type_ == table_type) {
255 return *this;
256 } else if (incoming_type.type_ == table_type) {
257 return incoming_type;
258 } else {
259 return reg_types->FromType(table_type);
260 }
261 } else if (IsZero()) { // Merge of primitive zero and reference => reference
262 return incoming_type;
263 } else { // Merge with non-zero with reference => conflict
264 return reg_types->Conflict();
265 }
266 } else if (!incoming_type.IsReference()) {
267 DCHECK(IsReference());
268 if (incoming_type.IsZero()) { // Merge of primitive zero and reference => reference
269 return *this;
270 } else { // Merge with non-zero with reference => conflict
271 return reg_types->Conflict();
272 }
273 } else if (IsUninitializedReference()) {
274 // Can only merge an uninitialized type with itself, but we already checked this
275 return reg_types->Conflict();
276 } else { // Two reference types, compute Join
277 Class* c1 = GetClass();
278 Class* c2 = incoming_type.GetClass();
279 DCHECK(c1 != NULL && !c1->IsPrimitive());
280 DCHECK(c2 != NULL && !c2->IsPrimitive());
281 Class* join_class = ClassJoin(c1, c2);
282 if (c1 == join_class) {
283 return *this;
284 } else if (c2 == join_class) {
285 return incoming_type;
286 } else {
287 return reg_types->FromClass(join_class);
288 }
289 }
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) {
340 if (type < RegType::kRegTypeReference) {
341 // 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) {
345 Class *klass = NULL;
346 if (descriptor.size() != 0) {
347 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
348 }
349 entry = new RegType(type, klass, RegType::kInitArgAddr, type);
350 entries_[type] = entry;
351 }
352 return *entry;
353 } else {
354 DCHECK (type == RegType::kRegTypeReference);
355 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
356 RegType* cur_entry = entries_[i];
357 if (cur_entry->IsInitialized() &&
358 cur_entry->GetClass()->GetDescriptor()->Equals(descriptor)) {
359 return *cur_entry;
360 }
361 }
362 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700363 if (klass != NULL) {
364 RegType* entry = new RegType(type, klass, RegType::kInitArgAddr, entries_.size());
365 entries_.push_back(entry);
366 return *entry;
367 } else {
368 DCHECK(Thread::Current()->IsExceptionPending());
369 return FromType(RegType::kRegTypeUnknown);
370 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 }
372}
373
374const RegType& RegTypeCache::FromClass(Class* klass) {
375 if (klass->IsPrimitive()) {
376 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
377 // entries should be sized greater than primitive types
378 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
379 RegType* entry = entries_[type];
380 if (entry == NULL) {
381 entry = new RegType(type, klass, RegType::kInitArgAddr, type);
382 entries_[type] = entry;
383 }
384 return *entry;
385 } else {
386 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
387 RegType* cur_entry = entries_[i];
388 if (cur_entry->IsInitialized() && cur_entry->GetClass() == klass) {
389 return *cur_entry;
390 }
391 }
392 RegType* entry = new RegType(RegType::kRegTypeReference, klass, RegType::kInitArgAddr,
393 entries_.size());
394 entries_.push_back(entry);
395 return *entry;
396 }
397}
398
399const RegType& RegTypeCache::Uninitialized(Class* klass, uint32_t allocation_pc) {
400 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
401 RegType* cur_entry = entries_[i];
402 if (cur_entry->allocation_pc_ == allocation_pc && cur_entry->GetClass() == klass) {
403 return *cur_entry;
404 }
405 }
406 RegType* entry = new RegType(RegType::kRegTypeReference, klass, allocation_pc, entries_.size());
407 entries_.push_back(entry);
408 return *entry;
409}
410
411const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
412 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
413 RegType* cur_entry = entries_[i];
414 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
415 return *cur_entry;
416 }
417 }
418 RegType* entry = new RegType(RegType::kRegTypeReference, klass, RegType::kUninitThisArgAddr,
419 entries_.size());
420 entries_.push_back(entry);
421 return *entry;
422}
423
424const RegType& RegTypeCache::FromType(RegType::Type type) {
425 CHECK(type < RegType::kRegTypeReference);
426 switch (type) {
427 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
428 case RegType::kRegTypeByte: return From(type, NULL, "B");
429 case RegType::kRegTypeShort: return From(type, NULL, "S");
430 case RegType::kRegTypeChar: return From(type, NULL, "C");
431 case RegType::kRegTypeInteger: return From(type, NULL, "I");
432 case RegType::kRegTypeFloat: return From(type, NULL, "F");
433 case RegType::kRegTypeLongLo:
434 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
435 case RegType::kRegTypeDoubleLo:
436 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
437 default: return From(type, NULL, "");
438 }
439}
440
441const RegType& RegTypeCache::FromCat1Const(int32_t value) {
442 if (value < -32768) {
443 return FromType(RegType::kRegTypeConstInteger);
444 } else if (value < -128) {
445 return FromType(RegType::kRegTypeConstShort);
446 } else if (value < 0) {
447 return FromType(RegType::kRegTypeConstByte);
448 } else if (value == 0) {
449 return FromType(RegType::kRegTypeZero);
450 } else if (value == 1) {
451 return FromType(RegType::kRegTypeOne);
452 } else if (value < 128) {
453 return FromType(RegType::kRegTypeConstPosByte);
454 } else if (value < 32768) {
455 return FromType(RegType::kRegTypeConstPosShort);
456 } else if (value < 65536) {
457 return FromType(RegType::kRegTypeConstChar);
458 } else {
459 return FromType(RegType::kRegTypeConstInteger);
460 }
461}
462
463bool RegisterLine::CheckConstructorReturn() const {
464 for (size_t i = 0; i < num_regs_; i++) {
465 if (GetRegisterType(i).IsUninitializedThisReference()) {
466 verifier_->Fail(VERIFY_ERROR_GENERIC)
467 << "Constructor returning without calling superclass constructor";
468 return false;
469 }
470 }
471 return true;
472}
473
474void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
475 DCHECK(vdst < num_regs_);
476 if (new_type.IsLowHalf()) {
477 line_[vdst] = new_type.GetId();
478 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
479 } else if (new_type.IsHighHalf()) {
480 /* should never set these explicitly */
481 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
482 } else if (new_type.IsConflict()) { // should only be set during a merge
483 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
484 } else {
485 line_[vdst] = new_type.GetId();
486 }
487 // Clear the monitor entry bits for this register.
488 ClearAllRegToLockDepths(vdst);
489}
490
491void RegisterLine::SetResultTypeToUnknown() {
492 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
493 result_[0] = unknown_id;
494 result_[1] = unknown_id;
495}
496
497void RegisterLine::SetResultRegisterType(const RegType& new_type) {
498 result_[0] = new_type.GetId();
499 if(new_type.IsLowHalf()) {
500 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
501 result_[1] = new_type.GetId() + 1;
502 } else {
503 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
504 }
505}
506
507const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
508 // The register index was validated during the static pass, so we don't need to check it here.
509 DCHECK_LT(vsrc, num_regs_);
510 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
511}
512
513const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
514 if (dec_insn.vA_ < 1) {
515 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
516 return verifier_->GetRegTypeCache()->Unknown();
517 }
518 /* get the element type of the array held in vsrc */
519 const RegType& this_type = GetRegisterType(dec_insn.vC_);
520 if (!this_type.IsReferenceTypes()) {
521 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
522 << dec_insn.vC_ << " (type=" << this_type << ")";
523 return verifier_->GetRegTypeCache()->Unknown();
524 }
525 return this_type;
526}
527
528Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
529 /* get the element type of the array held in vsrc */
530 const RegType& type = GetRegisterType(vsrc);
531 /* if "always zero", we allow it to fail at runtime */
532 if (type.IsZero()) {
533 return NULL;
534 } else if (!type.IsReferenceTypes()) {
535 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
536 << " (type=" << type << ")";
537 return NULL;
538 } else if (type.IsUninitializedReference()) {
539 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
540 return NULL;
541 } else {
542 return type.GetClass();
543 }
544}
545
546bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
547 // Verify the src register type against the check type refining the type of the register
548 const RegType& src_type = GetRegisterType(vsrc);
549 const RegType& lub_type = src_type.VerifyAgainst(check_type, verifier_->GetRegTypeCache());
550 if (lub_type.IsConflict()) {
551 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
781 if (GetRegisterType(i + j).IsReference()) {
782 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) << " "
878 << verifier.fail_messages_.str() << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700879 if (gDebugVerify) {
880 verifier.Dump(std::cout);
881 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
883 }
884 return success;
885}
886
887DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
888 method_(method), failure_(VERIFY_ERROR_NONE),
889 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700890 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
891 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 dex_file_ = &class_linker->FindDexFile(dex_cache);
893 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700894}
895
Ian Rogersd81871c2011-10-03 13:57:23 -0700896bool DexVerifier::Verify() {
897 // If there aren't any instructions, make sure that's expected, then exit successfully.
898 if (code_item_ == NULL) {
899 if (!method_->IsNative() && !method_->IsAbstract()) {
900 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700901 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 } else {
903 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700904 }
jeffhaobdb76512011-09-07 11:43:16 -0700905 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
907 if (code_item_->ins_size_ > code_item_->registers_size_) {
908 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
909 << " regs=" << code_item_->registers_size_;
910 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700911 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 // Allocate and initialize an array to hold instruction data.
913 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
914 // Run through the instructions and see if the width checks out.
915 bool result = ComputeWidthsAndCountOps();
916 // Flag instructions guarded by a "try" block and check exception handlers.
917 result = result && ScanTryCatchBlocks();
918 // Perform static instruction verification.
919 result = result && VerifyInstructions();
920 // Perform code flow analysis.
921 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700922 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700923}
924
Ian Rogersd81871c2011-10-03 13:57:23 -0700925bool DexVerifier::ComputeWidthsAndCountOps() {
926 const uint16_t* insns = code_item_->insns_;
927 size_t insns_size = code_item_->insns_size_in_code_units_;
928 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700929 size_t new_instance_count = 0;
930 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700932
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700934 Instruction::Code opcode = inst->Opcode();
935 if (opcode == Instruction::NEW_INSTANCE) {
936 new_instance_count++;
937 } else if (opcode == Instruction::MONITOR_ENTER) {
938 monitor_enter_count++;
939 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 size_t inst_size = inst->SizeInCodeUnits();
941 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
942 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700943 inst = inst->Next();
944 }
945
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 if (dex_pc != insns_size) {
947 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
948 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700949 return false;
950 }
951
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 new_instance_count_ = new_instance_count;
953 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700954 return true;
955}
956
Ian Rogersd81871c2011-10-03 13:57:23 -0700957bool DexVerifier::ScanTryCatchBlocks() {
958 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700959 if (tries_size == 0) {
960 return true;
961 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 uint32_t insns_size = code_item_->insns_size_in_code_units_;
963 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700964
965 for (uint32_t idx = 0; idx < tries_size; idx++) {
966 const DexFile::TryItem* try_item = &tries[idx];
967 uint32_t start = try_item->start_addr_;
968 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700969 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
971 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700972 return false;
973 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 if (!insn_flags_[start].IsOpcode()) {
975 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700976 return false;
977 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 for (uint32_t dex_pc = start; dex_pc < end;
979 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
980 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700981 }
982 }
jeffhaobdb76512011-09-07 11:43:16 -0700983 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700985 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
986 for (uint32_t idx = 0; idx < handlers_size; idx++) {
987 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700988 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 uint32_t dex_pc= iterator.Get().address_;
990 if (!insn_flags_[dex_pc].IsOpcode()) {
991 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700992 return false;
993 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -0700995 }
jeffhaobdb76512011-09-07 11:43:16 -0700996 handlers_ptr = iterator.GetData();
997 }
jeffhaobdb76512011-09-07 11:43:16 -0700998 return true;
999}
1000
Ian Rogersd81871c2011-10-03 13:57:23 -07001001bool DexVerifier::VerifyInstructions() {
1002 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001003
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 /* Flag the start of the method as a branch target. */
1005 insn_flags_[0].SetBranchTarget();
1006
1007 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1008 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1009 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001010 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1011 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 return false;
1013 }
1014 /* Flag instructions that are garbage collection points */
1015 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1016 insn_flags_[dex_pc].SetGcPoint();
1017 }
1018 dex_pc += inst->SizeInCodeUnits();
1019 inst = inst->Next();
1020 }
1021 return true;
1022}
1023
1024bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1025 Instruction::DecodedInstruction dec_insn(inst);
1026 bool result = true;
1027 switch (inst->GetVerifyTypeArgumentA()) {
1028 case Instruction::kVerifyRegA:
1029 result = result && CheckRegisterIndex(dec_insn.vA_);
1030 break;
1031 case Instruction::kVerifyRegAWide:
1032 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1033 break;
1034 }
1035 switch (inst->GetVerifyTypeArgumentB()) {
1036 case Instruction::kVerifyRegB:
1037 result = result && CheckRegisterIndex(dec_insn.vB_);
1038 break;
1039 case Instruction::kVerifyRegBField:
1040 result = result && CheckFieldIndex(dec_insn.vB_);
1041 break;
1042 case Instruction::kVerifyRegBMethod:
1043 result = result && CheckMethodIndex(dec_insn.vB_);
1044 break;
1045 case Instruction::kVerifyRegBNewInstance:
1046 result = result && CheckNewInstance(dec_insn.vB_);
1047 break;
1048 case Instruction::kVerifyRegBString:
1049 result = result && CheckStringIndex(dec_insn.vB_);
1050 break;
1051 case Instruction::kVerifyRegBType:
1052 result = result && CheckTypeIndex(dec_insn.vB_);
1053 break;
1054 case Instruction::kVerifyRegBWide:
1055 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1056 break;
1057 }
1058 switch (inst->GetVerifyTypeArgumentC()) {
1059 case Instruction::kVerifyRegC:
1060 result = result && CheckRegisterIndex(dec_insn.vC_);
1061 break;
1062 case Instruction::kVerifyRegCField:
1063 result = result && CheckFieldIndex(dec_insn.vC_);
1064 break;
1065 case Instruction::kVerifyRegCNewArray:
1066 result = result && CheckNewArray(dec_insn.vC_);
1067 break;
1068 case Instruction::kVerifyRegCType:
1069 result = result && CheckTypeIndex(dec_insn.vC_);
1070 break;
1071 case Instruction::kVerifyRegCWide:
1072 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1073 break;
1074 }
1075 switch (inst->GetVerifyExtraFlags()) {
1076 case Instruction::kVerifyArrayData:
1077 result = result && CheckArrayData(code_offset);
1078 break;
1079 case Instruction::kVerifyBranchTarget:
1080 result = result && CheckBranchTarget(code_offset);
1081 break;
1082 case Instruction::kVerifySwitchTargets:
1083 result = result && CheckSwitchTargets(code_offset);
1084 break;
1085 case Instruction::kVerifyVarArg:
1086 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1087 break;
1088 case Instruction::kVerifyVarArgRange:
1089 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1090 break;
1091 case Instruction::kVerifyError:
1092 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1093 result = false;
1094 break;
1095 }
1096 return result;
1097}
1098
1099bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1100 if (idx >= code_item_->registers_size_) {
1101 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1102 << code_item_->registers_size_ << ")";
1103 return false;
1104 }
1105 return true;
1106}
1107
1108bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1109 if (idx + 1 >= code_item_->registers_size_) {
1110 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1111 << "+1 >= " << code_item_->registers_size_ << ")";
1112 return false;
1113 }
1114 return true;
1115}
1116
1117bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1118 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1119 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1120 << dex_file_->GetHeader().field_ids_size_ << ")";
1121 return false;
1122 }
1123 return true;
1124}
1125
1126bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1127 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1128 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1129 << dex_file_->GetHeader().method_ids_size_ << ")";
1130 return false;
1131 }
1132 return true;
1133}
1134
1135bool DexVerifier::CheckNewInstance(uint32_t idx) {
1136 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1137 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1138 << dex_file_->GetHeader().type_ids_size_ << ")";
1139 return false;
1140 }
1141 // We don't need the actual class, just a pointer to the class name.
1142 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1143 if (descriptor[0] != 'L') {
1144 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1145 return false;
1146 }
1147 return true;
1148}
1149
1150bool DexVerifier::CheckStringIndex(uint32_t idx) {
1151 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1152 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1153 << dex_file_->GetHeader().string_ids_size_ << ")";
1154 return false;
1155 }
1156 return true;
1157}
1158
1159bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1160 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1161 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1162 << dex_file_->GetHeader().type_ids_size_ << ")";
1163 return false;
1164 }
1165 return true;
1166}
1167
1168bool DexVerifier::CheckNewArray(uint32_t idx) {
1169 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1170 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1171 << dex_file_->GetHeader().type_ids_size_ << ")";
1172 return false;
1173 }
1174 int bracket_count = 0;
1175 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1176 const char* cp = descriptor;
1177 while (*cp++ == '[') {
1178 bracket_count++;
1179 }
1180 if (bracket_count == 0) {
1181 /* The given class must be an array type. */
1182 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1183 return false;
1184 } else if (bracket_count > 255) {
1185 /* It is illegal to create an array of more than 255 dimensions. */
1186 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1187 return false;
1188 }
1189 return true;
1190}
1191
1192bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1193 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1194 const uint16_t* insns = code_item_->insns_ + cur_offset;
1195 const uint16_t* array_data;
1196 int32_t array_data_offset;
1197
1198 DCHECK_LT(cur_offset, insn_count);
1199 /* make sure the start of the array data table is in range */
1200 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1201 if ((int32_t) cur_offset + array_data_offset < 0 ||
1202 cur_offset + array_data_offset + 2 >= insn_count) {
1203 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1204 << ", data offset " << array_data_offset << ", count " << insn_count;
1205 return false;
1206 }
1207 /* offset to array data table is a relative branch-style offset */
1208 array_data = insns + array_data_offset;
1209 /* make sure the table is 32-bit aligned */
1210 if ((((uint32_t) array_data) & 0x03) != 0) {
1211 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1212 << ", data offset " << array_data_offset;
1213 return false;
1214 }
1215 uint32_t value_width = array_data[1];
1216 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1217 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1218 /* make sure the end of the switch is in range */
1219 if (cur_offset + array_data_offset + table_size > insn_count) {
1220 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1221 << ", data offset " << array_data_offset << ", end "
1222 << cur_offset + array_data_offset + table_size
1223 << ", count " << insn_count;
1224 return false;
1225 }
1226 return true;
1227}
1228
1229bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1230 int32_t offset;
1231 bool isConditional, selfOkay;
1232 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1233 return false;
1234 }
1235 if (!selfOkay && offset == 0) {
1236 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1237 return false;
1238 }
1239 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1240 // identical "wrap-around" behavior, but it's unwise to depend on that.
1241 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1242 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1243 return false;
1244 }
1245 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1246 int32_t abs_offset = cur_offset + offset;
1247 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1248 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1249 << (void*) abs_offset << ") at " << (void*) cur_offset;
1250 return false;
1251 }
1252 insn_flags_[abs_offset].SetBranchTarget();
1253 return true;
1254}
1255
1256bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1257 bool* selfOkay) {
1258 const uint16_t* insns = code_item_->insns_ + cur_offset;
1259 *pConditional = false;
1260 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001261 switch (*insns & 0xff) {
1262 case Instruction::GOTO:
1263 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 break;
1265 case Instruction::GOTO_32:
1266 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 *selfOkay = true;
1268 break;
1269 case Instruction::GOTO_16:
1270 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001271 break;
1272 case Instruction::IF_EQ:
1273 case Instruction::IF_NE:
1274 case Instruction::IF_LT:
1275 case Instruction::IF_GE:
1276 case Instruction::IF_GT:
1277 case Instruction::IF_LE:
1278 case Instruction::IF_EQZ:
1279 case Instruction::IF_NEZ:
1280 case Instruction::IF_LTZ:
1281 case Instruction::IF_GEZ:
1282 case Instruction::IF_GTZ:
1283 case Instruction::IF_LEZ:
1284 *pOffset = (int16_t) insns[1];
1285 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001286 break;
1287 default:
1288 return false;
1289 break;
1290 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 return true;
1292}
1293
Ian Rogersd81871c2011-10-03 13:57:23 -07001294bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1295 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001296 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001298 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1300 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1301 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1302 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001303 return false;
1304 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001307 /* make sure the table is 32-bit aligned */
1308 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1310 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001311 return false;
1312 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 uint32_t switch_count = switch_insns[1];
1314 int32_t keys_offset, targets_offset;
1315 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1317 /* 0=sig, 1=count, 2/3=firstKey */
1318 targets_offset = 4;
1319 keys_offset = -1;
1320 expected_signature = Instruction::kPackedSwitchSignature;
1321 } else {
1322 /* 0=sig, 1=count, 2..count*2 = keys */
1323 keys_offset = 2;
1324 targets_offset = 2 + 2 * switch_count;
1325 expected_signature = Instruction::kSparseSwitchSignature;
1326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001328 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1330 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001331 return false;
1332 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 /* make sure the end of the switch is in range */
1334 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1336 << switch_offset << ", end "
1337 << (cur_offset + switch_offset + table_size)
1338 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 return false;
1340 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001341 /* for a sparse switch, verify the keys are in ascending order */
1342 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1344 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1346 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1347 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1349 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001350 return false;
1351 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 last_key = key;
1353 }
1354 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 for (uint32_t targ = 0; targ < switch_count; targ++) {
1357 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1358 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1359 int32_t abs_offset = cur_offset + offset;
1360 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1361 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1362 << (void*) abs_offset << ") at "
1363 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 return false;
1365 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 insn_flags_[abs_offset].SetBranchTarget();
1367 }
1368 return true;
1369}
1370
1371bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1372 if (vA > 5) {
1373 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1374 return false;
1375 }
1376 uint16_t registers_size = code_item_->registers_size_;
1377 for (uint32_t idx = 0; idx < vA; idx++) {
1378 if (arg[idx] > registers_size) {
1379 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1380 << ") in non-range invoke (> " << registers_size << ")";
1381 return false;
1382 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 }
1384
1385 return true;
1386}
1387
Ian Rogersd81871c2011-10-03 13:57:23 -07001388bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1389 uint16_t registers_size = code_item_->registers_size_;
1390 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1391 // integer overflow when adding them here.
1392 if (vA + vC > registers_size) {
1393 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1394 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 return false;
1396 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 return true;
1398}
1399
Ian Rogersd81871c2011-10-03 13:57:23 -07001400bool DexVerifier::VerifyCodeFlow() {
1401 uint16_t registers_size = code_item_->registers_size_;
1402 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001403
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 if (registers_size * insns_size > 4*1024*1024) {
1405 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1406 << " insns_size=" << insns_size << ")";
1407 }
1408 /* Create and initialize table holding register status */
1409 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1410 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001411
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 work_line_.reset(new RegisterLine(registers_size, this));
1413 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001414
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 /* Initialize register types of method arguments. */
1416 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001417 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1418 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 }
1421 /* Perform code flow verification. */
1422 if (!CodeFlowVerifyMethod()) {
1423 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001424 }
1425
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 /* Generate a register map and add it to the method. */
1427 ByteArray* map = GenerateGcMap();
1428 if (map == NULL) {
1429 return false; // Not a real failure, but a failure to encode
1430 }
1431 method_->SetGcMap(map);
1432#ifndef NDEBUG
1433 VerifyGcMap();
1434#endif
jeffhaobdb76512011-09-07 11:43:16 -07001435 return true;
1436}
1437
Ian Rogersd81871c2011-10-03 13:57:23 -07001438void DexVerifier::Dump(std::ostream& os) {
1439 if (method_->IsNative()) {
1440 os << "Native method" << std::endl;
1441 return;
jeffhaobdb76512011-09-07 11:43:16 -07001442 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 DCHECK(code_item_ != NULL);
1444 const Instruction* inst = Instruction::At(code_item_->insns_);
1445 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1446 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001447 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1448 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1450 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001451 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001452 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001453 inst = inst->Next();
1454 }
jeffhaobdb76512011-09-07 11:43:16 -07001455}
1456
Ian Rogersd81871c2011-10-03 13:57:23 -07001457static bool IsPrimitiveDescriptor(char descriptor) {
1458 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001459 case 'I':
1460 case 'C':
1461 case 'S':
1462 case 'B':
1463 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001464 case 'F':
1465 case 'D':
1466 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001468 default:
1469 return false;
1470 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001471}
1472
Ian Rogersd81871c2011-10-03 13:57:23 -07001473bool DexVerifier::SetTypesFromSignature() {
1474 RegisterLine* reg_line = reg_table_.GetLine(0);
1475 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1476 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001477
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1479 //Include the "this" pointer.
1480 size_t cur_arg = 0;
1481 if (!method_->IsStatic()) {
1482 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1483 // argument as uninitialized. This restricts field access until the superclass constructor is
1484 // called.
1485 Class* declaring_class = method_->GetDeclaringClass();
1486 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1487 reg_line->SetRegisterType(arg_start + cur_arg,
1488 reg_types_.UninitializedThisArgument(declaring_class));
1489 } else {
1490 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001491 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
1494
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1496 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1497
1498 for (; iterator.HasNext(); iterator.Next()) {
1499 const char* descriptor = iterator.GetDescriptor();
1500 if (descriptor == NULL) {
1501 LOG(FATAL) << "Null descriptor";
1502 }
1503 if (cur_arg >= expected_args) {
1504 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1505 << " args, found more (" << descriptor << ")";
1506 return false;
1507 }
1508 switch (descriptor[0]) {
1509 case 'L':
1510 case '[':
1511 // We assume that reference arguments are initialized. The only way it could be otherwise
1512 // (assuming the caller was verified) is if the current method is <init>, but in that case
1513 // it's effectively considered initialized the instant we reach here (in the sense that we
1514 // can return without doing anything or call virtual methods).
1515 {
1516 const RegType& reg_type =
1517 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers2c8a8572011-10-24 17:11:36 -07001518 if (!reg_type.IsUnknown()) {
1519 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
1520 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 DCHECK(Thread::Current()->IsExceptionPending());
1522 Thread::Current()->ClearException();
Ian Rogers2c8a8572011-10-24 17:11:36 -07001523 LOG(WARNING) << "Unable to resolve descriptor in signature " << descriptor
1524 << " using Object";
1525 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.JavaLangObject());
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 }
1528 break;
1529 case 'Z':
1530 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1531 break;
1532 case 'C':
1533 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1534 break;
1535 case 'B':
1536 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1537 break;
1538 case 'I':
1539 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1540 break;
1541 case 'S':
1542 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1543 break;
1544 case 'F':
1545 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1546 break;
1547 case 'J':
1548 case 'D': {
1549 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1550 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1551 cur_arg++;
1552 break;
1553 }
1554 default:
1555 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1556 return false;
1557 }
1558 cur_arg++;
1559 }
1560 if (cur_arg != expected_args) {
1561 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1562 return false;
1563 }
1564 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1565 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1566 // format. Only major difference from the method argument format is that 'V' is supported.
1567 bool result;
1568 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1569 result = descriptor[1] == '\0';
1570 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1571 size_t i = 0;
1572 do {
1573 i++;
1574 } while (descriptor[i] == '['); // process leading [
1575 if (descriptor[i] == 'L') { // object array
1576 do {
1577 i++; // find closing ;
1578 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1579 result = descriptor[i] == ';';
1580 } else { // primitive array
1581 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1582 }
1583 } else if (descriptor[0] == 'L') {
1584 // could be more thorough here, but shouldn't be required
1585 size_t i = 0;
1586 do {
1587 i++;
1588 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1589 result = descriptor[i] == ';';
1590 } else {
1591 result = false;
1592 }
1593 if (!result) {
1594 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1595 << descriptor << "'";
1596 }
1597 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001598}
1599
Ian Rogersd81871c2011-10-03 13:57:23 -07001600bool DexVerifier::CodeFlowVerifyMethod() {
1601 const uint16_t* insns = code_item_->insns_;
1602 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001603
jeffhaobdb76512011-09-07 11:43:16 -07001604 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 insn_flags_[0].SetChanged();
1606 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001607
jeffhaobdb76512011-09-07 11:43:16 -07001608 /* Continue until no instructions are marked "changed". */
1609 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1611 uint32_t insn_idx = start_guess;
1612 for (; insn_idx < insns_size; insn_idx++) {
1613 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
1615 }
jeffhaobdb76512011-09-07 11:43:16 -07001616 if (insn_idx == insns_size) {
1617 if (start_guess != 0) {
1618 /* try again, starting from the top */
1619 start_guess = 0;
1620 continue;
1621 } else {
1622 /* all flags are clear */
1623 break;
1624 }
1625 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 // We carry the working set of registers from instruction to instruction. If this address can
1627 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1628 // "changed" flags, we need to load the set of registers from the table.
1629 // Because we always prefer to continue on to the next instruction, we should never have a
1630 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1631 // target.
1632 work_insn_idx_ = insn_idx;
1633 if (insn_flags_[insn_idx].IsBranchTarget()) {
1634 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001635 } else {
1636#ifndef NDEBUG
1637 /*
1638 * Sanity check: retrieve the stored register line (assuming
1639 * a full table) and make sure it actually matches.
1640 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1642 if (register_line != NULL) {
1643 if (work_line_->CompareLine(register_line) != 0) {
1644 Dump(std::cout);
1645 std::cout << info_messages_.str();
1646 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1647 << "@" << (void*)work_insn_idx_ << std::endl
1648 << " work_line=" << *work_line_ << std::endl
1649 << " expected=" << *register_line;
1650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 }
1652#endif
1653 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 if (!CodeFlowVerifyInstruction(&start_guess)) {
1655 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001656 return false;
1657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 insn_flags_[insn_idx].SetVisited();
1660 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001661 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001662
Ian Rogersd81871c2011-10-03 13:57:23 -07001663 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001664 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001666 * (besides the wasted space), but it indicates a flaw somewhere
1667 * down the line, possibly in the verifier.
1668 *
1669 * If we've substituted "always throw" instructions into the stream,
1670 * we are almost certainly going to have some dead code.
1671 */
1672 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 uint32_t insn_idx = 0;
1674 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001675 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001676 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001677 * may or may not be preceded by a padding NOP (for alignment).
1678 */
1679 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1680 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1681 insns[insn_idx] == Instruction::kArrayDataSignature ||
1682 (insns[insn_idx] == Instruction::NOP &&
1683 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1684 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1685 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001687 }
1688
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001690 if (dead_start < 0)
1691 dead_start = insn_idx;
1692 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001694 dead_start = -1;
1695 }
1696 }
1697 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001699 }
1700 }
jeffhaobdb76512011-09-07 11:43:16 -07001701 return true;
1702}
1703
Ian Rogersd81871c2011-10-03 13:57:23 -07001704bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001705#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001707 gDvm.verifierStats.instrsReexamined++;
1708 } else {
1709 gDvm.verifierStats.instrsExamined++;
1710 }
1711#endif
1712
1713 /*
1714 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001715 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001716 * control to another statement:
1717 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001718 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001719 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001720 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001721 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001722 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001723 * throw an exception that is handled by an encompassing "try"
1724 * block.
1725 *
1726 * We can also return, in which case there is no successor instruction
1727 * from this point.
1728 *
1729 * The behavior can be determined from the OpcodeFlags.
1730 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1732 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001733 Instruction::DecodedInstruction dec_insn(inst);
1734 int opcode_flag = inst->Flag();
1735
jeffhaobdb76512011-09-07 11:43:16 -07001736 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001737 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001738 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 // Generate processing back trace to debug verifier
Ian Rogers2c8a8572011-10-24 17:11:36 -07001740 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl << *work_line_.get();
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 }
jeffhaobdb76512011-09-07 11:43:16 -07001742
1743 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001744 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001745 * can throw an exception, we will copy/merge this into the "catch"
1746 * address rather than work_line, because we don't want the result
1747 * from the "successful" code path (e.g. a check-cast that "improves"
1748 * a type) to be visible to the exception handler.
1749 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1751 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001752 } else {
1753#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001755#endif
1756 }
1757
1758 switch (dec_insn.opcode_) {
1759 case Instruction::NOP:
1760 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001761 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001762 * a signature that looks like a NOP; if we see one of these in
1763 * the course of executing code then we have a problem.
1764 */
1765 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001767 }
1768 break;
1769
1770 case Instruction::MOVE:
1771 case Instruction::MOVE_FROM16:
1772 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775 case Instruction::MOVE_WIDE:
1776 case Instruction::MOVE_WIDE_FROM16:
1777 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
1780 case Instruction::MOVE_OBJECT:
1781 case Instruction::MOVE_OBJECT_FROM16:
1782 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785
1786 /*
1787 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001788 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001789 * might want to hold the result in an actual CPU register, so the
1790 * Dalvik spec requires that these only appear immediately after an
1791 * invoke or filled-new-array.
1792 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001793 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001794 * redundant with the reset done below, but it can make the debug info
1795 * easier to read in some cases.)
1796 */
1797 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
1800 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001802 break;
1803 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
1806
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001808 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 * This statement can only appear as the first instruction in an exception handler (though not
1810 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001811 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001812 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001814 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001816 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001818 }
1819 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1823 if (!GetMethodReturnType().IsUnknown()) {
1824 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1825 }
jeffhaobdb76512011-09-07 11:43:16 -07001826 }
1827 break;
1828 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001830 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 const RegType& return_type = GetMethodReturnType();
1832 if (!return_type.IsCategory1Types()) {
1833 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1834 } else {
1835 // Compilers may generate synthetic functions that write byte values into boolean fields.
1836 // Also, it may use integer values for boolean, byte, short, and character return types.
1837 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1838 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1839 ((return_type.IsBoolean() || return_type.IsByte() ||
1840 return_type.IsShort() || return_type.IsChar()) &&
1841 src_type.IsInteger()));
1842 /* check the register contents */
1843 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1844 if (failure_ != VERIFY_ERROR_NONE) {
1845 Fail(VERIFY_ERROR_GENERIC) << "return-1nr on invalid register v" << dec_insn.vA_;
1846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 }
1848 }
1849 break;
1850 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001852 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 const RegType& return_type = GetMethodReturnType();
1854 if (!return_type.IsCategory2Types()) {
1855 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1856 } else {
1857 /* check the register contents */
1858 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1859 if (failure_ != VERIFY_ERROR_NONE) {
1860 Fail(failure_) << "return-wide on invalid register pair v" << dec_insn.vA_;
1861 }
jeffhaobdb76512011-09-07 11:43:16 -07001862 }
1863 }
1864 break;
1865 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1867 const RegType& return_type = GetMethodReturnType();
1868 if (!return_type.IsReferenceTypes()) {
1869 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1870 } else {
1871 /* return_type is the *expected* return type, not register value */
1872 DCHECK(!return_type.IsZero());
1873 DCHECK(!return_type.IsUninitializedReference());
1874 // Verify that the reference in vAA is an instance of the type in "return_type". The Zero
1875 // type is allowed here. If the method is declared to return an interface, then any
1876 // initialized reference is acceptable.
1877 // Note GetClassFromRegister fails if the register holds an uninitialized reference, so
1878 // we do not allow them to be returned.
1879 Class* decl_class = return_type.GetClass();
1880 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
1881 if (res_class != NULL && failure_ == VERIFY_ERROR_NONE) {
1882 if (!decl_class->IsInterface() && !decl_class->IsAssignableFrom(res_class)) {
1883 Fail(VERIFY_ERROR_GENERIC) << "returning " << PrettyClassAndClassLoader(res_class)
1884 << ", declared " << PrettyClassAndClassLoader(res_class);
1885 }
jeffhaobdb76512011-09-07 11:43:16 -07001886 }
1887 }
1888 }
1889 break;
1890
1891 case Instruction::CONST_4:
1892 case Instruction::CONST_16:
1893 case Instruction::CONST:
1894 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
1897 case Instruction::CONST_HIGH16:
1898 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 work_line_->SetRegisterType(dec_insn.vA_,
1900 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902 case Instruction::CONST_WIDE_16:
1903 case Instruction::CONST_WIDE_32:
1904 case Instruction::CONST_WIDE:
1905 case Instruction::CONST_WIDE_HIGH16:
1906 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
1909 case Instruction::CONST_STRING:
1910 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001914 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001916 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1918 Fail(failure_) << "unable to resolve const-class " << dec_insn.vB_
1919 << " (" << bad_class_desc << ") in "
1920 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1921 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001922 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001924 }
1925 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001929 break;
1930 case Instruction::MONITOR_EXIT:
1931 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001932 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001933 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001934 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001935 * to the need to handle asynchronous exceptions, a now-deprecated
1936 * feature that Dalvik doesn't support.)
1937 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001938 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001939 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001941 * structured locking checks are working, the former would have
1942 * failed on the -enter instruction, and the latter is impossible.
1943 *
1944 * This is fortunate, because issue 3221411 prevents us from
1945 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001947 * some catch blocks (which will show up as "dead" code when
1948 * we skip them here); if we can't, then the code path could be
1949 * "live" so we still need to check it.
1950 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 opcode_flag &= ~Instruction::kThrow;
1952 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001956 /*
1957 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001958 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001959 * we don't try to address it.)
1960 *
1961 * If it fails, an exception is thrown, which we deal with later
1962 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1963 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001965 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1967 Fail(failure_) << "unable to resolve check-cast " << dec_insn.vB_
1968 << " (" << bad_class_desc << ") in "
1969 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1970 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001971 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1973 if (!orig_type.IsReferenceTypes()) {
1974 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1975 } else {
1976 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 }
1979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
1981 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001982 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1984 if (!tmp_type.IsReferenceTypes()) {
1985 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001986 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 /* make sure we can resolve the class; access check is important */
1988 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1989 if (res_class == NULL) {
1990 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
1991 Fail(failure_) << "unable to resolve instance of " << dec_insn.vC_
1992 << " (" << bad_class_desc << ") in "
1993 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1994 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1995 } else {
1996 /* result is boolean */
1997 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001998 }
jeffhaobdb76512011-09-07 11:43:16 -07001999 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002000 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
2002 case Instruction::ARRAY_LENGTH: {
2003 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
2004 if (failure_ == VERIFY_ERROR_NONE) {
2005 if (res_class != NULL && !res_class->IsArrayClass()) {
2006 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
2007 } else {
2008 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2009 }
2010 }
2011 break;
2012 }
2013 case Instruction::NEW_INSTANCE: {
2014 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002015 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2017 Fail(failure_) << "unable to resolve new-instance " << dec_insn.vB_
2018 << " (" << bad_class_desc << ") in "
2019 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2020 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2021 } else {
2022 /* can't create an instance of an interface or abstract class */
2023 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2024 Fail(VERIFY_ERROR_INSTANTIATION)
2025 << "new-instance on primitive, interface or abstract class"
2026 << PrettyDescriptor(res_class->GetDescriptor());
2027 } else {
2028 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2029 // Any registers holding previous allocations from this address that have not yet been
2030 // initialized must be marked invalid.
2031 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2032
2033 /* add the new uninitialized reference to the register state */
2034 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2035 }
2036 }
2037 break;
2038 }
2039 case Instruction::NEW_ARRAY: {
2040 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2041 if (res_class == NULL) {
2042 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
2043 Fail(failure_) << "unable to resolve new-array " << dec_insn.vC_
2044 << " (" << bad_class_desc << ") in "
2045 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2046 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002047 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002049 } else {
2050 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002052 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002054 }
2055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 }
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2059 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002060 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2062 Fail(failure_) << "unable to resolve filled-array " << dec_insn.vB_
2063 << " (" << bad_class_desc << ") in "
2064 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2065 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002066 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002068 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002069 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002070 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002072 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002074 just_set_result = true;
2075 }
2076 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 }
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::CMPL_FLOAT:
2079 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2081 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2082 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::CMPL_DOUBLE:
2085 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2087 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2088 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2092 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2093 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 case Instruction::THROW: {
2096 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2097 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2098 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2099 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2100 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002101 }
2102 }
2103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::GOTO:
2106 case Instruction::GOTO_16:
2107 case Instruction::GOTO_32:
2108 /* no effect on or use of registers */
2109 break;
2110
2111 case Instruction::PACKED_SWITCH:
2112 case Instruction::SPARSE_SWITCH:
2113 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 case Instruction::FILL_ARRAY_DATA: {
2118 /* Similar to the verification done for APUT */
2119 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2120 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002121 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 if (res_class != NULL) {
2123 Class* component_type = res_class->GetComponentType();
2124 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2125 component_type->IsPrimitiveVoid()) {
2126 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2127 << PrettyDescriptor(res_class->GetDescriptor());
2128 } else {
2129 const RegType& value_type = reg_types_.FromClass(component_type);
2130 DCHECK(!value_type.IsUnknown());
2131 // Now verify if the element width in the table matches the element width declared in
2132 // the array
2133 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2134 if (array_data[0] != Instruction::kArrayDataSignature) {
2135 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2136 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002137 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 // Since we don't compress the data in Dex, expect to see equal width of data stored
2139 // in the table and expected from the array class.
2140 if (array_data[1] != elem_width) {
2141 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2142 << " vs " << elem_width << ")";
2143 }
2144 }
2145 }
jeffhaobdb76512011-09-07 11:43:16 -07002146 }
2147 }
2148 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 }
jeffhaobdb76512011-09-07 11:43:16 -07002150 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 case Instruction::IF_NE: {
2152 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2153 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2154 bool mismatch = false;
2155 if (reg_type1.IsZero()) { // zero then integral or reference expected
2156 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2157 } else if (reg_type1.IsReferenceTypes()) { // both references?
2158 mismatch = !reg_type2.IsReferenceTypes();
2159 } else { // both integral?
2160 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2161 }
2162 if (mismatch) {
2163 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2164 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002165 }
2166 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 }
jeffhaobdb76512011-09-07 11:43:16 -07002168 case Instruction::IF_LT:
2169 case Instruction::IF_GE:
2170 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 case Instruction::IF_LE: {
2172 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2173 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2174 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2175 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2176 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002177 }
2178 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 }
jeffhaobdb76512011-09-07 11:43:16 -07002180 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 case Instruction::IF_NEZ: {
2182 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2183 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2184 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2185 }
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 case Instruction::IF_LTZ:
2189 case Instruction::IF_GEZ:
2190 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 case Instruction::IF_LEZ: {
2192 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2193 if (!reg_type.IsIntegralTypes()) {
2194 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2195 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2196 }
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 }
jeffhaobdb76512011-09-07 11:43:16 -07002199 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2201 break;
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2204 break;
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 VerifyAGet(dec_insn, reg_types_.Char(), true);
2207 break;
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 case Instruction::AGET:
2212 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2213 break;
jeffhaobdb76512011-09-07 11:43:16 -07002214 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 VerifyAGet(dec_insn, reg_types_.Long(), true);
2216 break;
2217 case Instruction::AGET_OBJECT:
2218 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 case Instruction::APUT_BOOLEAN:
2222 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2223 break;
2224 case Instruction::APUT_BYTE:
2225 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2226 break;
2227 case Instruction::APUT_CHAR:
2228 VerifyAPut(dec_insn, reg_types_.Char(), true);
2229 break;
2230 case Instruction::APUT_SHORT:
2231 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
2236 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
2239 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 VerifyIGet(dec_insn, reg_types_.Boolean(), true);
2245 break;
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::IGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 VerifyIGet(dec_insn, reg_types_.Byte(), true);
2248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::IGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 VerifyIGet(dec_insn, reg_types_.Char(), true);
2251 break;
jeffhaobdb76512011-09-07 11:43:16 -07002252 case Instruction::IGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 VerifyIGet(dec_insn, reg_types_.Short(), true);
2254 break;
2255 case Instruction::IGET:
2256 VerifyIGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
2258 case Instruction::IGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 VerifyIGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::IGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 VerifyIGet(dec_insn, reg_types_.JavaLangObject(), false);
2263 break;
jeffhaobdb76512011-09-07 11:43:16 -07002264
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 case Instruction::IPUT_BOOLEAN:
2266 VerifyIPut(dec_insn, reg_types_.Boolean(), true);
2267 break;
2268 case Instruction::IPUT_BYTE:
2269 VerifyIPut(dec_insn, reg_types_.Byte(), true);
2270 break;
2271 case Instruction::IPUT_CHAR:
2272 VerifyIPut(dec_insn, reg_types_.Char(), true);
2273 break;
2274 case Instruction::IPUT_SHORT:
2275 VerifyIPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::IPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 VerifyIPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::IPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 VerifyIPut(dec_insn, reg_types_.Long(), true);
2282 break;
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 VerifyIPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::SGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 VerifySGet(dec_insn, reg_types_.Boolean(), true);
2289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::SGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 VerifySGet(dec_insn, reg_types_.Byte(), true);
2292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::SGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 VerifySGet(dec_insn, reg_types_.Char(), true);
2295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::SGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 VerifySGet(dec_insn, reg_types_.Short(), true);
2298 break;
2299 case Instruction::SGET:
2300 VerifySGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::SGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002303 VerifySGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::SGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 VerifySGet(dec_insn, reg_types_.JavaLangObject(), false);
2307 break;
2308
2309 case Instruction::SPUT_BOOLEAN:
2310 VerifySPut(dec_insn, reg_types_.Boolean(), true);
2311 break;
2312 case Instruction::SPUT_BYTE:
2313 VerifySPut(dec_insn, reg_types_.Byte(), true);
2314 break;
2315 case Instruction::SPUT_CHAR:
2316 VerifySPut(dec_insn, reg_types_.Char(), true);
2317 break;
2318 case Instruction::SPUT_SHORT:
2319 VerifySPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::SPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 VerifySPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::SPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 VerifySPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::SPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 VerifySPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330
2331 case Instruction::INVOKE_VIRTUAL:
2332 case Instruction::INVOKE_VIRTUAL_RANGE:
2333 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::INVOKE_SUPER_RANGE: {
2335 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2336 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2337 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2338 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2339 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2340 if (failure_ == VERIFY_ERROR_NONE) {
2341 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2342 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002343 just_set_result = true;
2344 }
2345 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 }
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 case Instruction::INVOKE_DIRECT_RANGE: {
2349 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2350 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2351 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002352 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 * Some additional checks when calling a constructor. We know from the invocation arg check
2354 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2355 * that to require that called_method->klass is the same as this->klass or this->super,
2356 * allowing the latter only if the "this" argument is the same as the "this" argument to
2357 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002358 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 if (called_method->IsConstructor()) {
2360 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2361 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363
2364 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 if (this_type.IsZero()) {
2366 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002370 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002371
2372 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2374 if (this_class != method_->GetDeclaringClass()) {
2375 Fail(VERIFY_ERROR_GENERIC)
2376 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 }
2379 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 }
2383
2384 /* arg must be an uninitialized reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 if (this_type.IsInitialized()) {
2386 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2387 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002388 break;
2389 }
2390
2391 /*
2392 * Replace the uninitialized reference with an initialized
jeffhaod1f0fde2011-09-08 17:25:33 -07002393 * one, and clear the entry in the uninit map. We need to
jeffhaobdb76512011-09-07 11:43:16 -07002394 * do this for all registers that have the same object
2395 * instance in them, not just the "this" register.
2396 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 work_line_->MarkRefsAsInitialized(this_type);
2398 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002400 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2402 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002403 just_set_result = true;
2404 }
2405 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 }
jeffhaobdb76512011-09-07 11:43:16 -07002407 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 case Instruction::INVOKE_STATIC_RANGE: {
2409 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2410 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2411 if (failure_ == VERIFY_ERROR_NONE) {
2412 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2413 work_line_->SetResultRegisterType(return_type);
2414 just_set_result = true;
2415 }
jeffhaobdb76512011-09-07 11:43:16 -07002416 }
2417 break;
2418 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 case Instruction::INVOKE_INTERFACE_RANGE: {
2420 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2421 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2422 if (failure_ == VERIFY_ERROR_NONE) {
2423 Class* called_interface = abs_method->GetDeclaringClass();
2424 if (!called_interface->IsInterface()) {
2425 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2426 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
jeffhaobdb76512011-09-07 11:43:16 -07002428 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 /* Get the type of the "this" arg, which should either be a sub-interface of called
2430 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002431 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2433 if (failure_ == VERIFY_ERROR_NONE) {
2434 if (this_type.IsZero()) {
2435 /* null pointer always passes (and always fails at runtime) */
2436 } else {
2437 Class* this_class = this_type.GetClass();
2438 if (this_type.IsUninitializedReference() || this_class == NULL) {
2439 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized";
2440 break;
2441 }
2442 if (!this_class->IsObjectClass() && !called_interface->IsAssignableFrom(this_class)) {
2443 Fail(VERIFY_ERROR_GENERIC) << "unable to match abstract method '"
2444 << PrettyMethod(abs_method) << "' with "
2445 << PrettyDescriptor(this_class->GetDescriptor())
2446 << " interfaces";
2447 break;
2448 }
2449 }
jeffhaobdb76512011-09-07 11:43:16 -07002450 }
2451 }
jeffhaobdb76512011-09-07 11:43:16 -07002452 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 * We don't have an object instance, so we can't find the concrete method. However, all of
2454 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002455 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 const RegType& return_type = reg_types_.FromClass(abs_method->GetReturnType());
2457 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002458 just_set_result = true;
2459 }
2460 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 }
jeffhaobdb76512011-09-07 11:43:16 -07002462 case Instruction::NEG_INT:
2463 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::NEG_LONG:
2467 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521
2522 case Instruction::ADD_INT:
2523 case Instruction::SUB_INT:
2524 case Instruction::MUL_INT:
2525 case Instruction::REM_INT:
2526 case Instruction::DIV_INT:
2527 case Instruction::SHL_INT:
2528 case Instruction::SHR_INT:
2529 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::AND_INT:
2533 case Instruction::OR_INT:
2534 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::ADD_LONG:
2538 case Instruction::SUB_LONG:
2539 case Instruction::MUL_LONG:
2540 case Instruction::DIV_LONG:
2541 case Instruction::REM_LONG:
2542 case Instruction::AND_LONG:
2543 case Instruction::OR_LONG:
2544 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547 case Instruction::SHL_LONG:
2548 case Instruction::SHR_LONG:
2549 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 /* shift distance is Int, making these different from other binary operations */
2551 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::ADD_FLOAT:
2554 case Instruction::SUB_FLOAT:
2555 case Instruction::MUL_FLOAT:
2556 case Instruction::DIV_FLOAT:
2557 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::ADD_DOUBLE:
2561 case Instruction::SUB_DOUBLE:
2562 case Instruction::MUL_DOUBLE:
2563 case Instruction::DIV_DOUBLE:
2564 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::ADD_INT_2ADDR:
2568 case Instruction::SUB_INT_2ADDR:
2569 case Instruction::MUL_INT_2ADDR:
2570 case Instruction::REM_INT_2ADDR:
2571 case Instruction::SHL_INT_2ADDR:
2572 case Instruction::SHR_INT_2ADDR:
2573 case Instruction::USHR_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::AND_INT_2ADDR:
2577 case Instruction::OR_INT_2ADDR:
2578 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::ADD_LONG_2ADDR:
2585 case Instruction::SUB_LONG_2ADDR:
2586 case Instruction::MUL_LONG_2ADDR:
2587 case Instruction::DIV_LONG_2ADDR:
2588 case Instruction::REM_LONG_2ADDR:
2589 case Instruction::AND_LONG_2ADDR:
2590 case Instruction::OR_LONG_2ADDR:
2591 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::SHL_LONG_2ADDR:
2595 case Instruction::SHR_LONG_2ADDR:
2596 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::ADD_FLOAT_2ADDR:
2600 case Instruction::SUB_FLOAT_2ADDR:
2601 case Instruction::MUL_FLOAT_2ADDR:
2602 case Instruction::DIV_FLOAT_2ADDR:
2603 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::ADD_DOUBLE_2ADDR:
2607 case Instruction::SUB_DOUBLE_2ADDR:
2608 case Instruction::MUL_DOUBLE_2ADDR:
2609 case Instruction::DIV_DOUBLE_2ADDR:
2610 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::ADD_INT_LIT16:
2614 case Instruction::RSUB_INT:
2615 case Instruction::MUL_INT_LIT16:
2616 case Instruction::DIV_INT_LIT16:
2617 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002619 break;
2620 case Instruction::AND_INT_LIT16:
2621 case Instruction::OR_INT_LIT16:
2622 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::ADD_INT_LIT8:
2626 case Instruction::RSUB_INT_LIT8:
2627 case Instruction::MUL_INT_LIT8:
2628 case Instruction::DIV_INT_LIT8:
2629 case Instruction::REM_INT_LIT8:
2630 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002631 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002632 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::AND_INT_LIT8:
2636 case Instruction::OR_INT_LIT8:
2637 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640
2641 /*
2642 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002643 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002644 * inserted in the course of verification, we can expect to see it here.
2645 */
jeffhaob4df5142011-09-19 20:25:32 -07002646 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002650 case Instruction::UNUSED_EE:
2651 case Instruction::UNUSED_EF:
2652 case Instruction::UNUSED_F2:
2653 case Instruction::UNUSED_F3:
2654 case Instruction::UNUSED_F4:
2655 case Instruction::UNUSED_F5:
2656 case Instruction::UNUSED_F6:
2657 case Instruction::UNUSED_F7:
2658 case Instruction::UNUSED_F8:
2659 case Instruction::UNUSED_F9:
2660 case Instruction::UNUSED_FA:
2661 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002662 case Instruction::UNUSED_F0:
2663 case Instruction::UNUSED_F1:
2664 case Instruction::UNUSED_E3:
2665 case Instruction::UNUSED_E8:
2666 case Instruction::UNUSED_E7:
2667 case Instruction::UNUSED_E4:
2668 case Instruction::UNUSED_E9:
2669 case Instruction::UNUSED_FC:
2670 case Instruction::UNUSED_E5:
2671 case Instruction::UNUSED_EA:
2672 case Instruction::UNUSED_FD:
2673 case Instruction::UNUSED_E6:
2674 case Instruction::UNUSED_EB:
2675 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002676 case Instruction::UNUSED_3E:
2677 case Instruction::UNUSED_3F:
2678 case Instruction::UNUSED_40:
2679 case Instruction::UNUSED_41:
2680 case Instruction::UNUSED_42:
2681 case Instruction::UNUSED_43:
2682 case Instruction::UNUSED_73:
2683 case Instruction::UNUSED_79:
2684 case Instruction::UNUSED_7A:
2685 case Instruction::UNUSED_EC:
2686 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002687 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689
2690 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002691 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002692 * complain if an instruction is missing (which is desirable).
2693 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002695
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 if (failure_ != VERIFY_ERROR_NONE) {
2697 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002699 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002700 return false;
2701 } else {
2702 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002703 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002705 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002707 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002709 opcode_flag = Instruction::kThrow;
2710 }
2711 }
jeffhaobdb76512011-09-07 11:43:16 -07002712 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 * If we didn't just set the result register, clear it out. This ensures that you can only use
2714 * "move-result" immediately after the result is set. (We could check this statically, but it's
2715 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002716 */
2717 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002719 }
2720
jeffhaoa0a764a2011-09-16 10:43:38 -07002721 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002722 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2724 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2725 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002726 return false;
2727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2729 // next instruction isn't one.
2730 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002731 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 }
2733 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2734 if (next_line != NULL) {
2735 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2736 // needed.
2737 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002738 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 }
jeffhaobdb76512011-09-07 11:43:16 -07002740 } else {
2741 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 * We're not recording register data for the next instruction, so we don't know what the prior
2743 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002744 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002746 }
2747 }
2748
2749 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002750 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002751 *
2752 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002753 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002754 * somebody could get a reference field, check it for zero, and if the
2755 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002756 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002757 * that, and will reject the code.
2758 *
2759 * TODO: avoid re-fetching the branch target
2760 */
2761 if ((opcode_flag & Instruction::kBranch) != 0) {
2762 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002764 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002766 return false;
2767 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002768 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002770 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 }
jeffhaobdb76512011-09-07 11:43:16 -07002772 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002774 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 }
jeffhaobdb76512011-09-07 11:43:16 -07002776 }
2777
2778 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002779 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002780 *
2781 * We've already verified that the table is structurally sound, so we
2782 * just need to walk through and tag the targets.
2783 */
2784 if ((opcode_flag & Instruction::kSwitch) != 0) {
2785 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2786 const uint16_t* switch_insns = insns + offset_to_switch;
2787 int switch_count = switch_insns[1];
2788 int offset_to_targets, targ;
2789
2790 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2791 /* 0 = sig, 1 = count, 2/3 = first key */
2792 offset_to_targets = 4;
2793 } else {
2794 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002795 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002796 offset_to_targets = 2 + 2 * switch_count;
2797 }
2798
2799 /* verify each switch target */
2800 for (targ = 0; targ < switch_count; targ++) {
2801 int offset;
2802 uint32_t abs_offset;
2803
2804 /* offsets are 32-bit, and only partly endian-swapped */
2805 offset = switch_insns[offset_to_targets + targ * 2] |
2806 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 abs_offset = work_insn_idx_ + offset;
2808 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2809 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002810 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 }
2812 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002813 return false;
2814 }
2815 }
2816
2817 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2819 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002820 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2822 bool within_catch_all = false;
2823 DexFile::CatchHandlerIterator iterator =
2824 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002825
2826 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2828 within_catch_all = true;
2829 }
jeffhaobdb76512011-09-07 11:43:16 -07002830 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2832 * "work_regs", because at runtime the exception will be thrown before the instruction
2833 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002834 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 }
jeffhaobdb76512011-09-07 11:43:16 -07002838 }
2839
2840 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2842 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002843 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002845 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 * The state in work_line reflects the post-execution state. If the current instruction is a
2847 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002848 * it will do so before grabbing the lock).
2849 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2851 Fail(VERIFY_ERROR_GENERIC)
2852 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002853 return false;
2854 }
2855 }
2856 }
2857
jeffhaod1f0fde2011-09-08 17:25:33 -07002858 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 if ((opcode_flag & Instruction::kReturn) != 0) {
2860 if(!work_line_->VerifyMonitorStackEmpty()) {
2861 return false;
2862 }
jeffhaobdb76512011-09-07 11:43:16 -07002863 }
2864
2865 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002866 * Update start_guess. Advance to the next instruction of that's
2867 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002868 * neither of those exists we're in a return or throw; leave start_guess
2869 * alone and let the caller sort it out.
2870 */
2871 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002873 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2874 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002876 }
2877
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2879 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002880
2881 return true;
2882}
2883
Ian Rogersd81871c2011-10-03 13:57:23 -07002884Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2885 const Class* referrer = method_->GetDeclaringClass();
2886 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2887 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002888
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 if (res_class == NULL) {
2890 Thread::Current()->ClearException();
2891 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2892 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2893 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2894 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2895 << res_class->GetDescriptor()->ToModifiedUtf8();
2896 }
2897 return res_class;
2898}
2899
2900Class* DexVerifier::GetCaughtExceptionType() {
2901 Class* common_super = NULL;
2902 if (code_item_->tries_size_ != 0) {
2903 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2904 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2905 for (uint32_t i = 0; i < handlers_size; i++) {
2906 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2907 for (; !iterator.HasNext(); iterator.Next()) {
2908 DexFile::CatchHandlerItem handler = iterator.Get();
2909 if (handler.address_ == (uint32_t) work_insn_idx_) {
2910 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2911 common_super = JavaLangThrowable();
2912 } else {
2913 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2914 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2915 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2916 * test, so is essentially harmless.
2917 */
2918 if (klass == NULL) {
2919 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2920 << handler.type_idx_ << " ("
2921 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2922 return NULL;
2923 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2924 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2925 return NULL;
2926 } else if (common_super == NULL) {
2927 common_super = klass;
2928 } else {
2929 common_super = RegType::ClassJoin(common_super, klass);
2930 }
2931 }
2932 }
2933 }
2934 handlers_ptr = iterator.GetData();
2935 }
2936 }
2937 if (common_super == NULL) {
2938 /* no catch blocks, or no catches with classes we can find */
2939 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2940 }
2941 return common_super;
2942}
2943
2944Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2945 Class* referrer = method_->GetDeclaringClass();
2946 DexCache* dex_cache = referrer->GetDexCache();
2947 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2948 if (res_method == NULL) {
2949 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2950 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2951 if (klass == NULL) {
2952 DCHECK(failure_ != VERIFY_ERROR_NONE);
2953 return NULL;
2954 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002955 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2957 if (is_direct) {
2958 res_method = klass->FindDirectMethod(name, signature);
2959 } else if (klass->IsInterface()) {
2960 res_method = klass->FindInterfaceMethod(name, signature);
2961 } else {
2962 res_method = klass->FindVirtualMethod(name, signature);
2963 }
2964 if (res_method != NULL) {
2965 dex_cache->SetResolvedMethod(method_idx, res_method);
2966 } else {
2967 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2968 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2969 << " " << signature;
2970 return NULL;
2971 }
2972 }
2973 /* Check if access is allowed. */
2974 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2975 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2976 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2977 return NULL;
2978 }
2979 return res_method;
2980}
2981
2982Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2983 MethodType method_type, bool is_range, bool is_super) {
2984 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2985 // we're making.
2986 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2987 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2988 if (res_method == NULL) {
2989 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2990 const char* method_name = dex_file_->GetMethodName(method_id);
2991 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002992 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve method " << dec_insn.vB_ << ": "
2994 << class_descriptor << "." << method_name << " " << method_signature;
2995 return NULL;
2996 }
2997 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2998 // enforce them here.
2999 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3000 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3001 << PrettyMethod(res_method);
3002 return NULL;
3003 }
3004 // See if the method type implied by the invoke instruction matches the access flags for the
3005 // target method.
3006 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3007 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3008 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3009 ) {
3010 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3011 << PrettyMethod(res_method);
3012 return NULL;
3013 }
3014 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3015 // has a vtable entry for the target method.
3016 if (is_super) {
3017 DCHECK(method_type == METHOD_VIRTUAL);
3018 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3019 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3020 if (super == NULL) { // Only Object has no super class
3021 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3022 << " to super " << PrettyMethod(res_method);
3023 } else {
3024 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3025 << " to super " << PrettyDescriptor(super->GetDescriptor())
3026 << "." << res_method->GetName()->ToModifiedUtf8()
3027 << " " << res_method->GetSignature()->ToModifiedUtf8();
3028
3029 }
3030 return NULL;
3031 }
3032 }
3033 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3034 // match the call to the signature. Also, we might might be calling through an abstract method
3035 // definition (which doesn't have register count values).
3036 int expected_args = dec_insn.vA_;
3037 /* caught by static verifier */
3038 DCHECK(is_range || expected_args <= 5);
3039 if (expected_args > code_item_->outs_size_) {
3040 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3041 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3042 return NULL;
3043 }
3044 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3045 if (sig[0] != '(') {
3046 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3047 << " as descriptor doesn't start with '(': " << sig;
3048 return NULL;
3049 }
jeffhaobdb76512011-09-07 11:43:16 -07003050 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 * Check the "this" argument, which must be an instance of the class
3052 * that declared the method. For an interface class, we don't do the
3053 * full interface merge, so we can't do a rigorous check here (which
3054 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003055 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 int actual_args = 0;
3057 if (!res_method->IsStatic()) {
3058 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3059 if (failure_ != VERIFY_ERROR_NONE) {
3060 return NULL;
3061 }
3062 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3063 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3064 return NULL;
3065 }
3066 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3067 Class* actual_this_ref = actual_arg_type.GetClass();
3068 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
3069 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '"
3070 << PrettyDescriptor(actual_this_ref->GetDescriptor()) << "' not instance of '"
3071 << PrettyDescriptor(res_method->GetDeclaringClass()->GetDescriptor()) << "'";
3072 return NULL;
3073 }
3074 }
3075 actual_args++;
3076 }
3077 /*
3078 * Process the target method's signature. This signature may or may not
3079 * have been verified, so we can't assume it's properly formed.
3080 */
3081 size_t sig_offset = 0;
3082 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3083 if (actual_args >= expected_args) {
3084 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3085 << "'. Expected " << expected_args << " args, found more ("
3086 << sig.substr(sig_offset) << ")";
3087 return NULL;
3088 }
3089 std::string descriptor;
3090 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3091 size_t end;
3092 if (sig[sig_offset] == 'L') {
3093 end = sig.find(';', sig_offset);
3094 } else {
3095 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3096 if (sig[end] == 'L') {
3097 end = sig.find(';', end);
3098 }
3099 }
3100 if (end == std::string::npos) {
3101 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3102 << "bad signature component '" << sig << "' (missing ';')";
3103 return NULL;
3104 }
3105 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3106 sig_offset = end;
3107 } else {
3108 descriptor = sig[sig_offset];
3109 }
3110 const RegType& reg_type =
3111 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
3112 if (reg_type.IsUnknown()) {
3113 DCHECK(Thread::Current()->IsExceptionPending());
3114 Thread::Current()->ClearException();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003115 LOG(WARNING) << "Unable to resolve descriptor in signature " << descriptor
3116 << " using Object";
3117 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3118 if (!work_line_->VerifyRegisterType(get_reg, reg_types_.JavaLangObject())) {
3119 return NULL;
3120 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 } else {
3122 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3123 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3124 return NULL;
3125 }
3126 }
3127 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3128 }
3129 if (sig[sig_offset] != ')') {
3130 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3131 return NULL;
3132 }
3133 if (actual_args != expected_args) {
3134 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3135 << " expected " << expected_args << " args, found " << actual_args;
3136 return NULL;
3137 } else {
3138 return res_method;
3139 }
3140}
3141
3142void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3143 const RegType& insn_type, bool is_primitive) {
3144 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3145 if (!index_type.IsArrayIndexTypes()) {
3146 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3147 } else {
3148 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3149 if (failure_ == VERIFY_ERROR_NONE) {
3150 if (array_class == NULL) {
3151 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3152 // instruction type. TODO: have a proper notion of bottom here.
3153 if (!is_primitive || insn_type.IsCategory1Types()) {
3154 // Reference or category 1
3155 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3156 } else {
3157 // Category 2
3158 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3159 }
3160 } else {
3161 /* verify the class */
3162 Class* component_class = array_class->GetComponentType();
3163 const RegType& component_type = reg_types_.FromClass(component_class);
3164 if (!array_class->IsArrayClass()) {
3165 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3166 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3167 } else if (component_class->IsPrimitive() && !is_primitive) {
3168 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3169 << PrettyDescriptor(array_class->GetDescriptor())
3170 << " source for aget-object";
3171 } else if (!component_class->IsPrimitive() && is_primitive) {
3172 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3173 << PrettyDescriptor(array_class->GetDescriptor())
3174 << " source for category 1 aget";
3175 } else if (is_primitive && !insn_type.Equals(component_type) &&
3176 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3177 (insn_type.IsLong() && component_type.IsDouble()))) {
3178 Fail(VERIFY_ERROR_GENERIC) << "array type "
3179 << PrettyDescriptor(array_class->GetDescriptor())
3180 << " incompatible with aget of type " << insn_type;
3181 } else {
3182 // Use knowledge of the field type which is stronger than the type inferred from the
3183 // instruction, which can't differentiate object types and ints from floats, longs from
3184 // doubles.
3185 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3186 }
3187 }
3188 }
3189 }
3190}
3191
3192void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3193 const RegType& insn_type, bool is_primitive) {
3194 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3195 if (!index_type.IsArrayIndexTypes()) {
3196 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3197 } else {
3198 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3199 if (failure_ == VERIFY_ERROR_NONE) {
3200 if (array_class == NULL) {
3201 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3202 // instruction type.
3203 } else {
3204 /* verify the class */
3205 Class* component_class = array_class->GetComponentType();
3206 const RegType& component_type = reg_types_.FromClass(component_class);
3207 if (!array_class->IsArrayClass()) {
3208 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3209 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3210 } else if (component_class->IsPrimitive() && !is_primitive) {
3211 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3212 << PrettyDescriptor(array_class->GetDescriptor())
3213 << " source for aput-object";
3214 } else if (!component_class->IsPrimitive() && is_primitive) {
3215 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3216 << PrettyDescriptor(array_class->GetDescriptor())
3217 << " source for category 1 aput";
3218 } else if (is_primitive && !insn_type.Equals(component_type) &&
3219 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3220 (insn_type.IsLong() && component_type.IsDouble()))) {
3221 Fail(VERIFY_ERROR_GENERIC) << "array type "
3222 << PrettyDescriptor(array_class->GetDescriptor())
3223 << " incompatible with aput of type " << insn_type;
3224 } else {
3225 // The instruction agrees with the type of array, confirm the value to be stored does too
3226 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3227 }
3228 }
3229 }
3230 }
3231}
3232
3233Field* DexVerifier::GetStaticField(int field_idx) {
3234 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3235 if (field == NULL) {
3236 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3237 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3238 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003239 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 DCHECK(Thread::Current()->IsExceptionPending());
3241 Thread::Current()->ClearException();
3242 return NULL;
3243 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3244 field->GetAccessFlags())) {
3245 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3246 << " from " << PrettyClass(method_->GetDeclaringClass());
3247 return NULL;
3248 } else if (!field->IsStatic()) {
3249 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3250 return NULL;
3251 } else {
3252 return field;
3253 }
3254}
3255
3256void DexVerifier::VerifySGet(const Instruction::DecodedInstruction& dec_insn,
3257 const RegType& insn_type, bool is_primitive) {
3258 Field* field = GetStaticField(dec_insn.vB_);
3259 if (field != NULL) {
3260 DCHECK(field->GetDeclaringClass()->IsResolved());
3261 Class* field_class = field->GetType();
3262 Class* insn_class = insn_type.GetClass();
3263 if (is_primitive) {
3264 if (field_class == insn_class ||
3265 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3266 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3267 // expected that read is of the correct primitive type or that int reads are reading
3268 // floats or long reads are reading doubles
3269 } else {
3270 // This is a global failure rather than a class change failure as the instructions and
3271 // the descriptors for the type should have been consistent within the same file at
3272 // compile time
3273 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3274 << " to be of type " << PrettyClass(insn_class)
3275 << " but found type " << PrettyClass(field_class) << " in sget";
3276 return;
3277 }
3278 } else {
3279 if (!insn_class->IsAssignableFrom(field_class)) {
3280 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3281 << " to be of type " << PrettyClass(insn_class)
3282 << " but found type " << PrettyClass(field_class)
3283 << " in sget-object";
3284 return;
3285 }
3286 }
3287 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3288 }
3289}
3290
3291void DexVerifier::VerifySPut(const Instruction::DecodedInstruction& dec_insn,
3292 const RegType& insn_type, bool is_primitive) {
3293 Field* field = GetStaticField(dec_insn.vB_);
3294 if (field != NULL) {
3295 DCHECK(field->GetDeclaringClass()->IsResolved());
3296 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3297 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final static field " << PrettyField(field)
3298 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3299 return;
3300 }
3301 Class* field_class = field->GetType();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003302 const RegType& field_type = reg_types_.FromClass(field_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 Class* insn_class = insn_type.GetClass();
3304 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003305 // Primitive field assignability rules are weaker than regular assignability rules
3306 bool instruction_compatible;
3307 bool value_compatible;
3308 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3309 if (field_type.IsIntegralTypes()) {
3310 instruction_compatible = insn_type.IsIntegralTypes();
3311 value_compatible = value_type.IsIntegralTypes();
3312 } else if (field_type.IsFloat()) {
3313 instruction_compatible = insn_type.IsInteger(); // no sput-float, so expect sput-int
3314 value_compatible = value_type.IsFloatTypes();
3315 } else if (field_type.IsLong()) {
3316 instruction_compatible = insn_type.IsLong();
3317 value_compatible = value_type.IsLongTypes();
3318 } else if (field_type.IsDouble()) {
3319 instruction_compatible = insn_type.IsLong(); // no sput-double, so expect sput-long
3320 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003322 instruction_compatible = false; // reference field with primitive store
3323 value_compatible = false; // unused
3324 }
3325 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 // This is a global failure rather than a class change failure as the instructions and
3327 // the descriptors for the type should have been consistent within the same file at
3328 // compile time
3329 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3330 << " to be of type " << PrettyClass(insn_class)
3331 << " but found type " << PrettyClass(field_class) << " in sput";
3332 return;
3333 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003334 if (!value_compatible) {
3335 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3336 << " of type " << value_type
3337 << " but expected " << field_type
3338 << " for store to " << PrettyField(field) << " in sput";
3339 return;
3340 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 } else {
3342 if (!insn_class->IsAssignableFrom(field_class)) {
3343 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3344 << " to be compatible with type " << insn_type
3345 << " but found type " << PrettyClass(field_class)
3346 << " in sput-object";
3347 return;
3348 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003349 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 }
3352}
3353
3354Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3355 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3356 if (field == NULL) {
3357 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3358 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3359 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003360 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 DCHECK(Thread::Current()->IsExceptionPending());
3362 Thread::Current()->ClearException();
3363 return NULL;
3364 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3365 field->GetAccessFlags())) {
3366 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3367 << " from " << PrettyClass(method_->GetDeclaringClass());
3368 return NULL;
3369 } else if (field->IsStatic()) {
3370 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3371 << " to not be static";
3372 return NULL;
3373 } else if (obj_type.IsZero()) {
3374 // Cannot infer and check type, however, access will cause null pointer exception
3375 return field;
3376 } else if(obj_type.IsUninitializedReference() &&
3377 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3378 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3379 // Field accesses through uninitialized references are only allowable for constructors where
3380 // the field is declared in this class
3381 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3382 << " of a not fully initialized object within the context of "
3383 << PrettyMethod(method_);
3384 return NULL;
3385 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3386 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3387 // of C1. For resolution to occur the declared class of the field must be compatible with
3388 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3389 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3390 << " from object of type " << PrettyClass(obj_type.GetClass());
3391 return NULL;
3392 } else {
3393 return field;
3394 }
3395}
3396
3397void DexVerifier::VerifyIGet(const Instruction::DecodedInstruction& dec_insn,
3398 const RegType& insn_type, bool is_primitive) {
3399 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3400 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3401 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 Class* field_class = field->GetType();
3403 Class* insn_class = insn_type.GetClass();
3404 if (is_primitive) {
3405 if (field_class == insn_class ||
3406 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3407 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3408 // expected that read is of the correct primitive type or that int reads are reading
3409 // floats or long reads are reading doubles
3410 } else {
3411 // This is a global failure rather than a class change failure as the instructions and
3412 // the descriptors for the type should have been consistent within the same file at
3413 // compile time
3414 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3415 << " to be of type " << PrettyClass(insn_class)
3416 << " but found type " << PrettyClass(field_class) << " in iget";
3417 return;
3418 }
3419 } else {
3420 if (!insn_class->IsAssignableFrom(field_class)) {
3421 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3422 << " to be compatible with type " << PrettyClass(insn_class)
3423 << " but found type " << PrettyClass(field_class) << " in iget-object";
3424 return;
3425 }
3426 }
3427 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3428 }
3429}
3430
3431void DexVerifier::VerifyIPut(const Instruction::DecodedInstruction& dec_insn,
3432 const RegType& insn_type, bool is_primitive) {
3433 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3434 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3435 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3437 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3438 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3439 return;
3440 }
3441 Class* field_class = field->GetType();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003442 const RegType& field_type = reg_types_.FromClass(field_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 Class* insn_class = insn_type.GetClass();
3444 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003445 // Primitive field assignability rules are weaker than regular assignability rules
3446 bool instruction_compatible;
3447 bool value_compatible;
3448 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3449 if (field_type.IsIntegralTypes()) {
3450 instruction_compatible = insn_type.IsIntegralTypes();
3451 value_compatible = value_type.IsIntegralTypes();
3452 } else if (field_type.IsFloat()) {
3453 instruction_compatible = insn_type.IsInteger(); // no iput-float, so expect iput-int
3454 value_compatible = value_type.IsFloatTypes();
3455 } else if (field_type.IsLong()) {
3456 instruction_compatible = insn_type.IsLong();
3457 value_compatible = value_type.IsLongTypes();
3458 } else if (field_type.IsDouble()) {
3459 instruction_compatible = insn_type.IsLong(); // no iput-double, so expect iput-long
3460 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003462 instruction_compatible = false; // reference field with primitive store
3463 value_compatible = false; // unused
3464 }
3465 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 // This is a global failure rather than a class change failure as the instructions and
3467 // the descriptors for the type should have been consistent within the same file at
3468 // compile time
3469 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3470 << " to be of type " << PrettyClass(insn_class)
3471 << " but found type " << PrettyClass(field_class) << " in iput";
3472 return;
3473 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003474 if (!value_compatible) {
3475 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3476 << " of type " << value_type
3477 << " but expected " << field_type
3478 << " for store to " << PrettyField(field) << " in iput";
3479 return;
3480 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 } else {
3482 if (!insn_class->IsAssignableFrom(field_class)) {
3483 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogers2c8a8572011-10-24 17:11:36 -07003484 << " to be compatible with type " << insn_type
3485 << " but found type " << PrettyClass(field_class)
3486 << " in iput-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 return;
3488 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003489 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 }
3492}
3493
3494bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3495 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3496 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3497 return false;
3498 }
3499 return true;
3500}
3501
3502void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3503 Class* res_class, bool is_range) {
3504 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3505 /*
3506 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3507 * list and fail. It's legal, if silly, for arg_count to be zero.
3508 */
3509 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3510 uint32_t arg_count = dec_insn.vA_;
3511 for (size_t ui = 0; ui < arg_count; ui++) {
3512 uint32_t get_reg;
3513
3514 if (is_range)
3515 get_reg = dec_insn.vC_ + ui;
3516 else
3517 get_reg = dec_insn.arg_[ui];
3518
3519 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3520 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3521 << ") not valid";
3522 return;
3523 }
3524 }
3525}
3526
3527void DexVerifier::ReplaceFailingInstruction() {
3528 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3529 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3530 VerifyErrorRefType ref_type;
3531 switch (inst->Opcode()) {
3532 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003533 case Instruction::CHECK_CAST:
3534 case Instruction::INSTANCE_OF:
3535 case Instruction::NEW_INSTANCE:
3536 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003538 case Instruction::FILLED_NEW_ARRAY_RANGE:
3539 ref_type = VERIFY_ERROR_REF_CLASS;
3540 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003542 case Instruction::IGET_BOOLEAN:
3543 case Instruction::IGET_BYTE:
3544 case Instruction::IGET_CHAR:
3545 case Instruction::IGET_SHORT:
3546 case Instruction::IGET_WIDE:
3547 case Instruction::IGET_OBJECT:
3548 case Instruction::IPUT:
3549 case Instruction::IPUT_BOOLEAN:
3550 case Instruction::IPUT_BYTE:
3551 case Instruction::IPUT_CHAR:
3552 case Instruction::IPUT_SHORT:
3553 case Instruction::IPUT_WIDE:
3554 case Instruction::IPUT_OBJECT:
3555 case Instruction::SGET:
3556 case Instruction::SGET_BOOLEAN:
3557 case Instruction::SGET_BYTE:
3558 case Instruction::SGET_CHAR:
3559 case Instruction::SGET_SHORT:
3560 case Instruction::SGET_WIDE:
3561 case Instruction::SGET_OBJECT:
3562 case Instruction::SPUT:
3563 case Instruction::SPUT_BOOLEAN:
3564 case Instruction::SPUT_BYTE:
3565 case Instruction::SPUT_CHAR:
3566 case Instruction::SPUT_SHORT:
3567 case Instruction::SPUT_WIDE:
3568 case Instruction::SPUT_OBJECT:
3569 ref_type = VERIFY_ERROR_REF_FIELD;
3570 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003572 case Instruction::INVOKE_VIRTUAL_RANGE:
3573 case Instruction::INVOKE_SUPER:
3574 case Instruction::INVOKE_SUPER_RANGE:
3575 case Instruction::INVOKE_DIRECT:
3576 case Instruction::INVOKE_DIRECT_RANGE:
3577 case Instruction::INVOKE_STATIC:
3578 case Instruction::INVOKE_STATIC_RANGE:
3579 case Instruction::INVOKE_INTERFACE:
3580 case Instruction::INVOKE_INTERFACE_RANGE:
3581 ref_type = VERIFY_ERROR_REF_METHOD;
3582 break;
jeffhaobdb76512011-09-07 11:43:16 -07003583 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003584 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003585 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003586 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3588 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3589 // instruction, so assert it.
3590 size_t width = inst->SizeInCodeUnits();
3591 CHECK_GT(width, 1u);
3592 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3593 // NOPs
3594 for (size_t i = 2; i < width; i++) {
3595 insns[work_insn_idx_ + i] = Instruction::NOP;
3596 }
3597 // Encode the opcode, with the failure code in the high byte
3598 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3599 (failure_ << 8) | // AA - component
3600 (ref_type << (8 + kVerifyErrorRefTypeShift));
3601 insns[work_insn_idx_] = new_instruction;
3602 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3603 // rewrote, so nothing to do here.
jeffhaobdb76512011-09-07 11:43:16 -07003604}
jeffhaoba5ebb92011-08-25 17:24:37 -07003605
Ian Rogersd81871c2011-10-03 13:57:23 -07003606bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3607 const bool merge_debug = true;
3608 bool changed = true;
3609 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3610 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003611 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 * We haven't processed this instruction before, and we haven't touched the registers here, so
3613 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3614 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003615 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003617 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3619 copy->CopyFromLine(target_line);
3620 changed = target_line->MergeRegisters(merge_line);
3621 if (failure_ != VERIFY_ERROR_NONE) {
3622 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003623 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003624 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3626 << *copy.get() << " MERGE" << std::endl
3627 << *merge_line << " ==" << std::endl
3628 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003629 }
3630 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 if (changed) {
3632 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003633 }
3634 return true;
3635}
3636
Ian Rogersd81871c2011-10-03 13:57:23 -07003637void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3638 size_t* log2_max_gc_pc) {
3639 size_t local_gc_points = 0;
3640 size_t max_insn = 0;
3641 size_t max_ref_reg = -1;
3642 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3643 if (insn_flags_[i].IsGcPoint()) {
3644 local_gc_points++;
3645 max_insn = i;
3646 RegisterLine* line = reg_table_.GetLine(i);
3647 max_ref_reg = line->GetMaxReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003648 }
3649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 *gc_points = local_gc_points;
3651 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3652 size_t i = 0;
3653 while ((1U << i) < max_insn) {
3654 i++;
3655 }
3656 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003657}
3658
Ian Rogersd81871c2011-10-03 13:57:23 -07003659ByteArray* DexVerifier::GenerateGcMap() {
3660 size_t num_entries, ref_bitmap_bits, pc_bits;
3661 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3662 // There's a single byte to encode the size of each bitmap
3663 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3664 // TODO: either a better GC map format or per method failures
3665 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3666 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003667 return NULL;
3668 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003669 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3670 // There are 2 bytes to encode the number of entries
3671 if (num_entries >= 65536) {
3672 // TODO: either a better GC map format or per method failures
3673 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3674 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003675 return NULL;
3676 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003678 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003680 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 pc_bytes = 1;
3682 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003683 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003685 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003686 // TODO: either a better GC map format or per method failures
3687 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3688 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3689 return NULL;
3690 }
3691 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3692 ByteArray* table = ByteArray::Alloc(table_size);
3693 if (table == NULL) {
3694 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3695 return NULL;
3696 }
3697 // Write table header
3698 table->Set(0, format);
3699 table->Set(1, ref_bitmap_bytes);
3700 table->Set(2, num_entries & 0xFF);
3701 table->Set(3, (num_entries >> 8) & 0xFF);
3702 // Write table data
3703 size_t offset = 4;
3704 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3705 if (insn_flags_[i].IsGcPoint()) {
3706 table->Set(offset, i & 0xFF);
3707 offset++;
3708 if (pc_bytes == 2) {
3709 table->Set(offset, (i >> 8) & 0xFF);
3710 offset++;
3711 }
3712 RegisterLine* line = reg_table_.GetLine(i);
3713 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3714 offset += ref_bitmap_bytes;
3715 }
3716 }
3717 DCHECK(offset == table_size);
3718 return table;
3719}
jeffhaoa0a764a2011-09-16 10:43:38 -07003720
Ian Rogersd81871c2011-10-03 13:57:23 -07003721void DexVerifier::VerifyGcMap() {
3722 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3723 // that the table data is well formed and all references are marked (or not) in the bitmap
3724 PcToReferenceMap map(method_);
3725 size_t map_index = 0;
3726 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3727 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3728 if (insn_flags_[i].IsGcPoint()) {
3729 CHECK_LT(map_index, map.NumEntries());
3730 CHECK_EQ(map.GetPC(map_index), i);
3731 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3732 map_index++;
3733 RegisterLine* line = reg_table_.GetLine(i);
3734 for(size_t j = 0; j < code_item_->registers_size_; j++) {
3735 if (line->GetRegisterType(j).IsReference()) {
3736 CHECK_LT(j / 8, map.RegWidth());
3737 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3738 } else if ((j / 8) < map.RegWidth()) {
3739 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3740 } else {
3741 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3742 }
3743 }
3744 } else {
3745 CHECK(reg_bitmap == NULL);
3746 }
3747 }
3748}
jeffhaoa0a764a2011-09-16 10:43:38 -07003749
Ian Rogersd81871c2011-10-03 13:57:23 -07003750Class* DexVerifier::JavaLangThrowable() {
3751 if (java_lang_throwable_ == NULL) {
3752 java_lang_throwable_ =
3753 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3754 DCHECK(java_lang_throwable_ != NULL);
3755 }
3756 return java_lang_throwable_;
3757}
3758
3759const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3760 size_t num_entries = NumEntries();
3761 // Do linear or binary search?
3762 static const size_t kSearchThreshold = 8;
3763 if (num_entries < kSearchThreshold) {
3764 for (size_t i = 0; i < num_entries; i++) {
3765 if (GetPC(i) == dex_pc) {
3766 return GetBitMap(i);
3767 }
3768 }
3769 } else {
3770 int lo = 0;
3771 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003772 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003773 int mid = (hi + lo) / 2;
3774 int mid_pc = GetPC(mid);
3775 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003776 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003778 hi = mid - 1;
3779 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003781 }
3782 }
3783 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 if (error_if_not_present) {
3785 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3786 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003787 return NULL;
3788}
3789
Ian Rogersd81871c2011-10-03 13:57:23 -07003790} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003791} // namespace art