blob: 878069bd05eb520182742718337c97a002d6232a [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 Rogersd81871c2011-10-03 13:57:23 -070020std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
21 return os << (int)rhs;
22}
jeffhaobdb76512011-09-07 11:43:16 -070023
Ian Rogersd81871c2011-10-03 13:57:23 -070024static const char type_chars[RegType::kRegTypeMAX + 1 /* for '\0' */ ] = "UX01ZyYhHcibBsSCIFNnJjDdL";
25
26void RegType::Dump(std::ostream& os) const {
27 DCHECK(type_ < kRegTypeMAX);
28 os << type_chars[type_];
29 if (type_ == kRegTypeReference) {
30 if (IsUninitializedReference()) {
31 os << "[uninitialized-" << PrettyClass(GetClass())
32 << ", allocated@" << allocation_pc_ <<"]";
33 } else {
34 os << "[" << PrettyDescriptor(GetClass()->GetDescriptor()) << "]";
35 }
36 }
37}
38
39const RegType& RegType::HighHalf(RegTypeCache* cache) const {
40 CHECK(IsLowHalf());
41 if (type_ == kRegTypeLongLo) {
42 return cache->FromType(kRegTypeLongHi);
43 } else if (type_ == kRegTypeDoubleLo) {
44 return cache->FromType(kRegTypeDoubleHi);
45 } else {
46 return cache->FromType(kRegTypeConstHi);
47 }
48}
49
50/*
51 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
52 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
53 * 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
54 * is the deepest (lowest upper bound) parent of S and T).
55 *
56 * This operation applies for regular classes and arrays, however, for interface types there needn't
57 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
58 * introducing sets of types, however, the only operation permissible on an interface is
59 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
60 * types until an invoke-interface call on the interface typed reference at runtime and allow
61 * the perversion of Object being assignable to an interface type (note, however, that we don't
62 * allow assignment of Object or Interface to any concrete class and are therefore type safe;
63 * further the Join on a Object cannot result in a sub-class by definition).
64 */
65Class* RegType::ClassJoin(Class* s, Class* t) {
66 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
67 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
68 if (s == t) {
69 return s;
70 } else if (s->IsAssignableFrom(t)) {
71 return s;
72 } else if (t->IsAssignableFrom(s)) {
73 return t;
74 } else if (s->IsArrayClass() && t->IsArrayClass()) {
75 Class* s_ct = s->GetComponentType();
76 Class* t_ct = t->GetComponentType();
77 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
78 // Given the types aren't the same, if either array is of primitive types then the only
79 // common parent is java.lang.Object
80 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
81 DCHECK(result->IsObjectClass());
82 return result;
83 }
84 Class* common_elem = ClassJoin(s_ct, t_ct);
85 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
86 const ClassLoader* class_loader = s->GetClassLoader();
87 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
88 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
89 DCHECK(array_class != NULL);
90 return array_class;
91 } else {
92 size_t s_depth = s->Depth();
93 size_t t_depth = t->Depth();
94 // Get s and t to the same depth in the hierarchy
95 if (s_depth > t_depth) {
96 while (s_depth > t_depth) {
97 s = s->GetSuperClass();
98 s_depth--;
99 }
100 } else {
101 while (t_depth > s_depth) {
102 t = t->GetSuperClass();
103 t_depth--;
104 }
105 }
106 // Go up the hierarchy until we get to the common parent
107 while (s != t) {
108 s = s->GetSuperClass();
109 t = t->GetSuperClass();
110 }
111 return s;
112 }
113}
114
115const RegType& RegType::VerifyAgainst(const RegType& check_type, RegTypeCache* reg_types) const {
116 if (Equals(check_type)) {
117 return *this;
118 } else {
119 switch (check_type.GetType()) {
120 case RegType::kRegTypeBoolean:
121 return IsBooleanTypes() ? check_type : reg_types->Conflict();
122 break;
123 case RegType::kRegTypeByte:
124 return IsByteTypes() ? check_type : reg_types->Conflict();
125 break;
126 case RegType::kRegTypeShort:
127 return IsShortTypes() ? check_type : reg_types->Conflict();
128 break;
129 case RegType::kRegTypeChar:
130 return IsCharTypes() ? check_type : reg_types->Conflict();
131 break;
132 case RegType::kRegTypeInteger:
133 return IsIntegralTypes() ? check_type : reg_types->Conflict();
134 break;
135 case RegType::kRegTypeFloat:
136 return IsFloatTypes() ? check_type : reg_types->Conflict();
137 break;
138 case RegType::kRegTypeLongLo:
139 return IsLongTypes() ? check_type : reg_types->Conflict();
140 break;
141 case RegType::kRegTypeDoubleLo:
142 return IsDoubleTypes() ? check_type : reg_types->Conflict();
143 break;
144 case RegType::kRegTypeReference:
145 if (IsZero()) {
146 return check_type;
147 } else if (IsUninitializedReference()) {
148 return reg_types->Conflict(); // Nonsensical to Join two uninitialized classes
149 } else if (IsReference() && check_type.GetClass()->IsAssignableFrom(GetClass())) {
150 return check_type;
151 } else {
152 return reg_types->Conflict();
153 }
154 default:
155 LOG(FATAL) << "Unexpected register type verification against " << check_type;
156 return reg_types->Conflict();
157 }
158 }
159}
160
161#define k_ RegType::kRegTypeUnknown
162#define kX RegType::kRegTypeConflict
163#define k0 RegType::kRegTypeZero
164#define k1 RegType::kRegTypeOne
165#define kZ RegType::kRegTypeBoolean
166#define ky RegType::kRegTypeConstPosByte
167#define kY RegType::kRegTypeConstByte
168#define kh RegType::kRegTypeConstPosShort
169#define kH RegType::kRegTypeConstShort
170#define kc RegType::kRegTypeConstChar
171#define ki RegType::kRegTypeConstInteger
172#define kb RegType::kRegTypePosByte
173#define kB RegType::kRegTypeByte
174#define ks RegType::kRegTypePosShort
175#define kS RegType::kRegTypeShort
176#define kC RegType::kRegTypeChar
177#define kI RegType::kRegTypeInteger
178#define kF RegType::kRegTypeFloat
179#define kN RegType::kRegTypeConstLo
180#define kn RegType::kRegTypeConstHi
181#define kJ RegType::kRegTypeLongLo
182#define kj RegType::kRegTypeLongHi
183#define kD RegType::kRegTypeDoubleLo
184#define kd RegType::kRegTypeDoubleHi
185
186const RegType::Type RegType::merge_table_[kRegTypeReference][kRegTypeReference] =
jeffhaobdb76512011-09-07 11:43:16 -0700187 {
Ian Rogersd81871c2011-10-03 13:57:23 -0700188 /* chk: _ X 0 1 Z y Y h H c i b B s S C I F N n J j D d */
189 { /*_*/ k_,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX,kX },
190 { /*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 },
191 { /*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 },
192 { /*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 },
193 { /*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 },
194 { /*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 },
195 { /*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 },
196 { /*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 },
197 { /*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 },
198 { /*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 },
199 { /*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 },
200 { /*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 },
201 { /*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 },
202 { /*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 },
203 { /*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 },
204 { /*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 },
205 { /*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 },
206 { /*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 },
207 { /*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 },
208 { /*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 },
209 { /*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 },
210 { /*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 },
211 { /*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 },
212 { /*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 -0700213 };
214
215#undef k_
jeffhaobdb76512011-09-07 11:43:16 -0700216#undef kX
217#undef k0
218#undef k1
219#undef kZ
220#undef ky
221#undef kY
222#undef kh
223#undef kH
224#undef kc
225#undef ki
226#undef kb
227#undef kB
228#undef ks
229#undef kS
230#undef kC
231#undef kI
232#undef kF
233#undef kN
234#undef kn
235#undef kJ
236#undef kj
237#undef kD
238#undef kd
239
Ian Rogersd81871c2011-10-03 13:57:23 -0700240const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
241 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
242 if (type_ < kRegTypeReference) {
243 if (incoming_type.type_ < kRegTypeReference) {
244 // Merge of primitive types, unknown or conflict use lookup table
245 RegType::Type table_type = merge_table_[type_][incoming_type.type_];
246 // Check if we got an identity result before hitting the cache
247 if (type_ == table_type) {
248 return *this;
249 } else if (incoming_type.type_ == table_type) {
250 return incoming_type;
251 } else {
252 return reg_types->FromType(table_type);
253 }
254 } else if (IsZero()) { // Merge of primitive zero and reference => reference
255 return incoming_type;
256 } else { // Merge with non-zero with reference => conflict
257 return reg_types->Conflict();
258 }
259 } else if (!incoming_type.IsReference()) {
260 DCHECK(IsReference());
261 if (incoming_type.IsZero()) { // Merge of primitive zero and reference => reference
262 return *this;
263 } else { // Merge with non-zero with reference => conflict
264 return reg_types->Conflict();
265 }
266 } else if (IsUninitializedReference()) {
267 // Can only merge an uninitialized type with itself, but we already checked this
268 return reg_types->Conflict();
269 } else { // Two reference types, compute Join
270 Class* c1 = GetClass();
271 Class* c2 = incoming_type.GetClass();
272 DCHECK(c1 != NULL && !c1->IsPrimitive());
273 DCHECK(c2 != NULL && !c2->IsPrimitive());
274 Class* join_class = ClassJoin(c1, c2);
275 if (c1 == join_class) {
276 return *this;
277 } else if (c2 == join_class) {
278 return incoming_type;
279 } else {
280 return reg_types->FromClass(join_class);
281 }
282 }
283}
284
285static RegType::Type RegTypeFromPrimitiveType(Class::PrimitiveType prim_type) {
286 switch (prim_type) {
287 case Class::kPrimBoolean: return RegType::kRegTypeBoolean;
288 case Class::kPrimByte: return RegType::kRegTypeByte;
289 case Class::kPrimShort: return RegType::kRegTypeShort;
290 case Class::kPrimChar: return RegType::kRegTypeChar;
291 case Class::kPrimInt: return RegType::kRegTypeInteger;
292 case Class::kPrimLong: return RegType::kRegTypeLongLo;
293 case Class::kPrimFloat: return RegType::kRegTypeFloat;
294 case Class::kPrimDouble: return RegType::kRegTypeDoubleLo;
295 case Class::kPrimVoid:
296 default: return RegType::kRegTypeUnknown;
297 }
298}
299
300static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
301 if (descriptor.length() == 1) {
302 switch (descriptor[0]) {
303 case 'Z': return RegType::kRegTypeBoolean;
304 case 'B': return RegType::kRegTypeByte;
305 case 'S': return RegType::kRegTypeShort;
306 case 'C': return RegType::kRegTypeChar;
307 case 'I': return RegType::kRegTypeInteger;
308 case 'J': return RegType::kRegTypeLongLo;
309 case 'F': return RegType::kRegTypeFloat;
310 case 'D': return RegType::kRegTypeDoubleLo;
311 case 'V':
312 default: return RegType::kRegTypeUnknown;
313 }
314 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
315 return RegType::kRegTypeReference;
316 } else {
317 return RegType::kRegTypeUnknown;
318 }
319}
320
321std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
322 rhs.Dump(os);
323 return os;
324}
325
326const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
327 const std::string& descriptor) {
328 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
329}
330
331const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
332 const std::string& descriptor) {
333 if (type < RegType::kRegTypeReference) {
334 // entries should be sized greater than primitive types
335 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
336 RegType* entry = entries_[type];
337 if (entry == NULL) {
338 Class *klass = NULL;
339 if (descriptor.size() != 0) {
340 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
341 }
342 entry = new RegType(type, klass, RegType::kInitArgAddr, type);
343 entries_[type] = entry;
344 }
345 return *entry;
346 } else {
347 DCHECK (type == RegType::kRegTypeReference);
348 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
349 RegType* cur_entry = entries_[i];
350 if (cur_entry->IsInitialized() &&
351 cur_entry->GetClass()->GetDescriptor()->Equals(descriptor)) {
352 return *cur_entry;
353 }
354 }
355 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
356 RegType* entry = new RegType(type, klass, RegType::kInitArgAddr, entries_.size());
357 entries_.push_back(entry);
358 return *entry;
359 }
360}
361
362const RegType& RegTypeCache::FromClass(Class* klass) {
363 if (klass->IsPrimitive()) {
364 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
365 // entries should be sized greater than primitive types
366 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
367 RegType* entry = entries_[type];
368 if (entry == NULL) {
369 entry = new RegType(type, klass, RegType::kInitArgAddr, type);
370 entries_[type] = entry;
371 }
372 return *entry;
373 } else {
374 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
375 RegType* cur_entry = entries_[i];
376 if (cur_entry->IsInitialized() && cur_entry->GetClass() == klass) {
377 return *cur_entry;
378 }
379 }
380 RegType* entry = new RegType(RegType::kRegTypeReference, klass, RegType::kInitArgAddr,
381 entries_.size());
382 entries_.push_back(entry);
383 return *entry;
384 }
385}
386
387const RegType& RegTypeCache::Uninitialized(Class* klass, uint32_t allocation_pc) {
388 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
389 RegType* cur_entry = entries_[i];
390 if (cur_entry->allocation_pc_ == allocation_pc && cur_entry->GetClass() == klass) {
391 return *cur_entry;
392 }
393 }
394 RegType* entry = new RegType(RegType::kRegTypeReference, klass, allocation_pc, entries_.size());
395 entries_.push_back(entry);
396 return *entry;
397}
398
399const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
400 for (size_t i = RegType::kRegTypeReference; i < entries_.size(); i++) {
401 RegType* cur_entry = entries_[i];
402 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
403 return *cur_entry;
404 }
405 }
406 RegType* entry = new RegType(RegType::kRegTypeReference, klass, RegType::kUninitThisArgAddr,
407 entries_.size());
408 entries_.push_back(entry);
409 return *entry;
410}
411
412const RegType& RegTypeCache::FromType(RegType::Type type) {
413 CHECK(type < RegType::kRegTypeReference);
414 switch (type) {
415 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
416 case RegType::kRegTypeByte: return From(type, NULL, "B");
417 case RegType::kRegTypeShort: return From(type, NULL, "S");
418 case RegType::kRegTypeChar: return From(type, NULL, "C");
419 case RegType::kRegTypeInteger: return From(type, NULL, "I");
420 case RegType::kRegTypeFloat: return From(type, NULL, "F");
421 case RegType::kRegTypeLongLo:
422 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
423 case RegType::kRegTypeDoubleLo:
424 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
425 default: return From(type, NULL, "");
426 }
427}
428
429const RegType& RegTypeCache::FromCat1Const(int32_t value) {
430 if (value < -32768) {
431 return FromType(RegType::kRegTypeConstInteger);
432 } else if (value < -128) {
433 return FromType(RegType::kRegTypeConstShort);
434 } else if (value < 0) {
435 return FromType(RegType::kRegTypeConstByte);
436 } else if (value == 0) {
437 return FromType(RegType::kRegTypeZero);
438 } else if (value == 1) {
439 return FromType(RegType::kRegTypeOne);
440 } else if (value < 128) {
441 return FromType(RegType::kRegTypeConstPosByte);
442 } else if (value < 32768) {
443 return FromType(RegType::kRegTypeConstPosShort);
444 } else if (value < 65536) {
445 return FromType(RegType::kRegTypeConstChar);
446 } else {
447 return FromType(RegType::kRegTypeConstInteger);
448 }
449}
450
451bool RegisterLine::CheckConstructorReturn() const {
452 for (size_t i = 0; i < num_regs_; i++) {
453 if (GetRegisterType(i).IsUninitializedThisReference()) {
454 verifier_->Fail(VERIFY_ERROR_GENERIC)
455 << "Constructor returning without calling superclass constructor";
456 return false;
457 }
458 }
459 return true;
460}
461
462void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
463 DCHECK(vdst < num_regs_);
464 if (new_type.IsLowHalf()) {
465 line_[vdst] = new_type.GetId();
466 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
467 } else if (new_type.IsHighHalf()) {
468 /* should never set these explicitly */
469 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
470 } else if (new_type.IsConflict()) { // should only be set during a merge
471 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
472 } else {
473 line_[vdst] = new_type.GetId();
474 }
475 // Clear the monitor entry bits for this register.
476 ClearAllRegToLockDepths(vdst);
477}
478
479void RegisterLine::SetResultTypeToUnknown() {
480 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
481 result_[0] = unknown_id;
482 result_[1] = unknown_id;
483}
484
485void RegisterLine::SetResultRegisterType(const RegType& new_type) {
486 result_[0] = new_type.GetId();
487 if(new_type.IsLowHalf()) {
488 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
489 result_[1] = new_type.GetId() + 1;
490 } else {
491 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
492 }
493}
494
495const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
496 // The register index was validated during the static pass, so we don't need to check it here.
497 DCHECK_LT(vsrc, num_regs_);
498 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
499}
500
501const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
502 if (dec_insn.vA_ < 1) {
503 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
504 return verifier_->GetRegTypeCache()->Unknown();
505 }
506 /* get the element type of the array held in vsrc */
507 const RegType& this_type = GetRegisterType(dec_insn.vC_);
508 if (!this_type.IsReferenceTypes()) {
509 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
510 << dec_insn.vC_ << " (type=" << this_type << ")";
511 return verifier_->GetRegTypeCache()->Unknown();
512 }
513 return this_type;
514}
515
516Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
517 /* get the element type of the array held in vsrc */
518 const RegType& type = GetRegisterType(vsrc);
519 /* if "always zero", we allow it to fail at runtime */
520 if (type.IsZero()) {
521 return NULL;
522 } else if (!type.IsReferenceTypes()) {
523 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
524 << " (type=" << type << ")";
525 return NULL;
526 } else if (type.IsUninitializedReference()) {
527 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
528 return NULL;
529 } else {
530 return type.GetClass();
531 }
532}
533
534bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
535 // Verify the src register type against the check type refining the type of the register
536 const RegType& src_type = GetRegisterType(vsrc);
537 const RegType& lub_type = src_type.VerifyAgainst(check_type, verifier_->GetRegTypeCache());
538 if (lub_type.IsConflict()) {
539 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
540 << " but expected " << check_type;
541 return false;
542 }
543 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
544 // precise than the subtype in vsrc so leave it for reference types. For primitive types
545 // if they are a defined type then they are as precise as we can get, however, for constant
546 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
547 return true;
548}
549
550void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
551 Class* klass = uninit_type.GetClass();
552 if (klass == NULL) {
553 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Unable to find type=" << uninit_type;
554 } else {
555 const RegType& init_type = verifier_->GetRegTypeCache()->FromClass(klass);
556 size_t changed = 0;
557 for (size_t i = 0; i < num_regs_; i++) {
558 if (GetRegisterType(i).Equals(uninit_type)) {
559 line_[i] = init_type.GetId();
560 changed++;
561 }
562 }
563 DCHECK_GT(changed, 0u);
564 }
565}
566
567void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
568 for (size_t i = 0; i < num_regs_; i++) {
569 if (GetRegisterType(i).Equals(uninit_type)) {
570 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
571 ClearAllRegToLockDepths(i);
572 }
573 }
574}
575
576void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
577 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
578 const RegType& type = GetRegisterType(vsrc);
579 SetRegisterType(vdst, type);
580 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
581 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
582 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
583 << " cat=" << static_cast<int>(cat);
584 } else if (cat == kTypeCategoryRef) {
585 CopyRegToLockDepth(vdst, vsrc);
586 }
587}
588
589void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
590 const RegType& type_l = GetRegisterType(vsrc);
591 const RegType& type_h = GetRegisterType(vsrc + 1);
592
593 if (!type_l.CheckWidePair(type_h)) {
594 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
595 << " type=" << type_l << "/" << type_h;
596 } else {
597 SetRegisterType(vdst, type_l); // implicitly sets the second half
598 }
599}
600
601void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
602 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
603 if ((!is_reference && !type.IsCategory1Types()) ||
604 (is_reference && !type.IsReferenceTypes())) {
605 verifier_->Fail(VERIFY_ERROR_GENERIC)
606 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
607 } else {
608 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
609 SetRegisterType(vdst, type);
610 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
611 }
612}
613
614/*
615 * Implement "move-result-wide". Copy the category-2 value from the result
616 * register to another register, and reset the result register.
617 */
618void RegisterLine::CopyResultRegister2(uint32_t vdst) {
619 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
620 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
621 if (!type_l.IsCategory2Types()) {
622 verifier_->Fail(VERIFY_ERROR_GENERIC)
623 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
624 } else {
625 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
626 SetRegisterType(vdst, type_l); // also sets the high
627 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
628 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
629 }
630}
631
632void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
633 const RegType& dst_type, const RegType& src_type) {
634 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
635 SetRegisterType(dec_insn.vA_, dst_type);
636 }
637}
638
639void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
640 const RegType& dst_type,
641 const RegType& src_type1, const RegType& src_type2,
642 bool check_boolean_op) {
643 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
644 VerifyRegisterType(dec_insn.vC_, src_type2)) {
645 if (check_boolean_op) {
646 DCHECK(dst_type.IsInteger());
647 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
648 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
649 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
650 return;
651 }
652 }
653 SetRegisterType(dec_insn.vA_, dst_type);
654 }
655}
656
657void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
658 const RegType& dst_type, const RegType& src_type1,
659 const RegType& src_type2, bool check_boolean_op) {
660 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
661 VerifyRegisterType(dec_insn.vB_, src_type2)) {
662 if (check_boolean_op) {
663 DCHECK(dst_type.IsInteger());
664 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
665 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
666 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
667 return;
668 }
669 }
670 SetRegisterType(dec_insn.vA_, dst_type);
671 }
672}
673
674void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
675 const RegType& dst_type, const RegType& src_type,
676 bool check_boolean_op) {
677 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
678 if (check_boolean_op) {
679 DCHECK(dst_type.IsInteger());
680 /* check vB with the call, then check the constant manually */
681 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
682 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
683 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
684 return;
685 }
686 }
687 SetRegisterType(dec_insn.vA_, dst_type);
688 }
689}
690
691void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
692 const RegType& reg_type = GetRegisterType(reg_idx);
693 if (!reg_type.IsReferenceTypes()) {
694 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
695 } else {
696 SetRegToLockDepth(reg_idx, monitors_.size());
697 monitors_.push(insn_idx);
698 }
699}
700
701void RegisterLine::PopMonitor(uint32_t reg_idx) {
702 const RegType& reg_type = GetRegisterType(reg_idx);
703 if (!reg_type.IsReferenceTypes()) {
704 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
705 } else if (monitors_.empty()) {
706 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
707 } else {
708 monitors_.pop();
709 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
710 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
711 // format "036" the constant collector may create unlocks on the same object but referenced
712 // via different registers.
713 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
714 : verifier_->LogVerifyInfo())
715 << "monitor-exit not unlocking the top of the monitor stack";
716 } else {
717 // Record the register was unlocked
718 ClearRegToLockDepth(reg_idx, monitors_.size());
719 }
720 }
721}
722
723bool RegisterLine::VerifyMonitorStackEmpty() {
724 if (MonitorStackDepth() != 0) {
725 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
726 return false;
727 } else {
728 return true;
729 }
730}
731
732bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
733 bool changed = false;
734 for (size_t idx = 0; idx < num_regs_; idx++) {
735 if (line_[idx] != incoming_line->line_[idx]) {
736 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
737 const RegType& cur_type = GetRegisterType(idx);
738 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
739 changed = changed || !cur_type.Equals(new_type);
740 line_[idx] = new_type.GetId();
741 }
742 }
743 if(monitors_ != incoming_line->monitors_) {
744 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
745 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
746 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
747 for (uint32_t idx = 0; idx < num_regs_; idx++) {
748 size_t depths = reg_to_lock_depths_.count(idx);
749 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
750 if (depths != incoming_depths) {
751 if (depths == 0 || incoming_depths == 0) {
752 reg_to_lock_depths_.erase(idx);
753 } else {
754 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
755 << ": " << depths << " != " << incoming_depths;
756 break;
757 }
758 }
759 }
760 }
761 return changed;
762}
763
764void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
765 for (size_t i = 0; i < num_regs_; i += 8) {
766 uint8_t val = 0;
767 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
768 // Note: we write 1 for a Reference but not for Null
769 if (GetRegisterType(i + j).IsReference()) {
770 val |= 1 << j;
771 }
772 }
773 if (val != 0) {
774 DCHECK_LT(i / 8, max_bytes);
775 data[i / 8] = val;
776 }
777 }
778}
779
780std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
781 rhs.Dump(os);
782 return os;
783}
784
785
786void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
787 uint32_t insns_size, uint16_t registers_size,
788 DexVerifier* verifier) {
789 DCHECK_GT(insns_size, 0U);
790
791 for (uint32_t i = 0; i < insns_size; i++) {
792 bool interesting = false;
793 switch (mode) {
794 case kTrackRegsAll:
795 interesting = flags[i].IsOpcode();
796 break;
797 case kTrackRegsGcPoints:
798 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
799 break;
800 case kTrackRegsBranches:
801 interesting = flags[i].IsBranchTarget();
802 break;
803 default:
804 break;
805 }
806 if (interesting) {
807 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
808 }
809 }
810}
811
812bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700813 if (klass->IsVerified()) {
814 return true;
815 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 Class* super = klass->GetSuperClass();
817 if (super == NULL && !klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
818 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
819 return false;
820 }
821 if (super != NULL) {
822 if (!super->IsVerified() && !super->IsErroneous()) {
823 Runtime::Current()->GetClassLinker()->VerifyClass(super);
824 }
825 if (!super->IsVerified()) {
826 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
827 << " that attempts to sub-class corrupt class " << PrettyClass(super);
828 return false;
829 } else if (super->IsFinal()) {
830 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
831 << " that attempts to sub-class final class " << PrettyClass(super);
832 return false;
833 }
834 }
jeffhaobdb76512011-09-07 11:43:16 -0700835 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
836 Method* method = klass->GetDirectMethod(i);
837 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
839 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700840 return false;
841 }
842 }
843 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
844 Method* method = klass->GetVirtualMethod(i);
845 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
847 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700848 return false;
849 }
850 }
851 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852}
853
jeffhaobdb76512011-09-07 11:43:16 -0700854bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 DexVerifier verifier(method);
856 bool success = verifier.Verify();
857 // We expect either success and no verification error, or failure and a generic failure to
858 // reject the class.
859 if (success) {
860 if (verifier.failure_ != VERIFY_ERROR_NONE) {
861 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
862 << verifier.fail_messages_;
863 }
864 } else {
865 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
866 << verifier.fail_messages_.str() << std::endl << verifier.info_messages_.str();
867 verifier.Dump(std::cout);
868 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
869 }
870 return success;
871}
872
873DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
874 method_(method), failure_(VERIFY_ERROR_NONE),
875 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700876 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
877 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 dex_file_ = &class_linker->FindDexFile(dex_cache);
879 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700880}
881
Ian Rogersd81871c2011-10-03 13:57:23 -0700882bool DexVerifier::Verify() {
883 // If there aren't any instructions, make sure that's expected, then exit successfully.
884 if (code_item_ == NULL) {
885 if (!method_->IsNative() && !method_->IsAbstract()) {
886 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700887 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 } else {
889 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700890 }
jeffhaobdb76512011-09-07 11:43:16 -0700891 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
893 if (code_item_->ins_size_ > code_item_->registers_size_) {
894 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
895 << " regs=" << code_item_->registers_size_;
896 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700897 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 // Allocate and initialize an array to hold instruction data.
899 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
900 // Run through the instructions and see if the width checks out.
901 bool result = ComputeWidthsAndCountOps();
902 // Flag instructions guarded by a "try" block and check exception handlers.
903 result = result && ScanTryCatchBlocks();
904 // Perform static instruction verification.
905 result = result && VerifyInstructions();
906 // Perform code flow analysis.
907 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700908 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700909}
910
Ian Rogersd81871c2011-10-03 13:57:23 -0700911bool DexVerifier::ComputeWidthsAndCountOps() {
912 const uint16_t* insns = code_item_->insns_;
913 size_t insns_size = code_item_->insns_size_in_code_units_;
914 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700915 size_t new_instance_count = 0;
916 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700918
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700920 Instruction::Code opcode = inst->Opcode();
921 if (opcode == Instruction::NEW_INSTANCE) {
922 new_instance_count++;
923 } else if (opcode == Instruction::MONITOR_ENTER) {
924 monitor_enter_count++;
925 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 size_t inst_size = inst->SizeInCodeUnits();
927 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
928 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700929 inst = inst->Next();
930 }
931
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 if (dex_pc != insns_size) {
933 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
934 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700935 return false;
936 }
937
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 new_instance_count_ = new_instance_count;
939 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700940 return true;
941}
942
Ian Rogersd81871c2011-10-03 13:57:23 -0700943bool DexVerifier::ScanTryCatchBlocks() {
944 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700945 if (tries_size == 0) {
946 return true;
947 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 uint32_t insns_size = code_item_->insns_size_in_code_units_;
949 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700950
951 for (uint32_t idx = 0; idx < tries_size; idx++) {
952 const DexFile::TryItem* try_item = &tries[idx];
953 uint32_t start = try_item->start_addr_;
954 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700955 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
957 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700958 return false;
959 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 if (!insn_flags_[start].IsOpcode()) {
961 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700962 return false;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 for (uint32_t dex_pc = start; dex_pc < end;
965 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
966 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700967 }
968 }
jeffhaobdb76512011-09-07 11:43:16 -0700969 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700971 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
972 for (uint32_t idx = 0; idx < handlers_size; idx++) {
973 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700974 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 uint32_t dex_pc= iterator.Get().address_;
976 if (!insn_flags_[dex_pc].IsOpcode()) {
977 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700978 return false;
979 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -0700981 }
jeffhaobdb76512011-09-07 11:43:16 -0700982 handlers_ptr = iterator.GetData();
983 }
jeffhaobdb76512011-09-07 11:43:16 -0700984 return true;
985}
986
Ian Rogersd81871c2011-10-03 13:57:23 -0700987bool DexVerifier::VerifyInstructions() {
988 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700989
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 /* Flag the start of the method as a branch target. */
991 insn_flags_[0].SetBranchTarget();
992
993 uint32_t insns_size = code_item_->insns_size_in_code_units_;
994 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
995 if (!VerifyInstruction(inst, dex_pc)) {
996 Fail(VERIFY_ERROR_GENERIC) << "rejecting opcode " << inst->Name() << " at " << dex_pc;
997 return false;
998 }
999 /* Flag instructions that are garbage collection points */
1000 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1001 insn_flags_[dex_pc].SetGcPoint();
1002 }
1003 dex_pc += inst->SizeInCodeUnits();
1004 inst = inst->Next();
1005 }
1006 return true;
1007}
1008
1009bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1010 Instruction::DecodedInstruction dec_insn(inst);
1011 bool result = true;
1012 switch (inst->GetVerifyTypeArgumentA()) {
1013 case Instruction::kVerifyRegA:
1014 result = result && CheckRegisterIndex(dec_insn.vA_);
1015 break;
1016 case Instruction::kVerifyRegAWide:
1017 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1018 break;
1019 }
1020 switch (inst->GetVerifyTypeArgumentB()) {
1021 case Instruction::kVerifyRegB:
1022 result = result && CheckRegisterIndex(dec_insn.vB_);
1023 break;
1024 case Instruction::kVerifyRegBField:
1025 result = result && CheckFieldIndex(dec_insn.vB_);
1026 break;
1027 case Instruction::kVerifyRegBMethod:
1028 result = result && CheckMethodIndex(dec_insn.vB_);
1029 break;
1030 case Instruction::kVerifyRegBNewInstance:
1031 result = result && CheckNewInstance(dec_insn.vB_);
1032 break;
1033 case Instruction::kVerifyRegBString:
1034 result = result && CheckStringIndex(dec_insn.vB_);
1035 break;
1036 case Instruction::kVerifyRegBType:
1037 result = result && CheckTypeIndex(dec_insn.vB_);
1038 break;
1039 case Instruction::kVerifyRegBWide:
1040 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1041 break;
1042 }
1043 switch (inst->GetVerifyTypeArgumentC()) {
1044 case Instruction::kVerifyRegC:
1045 result = result && CheckRegisterIndex(dec_insn.vC_);
1046 break;
1047 case Instruction::kVerifyRegCField:
1048 result = result && CheckFieldIndex(dec_insn.vC_);
1049 break;
1050 case Instruction::kVerifyRegCNewArray:
1051 result = result && CheckNewArray(dec_insn.vC_);
1052 break;
1053 case Instruction::kVerifyRegCType:
1054 result = result && CheckTypeIndex(dec_insn.vC_);
1055 break;
1056 case Instruction::kVerifyRegCWide:
1057 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1058 break;
1059 }
1060 switch (inst->GetVerifyExtraFlags()) {
1061 case Instruction::kVerifyArrayData:
1062 result = result && CheckArrayData(code_offset);
1063 break;
1064 case Instruction::kVerifyBranchTarget:
1065 result = result && CheckBranchTarget(code_offset);
1066 break;
1067 case Instruction::kVerifySwitchTargets:
1068 result = result && CheckSwitchTargets(code_offset);
1069 break;
1070 case Instruction::kVerifyVarArg:
1071 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1072 break;
1073 case Instruction::kVerifyVarArgRange:
1074 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1075 break;
1076 case Instruction::kVerifyError:
1077 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1078 result = false;
1079 break;
1080 }
1081 return result;
1082}
1083
1084bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1085 if (idx >= code_item_->registers_size_) {
1086 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1087 << code_item_->registers_size_ << ")";
1088 return false;
1089 }
1090 return true;
1091}
1092
1093bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1094 if (idx + 1 >= code_item_->registers_size_) {
1095 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1096 << "+1 >= " << code_item_->registers_size_ << ")";
1097 return false;
1098 }
1099 return true;
1100}
1101
1102bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1103 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1104 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1105 << dex_file_->GetHeader().field_ids_size_ << ")";
1106 return false;
1107 }
1108 return true;
1109}
1110
1111bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1112 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1113 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1114 << dex_file_->GetHeader().method_ids_size_ << ")";
1115 return false;
1116 }
1117 return true;
1118}
1119
1120bool DexVerifier::CheckNewInstance(uint32_t idx) {
1121 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1122 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1123 << dex_file_->GetHeader().type_ids_size_ << ")";
1124 return false;
1125 }
1126 // We don't need the actual class, just a pointer to the class name.
1127 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1128 if (descriptor[0] != 'L') {
1129 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1130 return false;
1131 }
1132 return true;
1133}
1134
1135bool DexVerifier::CheckStringIndex(uint32_t idx) {
1136 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1137 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1138 << dex_file_->GetHeader().string_ids_size_ << ")";
1139 return false;
1140 }
1141 return true;
1142}
1143
1144bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1145 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1146 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1147 << dex_file_->GetHeader().type_ids_size_ << ")";
1148 return false;
1149 }
1150 return true;
1151}
1152
1153bool DexVerifier::CheckNewArray(uint32_t idx) {
1154 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1155 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1156 << dex_file_->GetHeader().type_ids_size_ << ")";
1157 return false;
1158 }
1159 int bracket_count = 0;
1160 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1161 const char* cp = descriptor;
1162 while (*cp++ == '[') {
1163 bracket_count++;
1164 }
1165 if (bracket_count == 0) {
1166 /* The given class must be an array type. */
1167 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1168 return false;
1169 } else if (bracket_count > 255) {
1170 /* It is illegal to create an array of more than 255 dimensions. */
1171 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1172 return false;
1173 }
1174 return true;
1175}
1176
1177bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1178 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1179 const uint16_t* insns = code_item_->insns_ + cur_offset;
1180 const uint16_t* array_data;
1181 int32_t array_data_offset;
1182
1183 DCHECK_LT(cur_offset, insn_count);
1184 /* make sure the start of the array data table is in range */
1185 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1186 if ((int32_t) cur_offset + array_data_offset < 0 ||
1187 cur_offset + array_data_offset + 2 >= insn_count) {
1188 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1189 << ", data offset " << array_data_offset << ", count " << insn_count;
1190 return false;
1191 }
1192 /* offset to array data table is a relative branch-style offset */
1193 array_data = insns + array_data_offset;
1194 /* make sure the table is 32-bit aligned */
1195 if ((((uint32_t) array_data) & 0x03) != 0) {
1196 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1197 << ", data offset " << array_data_offset;
1198 return false;
1199 }
1200 uint32_t value_width = array_data[1];
1201 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1202 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1203 /* make sure the end of the switch is in range */
1204 if (cur_offset + array_data_offset + table_size > insn_count) {
1205 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1206 << ", data offset " << array_data_offset << ", end "
1207 << cur_offset + array_data_offset + table_size
1208 << ", count " << insn_count;
1209 return false;
1210 }
1211 return true;
1212}
1213
1214bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1215 int32_t offset;
1216 bool isConditional, selfOkay;
1217 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1218 return false;
1219 }
1220 if (!selfOkay && offset == 0) {
1221 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1222 return false;
1223 }
1224 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1225 // identical "wrap-around" behavior, but it's unwise to depend on that.
1226 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1227 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1228 return false;
1229 }
1230 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1231 int32_t abs_offset = cur_offset + offset;
1232 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1233 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1234 << (void*) abs_offset << ") at " << (void*) cur_offset;
1235 return false;
1236 }
1237 insn_flags_[abs_offset].SetBranchTarget();
1238 return true;
1239}
1240
1241bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1242 bool* selfOkay) {
1243 const uint16_t* insns = code_item_->insns_ + cur_offset;
1244 *pConditional = false;
1245 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 switch (*insns & 0xff) {
1247 case Instruction::GOTO:
1248 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001249 break;
1250 case Instruction::GOTO_32:
1251 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001252 *selfOkay = true;
1253 break;
1254 case Instruction::GOTO_16:
1255 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001256 break;
1257 case Instruction::IF_EQ:
1258 case Instruction::IF_NE:
1259 case Instruction::IF_LT:
1260 case Instruction::IF_GE:
1261 case Instruction::IF_GT:
1262 case Instruction::IF_LE:
1263 case Instruction::IF_EQZ:
1264 case Instruction::IF_NEZ:
1265 case Instruction::IF_LTZ:
1266 case Instruction::IF_GEZ:
1267 case Instruction::IF_GTZ:
1268 case Instruction::IF_LEZ:
1269 *pOffset = (int16_t) insns[1];
1270 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001271 break;
1272 default:
1273 return false;
1274 break;
1275 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001276 return true;
1277}
1278
Ian Rogersd81871c2011-10-03 13:57:23 -07001279bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1280 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001281 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001283 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1285 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1286 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1287 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001288 return false;
1289 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001290 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 /* make sure the table is 32-bit aligned */
1293 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1295 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001296 return false;
1297 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 uint32_t switch_count = switch_insns[1];
1299 int32_t keys_offset, targets_offset;
1300 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001301 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1302 /* 0=sig, 1=count, 2/3=firstKey */
1303 targets_offset = 4;
1304 keys_offset = -1;
1305 expected_signature = Instruction::kPackedSwitchSignature;
1306 } else {
1307 /* 0=sig, 1=count, 2..count*2 = keys */
1308 keys_offset = 2;
1309 targets_offset = 2 + 2 * switch_count;
1310 expected_signature = Instruction::kSparseSwitchSignature;
1311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001313 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1315 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 /* make sure the end of the switch is in range */
1319 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1321 << switch_offset << ", end "
1322 << (cur_offset + switch_offset + table_size)
1323 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001324 return false;
1325 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001326 /* for a sparse switch, verify the keys are in ascending order */
1327 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1329 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1331 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1332 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1334 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001335 return false;
1336 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 last_key = key;
1338 }
1339 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 for (uint32_t targ = 0; targ < switch_count; targ++) {
1342 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1343 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1344 int32_t abs_offset = cur_offset + offset;
1345 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1346 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1347 << (void*) abs_offset << ") at "
1348 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001349 return false;
1350 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 insn_flags_[abs_offset].SetBranchTarget();
1352 }
1353 return true;
1354}
1355
1356bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1357 if (vA > 5) {
1358 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1359 return false;
1360 }
1361 uint16_t registers_size = code_item_->registers_size_;
1362 for (uint32_t idx = 0; idx < vA; idx++) {
1363 if (arg[idx] > registers_size) {
1364 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1365 << ") in non-range invoke (> " << registers_size << ")";
1366 return false;
1367 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001368 }
1369
1370 return true;
1371}
1372
Ian Rogersd81871c2011-10-03 13:57:23 -07001373bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1374 uint16_t registers_size = code_item_->registers_size_;
1375 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1376 // integer overflow when adding them here.
1377 if (vA + vC > registers_size) {
1378 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1379 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 return false;
1381 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001382 return true;
1383}
1384
Ian Rogersd81871c2011-10-03 13:57:23 -07001385bool DexVerifier::VerifyCodeFlow() {
1386 uint16_t registers_size = code_item_->registers_size_;
1387 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001388
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 if (registers_size * insns_size > 4*1024*1024) {
1390 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1391 << " insns_size=" << insns_size << ")";
1392 }
1393 /* Create and initialize table holding register status */
1394 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1395 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001396
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 work_line_.reset(new RegisterLine(registers_size, this));
1398 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001399
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 /* Initialize register types of method arguments. */
1401 if (!SetTypesFromSignature()) {
1402 Fail(VERIFY_ERROR_GENERIC) << "bad signature in " << PrettyMethod(method_);
1403 return false;
1404 }
1405 /* Perform code flow verification. */
1406 if (!CodeFlowVerifyMethod()) {
1407 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001408 }
1409
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 /* Generate a register map and add it to the method. */
1411 ByteArray* map = GenerateGcMap();
1412 if (map == NULL) {
1413 return false; // Not a real failure, but a failure to encode
1414 }
1415 method_->SetGcMap(map);
1416#ifndef NDEBUG
1417 VerifyGcMap();
1418#endif
jeffhaobdb76512011-09-07 11:43:16 -07001419 return true;
1420}
1421
Ian Rogersd81871c2011-10-03 13:57:23 -07001422void DexVerifier::Dump(std::ostream& os) {
1423 if (method_->IsNative()) {
1424 os << "Native method" << std::endl;
1425 return;
jeffhaobdb76512011-09-07 11:43:16 -07001426 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 DCHECK(code_item_ != NULL);
1428 const Instruction* inst = Instruction::At(code_item_->insns_);
1429 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1430 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1431 std::ios::fmtflags saved_flags(os.flags());
1432 os << std::hex << std::showbase << std::setfill('0') << std::setw(4) << dex_pc << ": ";
1433 os.flags(saved_flags);
1434 insn_flags_[dex_pc].Dump(os);
1435 os << " ";
1436 inst->DumpHex(os, 5);
1437 os << " ";
1438 inst->Dump(os, dex_file_);
1439 os << std::endl;
1440 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1441 if (reg_line != NULL) {
1442 reg_line->Dump(os);
1443 os << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001444 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 inst = inst->Next();
1446 }
jeffhaobdb76512011-09-07 11:43:16 -07001447}
1448
Ian Rogersd81871c2011-10-03 13:57:23 -07001449static bool IsPrimitiveDescriptor(char descriptor) {
1450 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001451 case 'I':
1452 case 'C':
1453 case 'S':
1454 case 'B':
1455 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001456 case 'F':
1457 case 'D':
1458 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001460 default:
1461 return false;
1462 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001463}
1464
Ian Rogersd81871c2011-10-03 13:57:23 -07001465bool DexVerifier::SetTypesFromSignature() {
1466 RegisterLine* reg_line = reg_table_.GetLine(0);
1467 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1468 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001469
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1471 //Include the "this" pointer.
1472 size_t cur_arg = 0;
1473 if (!method_->IsStatic()) {
1474 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1475 // argument as uninitialized. This restricts field access until the superclass constructor is
1476 // called.
1477 Class* declaring_class = method_->GetDeclaringClass();
1478 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1479 reg_line->SetRegisterType(arg_start + cur_arg,
1480 reg_types_.UninitializedThisArgument(declaring_class));
1481 } else {
1482 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001485 }
1486
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1488 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1489
1490 for (; iterator.HasNext(); iterator.Next()) {
1491 const char* descriptor = iterator.GetDescriptor();
1492 if (descriptor == NULL) {
1493 LOG(FATAL) << "Null descriptor";
1494 }
1495 if (cur_arg >= expected_args) {
1496 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1497 << " args, found more (" << descriptor << ")";
1498 return false;
1499 }
1500 switch (descriptor[0]) {
1501 case 'L':
1502 case '[':
1503 // We assume that reference arguments are initialized. The only way it could be otherwise
1504 // (assuming the caller was verified) is if the current method is <init>, but in that case
1505 // it's effectively considered initialized the instant we reach here (in the sense that we
1506 // can return without doing anything or call virtual methods).
1507 {
1508 const RegType& reg_type =
1509 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
1510 if (reg_type.IsUnknown()) {
1511 DCHECK(Thread::Current()->IsExceptionPending());
1512 Thread::Current()->ClearException();
1513 Fail(VERIFY_ERROR_GENERIC)
1514 << "Unable to resolve descriptor in signature " << descriptor;
1515 return false;
1516 }
1517 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
1518 }
1519 break;
1520 case 'Z':
1521 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1522 break;
1523 case 'C':
1524 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1525 break;
1526 case 'B':
1527 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1528 break;
1529 case 'I':
1530 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1531 break;
1532 case 'S':
1533 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1534 break;
1535 case 'F':
1536 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1537 break;
1538 case 'J':
1539 case 'D': {
1540 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1541 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1542 cur_arg++;
1543 break;
1544 }
1545 default:
1546 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1547 return false;
1548 }
1549 cur_arg++;
1550 }
1551 if (cur_arg != expected_args) {
1552 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1553 return false;
1554 }
1555 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1556 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1557 // format. Only major difference from the method argument format is that 'V' is supported.
1558 bool result;
1559 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1560 result = descriptor[1] == '\0';
1561 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1562 size_t i = 0;
1563 do {
1564 i++;
1565 } while (descriptor[i] == '['); // process leading [
1566 if (descriptor[i] == 'L') { // object array
1567 do {
1568 i++; // find closing ;
1569 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1570 result = descriptor[i] == ';';
1571 } else { // primitive array
1572 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1573 }
1574 } else if (descriptor[0] == 'L') {
1575 // could be more thorough here, but shouldn't be required
1576 size_t i = 0;
1577 do {
1578 i++;
1579 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1580 result = descriptor[i] == ';';
1581 } else {
1582 result = false;
1583 }
1584 if (!result) {
1585 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1586 << descriptor << "'";
1587 }
1588 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001589}
1590
Ian Rogersd81871c2011-10-03 13:57:23 -07001591bool DexVerifier::CodeFlowVerifyMethod() {
1592 const uint16_t* insns = code_item_->insns_;
1593 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001594
jeffhaobdb76512011-09-07 11:43:16 -07001595 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 insn_flags_[0].SetChanged();
1597 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001598
jeffhaobdb76512011-09-07 11:43:16 -07001599 /* Continue until no instructions are marked "changed". */
1600 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1602 uint32_t insn_idx = start_guess;
1603 for (; insn_idx < insns_size; insn_idx++) {
1604 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001605 break;
1606 }
jeffhaobdb76512011-09-07 11:43:16 -07001607 if (insn_idx == insns_size) {
1608 if (start_guess != 0) {
1609 /* try again, starting from the top */
1610 start_guess = 0;
1611 continue;
1612 } else {
1613 /* all flags are clear */
1614 break;
1615 }
1616 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 // We carry the working set of registers from instruction to instruction. If this address can
1618 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1619 // "changed" flags, we need to load the set of registers from the table.
1620 // Because we always prefer to continue on to the next instruction, we should never have a
1621 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1622 // target.
1623 work_insn_idx_ = insn_idx;
1624 if (insn_flags_[insn_idx].IsBranchTarget()) {
1625 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001626 } else {
1627#ifndef NDEBUG
1628 /*
1629 * Sanity check: retrieve the stored register line (assuming
1630 * a full table) and make sure it actually matches.
1631 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1633 if (register_line != NULL) {
1634 if (work_line_->CompareLine(register_line) != 0) {
1635 Dump(std::cout);
1636 std::cout << info_messages_.str();
1637 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1638 << "@" << (void*)work_insn_idx_ << std::endl
1639 << " work_line=" << *work_line_ << std::endl
1640 << " expected=" << *register_line;
1641 }
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
1643#endif
1644 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 if (!CodeFlowVerifyInstruction(&start_guess)) {
1646 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001647 return false;
1648 }
jeffhaobdb76512011-09-07 11:43:16 -07001649 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 insn_flags_[insn_idx].SetVisited();
1651 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001652 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001653
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001655 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001656 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001657 * (besides the wasted space), but it indicates a flaw somewhere
1658 * down the line, possibly in the verifier.
1659 *
1660 * If we've substituted "always throw" instructions into the stream,
1661 * we are almost certainly going to have some dead code.
1662 */
1663 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 uint32_t insn_idx = 0;
1665 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001666 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001668 * may or may not be preceded by a padding NOP (for alignment).
1669 */
1670 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1671 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1672 insns[insn_idx] == Instruction::kArrayDataSignature ||
1673 (insns[insn_idx] == Instruction::NOP &&
1674 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1675 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1676 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001678 }
1679
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001681 if (dead_start < 0)
1682 dead_start = insn_idx;
1683 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001685 dead_start = -1;
1686 }
1687 }
1688 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001690 }
1691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 return true;
1693}
1694
Ian Rogersd81871c2011-10-03 13:57:23 -07001695bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001696#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001698 gDvm.verifierStats.instrsReexamined++;
1699 } else {
1700 gDvm.verifierStats.instrsExamined++;
1701 }
1702#endif
1703
1704 /*
1705 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001706 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001707 * control to another statement:
1708 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001709 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001710 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001711 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001712 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001713 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001714 * throw an exception that is handled by an encompassing "try"
1715 * block.
1716 *
1717 * We can also return, in which case there is no successor instruction
1718 * from this point.
1719 *
1720 * The behavior can be determined from the OpcodeFlags.
1721 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1723 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001724 Instruction::DecodedInstruction dec_insn(inst);
1725 int opcode_flag = inst->Flag();
1726
jeffhaobdb76512011-09-07 11:43:16 -07001727 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001728 bool just_set_result = false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 if (false) {
1730 // Generate processing back trace to debug verifier
1731 LogVerifyInfo() << "Processing " << *inst << std::endl << *work_line_.get();
1732 }
jeffhaobdb76512011-09-07 11:43:16 -07001733
1734 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001735 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001736 * can throw an exception, we will copy/merge this into the "catch"
1737 * address rather than work_line, because we don't want the result
1738 * from the "successful" code path (e.g. a check-cast that "improves"
1739 * a type) to be visible to the exception handler.
1740 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1742 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001743 } else {
1744#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001746#endif
1747 }
1748
1749 switch (dec_insn.opcode_) {
1750 case Instruction::NOP:
1751 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001752 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001753 * a signature that looks like a NOP; if we see one of these in
1754 * the course of executing code then we have a problem.
1755 */
1756 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001758 }
1759 break;
1760
1761 case Instruction::MOVE:
1762 case Instruction::MOVE_FROM16:
1763 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
1766 case Instruction::MOVE_WIDE:
1767 case Instruction::MOVE_WIDE_FROM16:
1768 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771 case Instruction::MOVE_OBJECT:
1772 case Instruction::MOVE_OBJECT_FROM16:
1773 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776
1777 /*
1778 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001779 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001780 * might want to hold the result in an actual CPU register, so the
1781 * Dalvik spec requires that these only appear immediately after an
1782 * invoke or filled-new-array.
1783 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001784 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001785 * redundant with the reset done below, but it can make the debug info
1786 * easier to read in some cases.)
1787 */
1788 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001790 break;
1791 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001799 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 * This statement can only appear as the first instruction in an exception handler (though not
1801 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001802 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001803 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001805 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001807 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001809 }
1810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1814 if (!GetMethodReturnType().IsUnknown()) {
1815 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1816 }
jeffhaobdb76512011-09-07 11:43:16 -07001817 }
1818 break;
1819 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001821 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 const RegType& return_type = GetMethodReturnType();
1823 if (!return_type.IsCategory1Types()) {
1824 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1825 } else {
1826 // Compilers may generate synthetic functions that write byte values into boolean fields.
1827 // Also, it may use integer values for boolean, byte, short, and character return types.
1828 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1829 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1830 ((return_type.IsBoolean() || return_type.IsByte() ||
1831 return_type.IsShort() || return_type.IsChar()) &&
1832 src_type.IsInteger()));
1833 /* check the register contents */
1834 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1835 if (failure_ != VERIFY_ERROR_NONE) {
1836 Fail(VERIFY_ERROR_GENERIC) << "return-1nr on invalid register v" << dec_insn.vA_;
1837 }
jeffhaobdb76512011-09-07 11:43:16 -07001838 }
1839 }
1840 break;
1841 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001843 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 const RegType& return_type = GetMethodReturnType();
1845 if (!return_type.IsCategory2Types()) {
1846 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1847 } else {
1848 /* check the register contents */
1849 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1850 if (failure_ != VERIFY_ERROR_NONE) {
1851 Fail(failure_) << "return-wide on invalid register pair v" << dec_insn.vA_;
1852 }
jeffhaobdb76512011-09-07 11:43:16 -07001853 }
1854 }
1855 break;
1856 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1858 const RegType& return_type = GetMethodReturnType();
1859 if (!return_type.IsReferenceTypes()) {
1860 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1861 } else {
1862 /* return_type is the *expected* return type, not register value */
1863 DCHECK(!return_type.IsZero());
1864 DCHECK(!return_type.IsUninitializedReference());
1865 // Verify that the reference in vAA is an instance of the type in "return_type". The Zero
1866 // type is allowed here. If the method is declared to return an interface, then any
1867 // initialized reference is acceptable.
1868 // Note GetClassFromRegister fails if the register holds an uninitialized reference, so
1869 // we do not allow them to be returned.
1870 Class* decl_class = return_type.GetClass();
1871 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
1872 if (res_class != NULL && failure_ == VERIFY_ERROR_NONE) {
1873 if (!decl_class->IsInterface() && !decl_class->IsAssignableFrom(res_class)) {
1874 Fail(VERIFY_ERROR_GENERIC) << "returning " << PrettyClassAndClassLoader(res_class)
1875 << ", declared " << PrettyClassAndClassLoader(res_class);
1876 }
jeffhaobdb76512011-09-07 11:43:16 -07001877 }
1878 }
1879 }
1880 break;
1881
1882 case Instruction::CONST_4:
1883 case Instruction::CONST_16:
1884 case Instruction::CONST:
1885 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
1888 case Instruction::CONST_HIGH16:
1889 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 work_line_->SetRegisterType(dec_insn.vA_,
1891 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::CONST_WIDE_16:
1894 case Instruction::CONST_WIDE_32:
1895 case Instruction::CONST_WIDE:
1896 case Instruction::CONST_WIDE_HIGH16:
1897 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900 case Instruction::CONST_STRING:
1901 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001903 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001905 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001907 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1909 Fail(failure_) << "unable to resolve const-class " << dec_insn.vB_
1910 << " (" << bad_class_desc << ") in "
1911 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1912 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001913 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001915 }
1916 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
1921 case Instruction::MONITOR_EXIT:
1922 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001923 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001924 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001925 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001926 * to the need to handle asynchronous exceptions, a now-deprecated
1927 * feature that Dalvik doesn't support.)
1928 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001929 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001930 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001932 * structured locking checks are working, the former would have
1933 * failed on the -enter instruction, and the latter is impossible.
1934 *
1935 * This is fortunate, because issue 3221411 prevents us from
1936 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001937 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001938 * some catch blocks (which will show up as "dead" code when
1939 * we skip them here); if we can't, then the code path could be
1940 * "live" so we still need to check it.
1941 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 opcode_flag &= ~Instruction::kThrow;
1943 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001947 /*
1948 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001949 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001950 * we don't try to address it.)
1951 *
1952 * If it fails, an exception is thrown, which we deal with later
1953 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1954 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001956 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1958 Fail(failure_) << "unable to resolve check-cast " << dec_insn.vB_
1959 << " (" << bad_class_desc << ") in "
1960 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1961 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001962 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1964 if (!orig_type.IsReferenceTypes()) {
1965 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1966 } else {
1967 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 }
1970 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 }
1972 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001973 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1975 if (!tmp_type.IsReferenceTypes()) {
1976 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001977 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 /* make sure we can resolve the class; access check is important */
1979 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1980 if (res_class == NULL) {
1981 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
1982 Fail(failure_) << "unable to resolve instance of " << dec_insn.vC_
1983 << " (" << bad_class_desc << ") in "
1984 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1985 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1986 } else {
1987 /* result is boolean */
1988 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
1993 case Instruction::ARRAY_LENGTH: {
1994 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
1995 if (failure_ == VERIFY_ERROR_NONE) {
1996 if (res_class != NULL && !res_class->IsArrayClass()) {
1997 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
1998 } else {
1999 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2000 }
2001 }
2002 break;
2003 }
2004 case Instruction::NEW_INSTANCE: {
2005 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002006 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2008 Fail(failure_) << "unable to resolve new-instance " << dec_insn.vB_
2009 << " (" << bad_class_desc << ") in "
2010 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2011 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2012 } else {
2013 /* can't create an instance of an interface or abstract class */
2014 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2015 Fail(VERIFY_ERROR_INSTANTIATION)
2016 << "new-instance on primitive, interface or abstract class"
2017 << PrettyDescriptor(res_class->GetDescriptor());
2018 } else {
2019 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2020 // Any registers holding previous allocations from this address that have not yet been
2021 // initialized must be marked invalid.
2022 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2023
2024 /* add the new uninitialized reference to the register state */
2025 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2026 }
2027 }
2028 break;
2029 }
2030 case Instruction::NEW_ARRAY: {
2031 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2032 if (res_class == NULL) {
2033 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
2034 Fail(failure_) << "unable to resolve new-array " << dec_insn.vC_
2035 << " (" << bad_class_desc << ") in "
2036 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2037 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002038 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002040 } else {
2041 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002043 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002045 }
2046 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 }
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2050 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002051 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2053 Fail(failure_) << "unable to resolve filled-array " << dec_insn.vB_
2054 << " (" << bad_class_desc << ") in "
2055 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2056 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002057 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002059 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002060 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002061 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002063 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002065 just_set_result = true;
2066 }
2067 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 }
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::CMPL_FLOAT:
2070 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2072 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2073 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::CMPL_DOUBLE:
2076 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2078 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2079 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2083 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2084 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 case Instruction::THROW: {
2087 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2088 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2089 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2090 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2091 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002092 }
2093 }
2094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::GOTO:
2097 case Instruction::GOTO_16:
2098 case Instruction::GOTO_32:
2099 /* no effect on or use of registers */
2100 break;
2101
2102 case Instruction::PACKED_SWITCH:
2103 case Instruction::SPARSE_SWITCH:
2104 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 case Instruction::FILL_ARRAY_DATA: {
2109 /* Similar to the verification done for APUT */
2110 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2111 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002112 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 if (res_class != NULL) {
2114 Class* component_type = res_class->GetComponentType();
2115 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2116 component_type->IsPrimitiveVoid()) {
2117 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2118 << PrettyDescriptor(res_class->GetDescriptor());
2119 } else {
2120 const RegType& value_type = reg_types_.FromClass(component_type);
2121 DCHECK(!value_type.IsUnknown());
2122 // Now verify if the element width in the table matches the element width declared in
2123 // the array
2124 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2125 if (array_data[0] != Instruction::kArrayDataSignature) {
2126 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2127 } else {
2128 size_t elem_width = component_type->PrimitiveSize();
2129 // Since we don't compress the data in Dex, expect to see equal width of data stored
2130 // in the table and expected from the array class.
2131 if (array_data[1] != elem_width) {
2132 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2133 << " vs " << elem_width << ")";
2134 }
2135 }
2136 }
jeffhaobdb76512011-09-07 11:43:16 -07002137 }
2138 }
2139 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 }
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 case Instruction::IF_NE: {
2143 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2144 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2145 bool mismatch = false;
2146 if (reg_type1.IsZero()) { // zero then integral or reference expected
2147 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2148 } else if (reg_type1.IsReferenceTypes()) { // both references?
2149 mismatch = !reg_type2.IsReferenceTypes();
2150 } else { // both integral?
2151 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2152 }
2153 if (mismatch) {
2154 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2155 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002156 }
2157 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 }
jeffhaobdb76512011-09-07 11:43:16 -07002159 case Instruction::IF_LT:
2160 case Instruction::IF_GE:
2161 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 case Instruction::IF_LE: {
2163 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2164 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2165 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2166 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2167 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002168 }
2169 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 }
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 case Instruction::IF_NEZ: {
2173 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2174 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2175 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 case Instruction::IF_LTZ:
2180 case Instruction::IF_GEZ:
2181 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 case Instruction::IF_LEZ: {
2183 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2184 if (!reg_type.IsIntegralTypes()) {
2185 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2186 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 }
jeffhaobdb76512011-09-07 11:43:16 -07002190 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2192 break;
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2195 break;
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 VerifyAGet(dec_insn, reg_types_.Char(), true);
2198 break;
jeffhaobdb76512011-09-07 11:43:16 -07002199 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 case Instruction::AGET:
2203 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2204 break;
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 VerifyAGet(dec_insn, reg_types_.Long(), true);
2207 break;
2208 case Instruction::AGET_OBJECT:
2209 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 case Instruction::APUT_BOOLEAN:
2213 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2214 break;
2215 case Instruction::APUT_BYTE:
2216 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2217 break;
2218 case Instruction::APUT_CHAR:
2219 VerifyAPut(dec_insn, reg_types_.Char(), true);
2220 break;
2221 case Instruction::APUT_SHORT:
2222 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233
jeffhaobdb76512011-09-07 11:43:16 -07002234 case Instruction::IGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 VerifyIGet(dec_insn, reg_types_.Boolean(), true);
2236 break;
jeffhaobdb76512011-09-07 11:43:16 -07002237 case Instruction::IGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 VerifyIGet(dec_insn, reg_types_.Byte(), true);
2239 break;
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::IGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 VerifyIGet(dec_insn, reg_types_.Char(), true);
2242 break;
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 VerifyIGet(dec_insn, reg_types_.Short(), true);
2245 break;
2246 case Instruction::IGET:
2247 VerifyIGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249 case Instruction::IGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 VerifyIGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::IGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 VerifyIGet(dec_insn, reg_types_.JavaLangObject(), false);
2254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 case Instruction::IPUT_BOOLEAN:
2257 VerifyIPut(dec_insn, reg_types_.Boolean(), true);
2258 break;
2259 case Instruction::IPUT_BYTE:
2260 VerifyIPut(dec_insn, reg_types_.Byte(), true);
2261 break;
2262 case Instruction::IPUT_CHAR:
2263 VerifyIPut(dec_insn, reg_types_.Char(), true);
2264 break;
2265 case Instruction::IPUT_SHORT:
2266 VerifyIPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::IPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 VerifyIPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::IPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 VerifyIPut(dec_insn, reg_types_.Long(), true);
2273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::IPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 VerifyIPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277
jeffhaobdb76512011-09-07 11:43:16 -07002278 case Instruction::SGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 VerifySGet(dec_insn, reg_types_.Boolean(), true);
2280 break;
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::SGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 VerifySGet(dec_insn, reg_types_.Byte(), true);
2283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::SGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 VerifySGet(dec_insn, reg_types_.Char(), true);
2286 break;
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::SGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 VerifySGet(dec_insn, reg_types_.Short(), true);
2289 break;
2290 case Instruction::SGET:
2291 VerifySGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::SGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 VerifySGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::SGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 VerifySGet(dec_insn, reg_types_.JavaLangObject(), false);
2298 break;
2299
2300 case Instruction::SPUT_BOOLEAN:
2301 VerifySPut(dec_insn, reg_types_.Boolean(), true);
2302 break;
2303 case Instruction::SPUT_BYTE:
2304 VerifySPut(dec_insn, reg_types_.Byte(), true);
2305 break;
2306 case Instruction::SPUT_CHAR:
2307 VerifySPut(dec_insn, reg_types_.Char(), true);
2308 break;
2309 case Instruction::SPUT_SHORT:
2310 VerifySPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::SPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 VerifySPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::SPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 VerifySPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::SPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 VerifySPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321
2322 case Instruction::INVOKE_VIRTUAL:
2323 case Instruction::INVOKE_VIRTUAL_RANGE:
2324 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 case Instruction::INVOKE_SUPER_RANGE: {
2326 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2327 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2328 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2329 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2330 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2331 if (failure_ == VERIFY_ERROR_NONE) {
2332 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2333 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002334 just_set_result = true;
2335 }
2336 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 }
jeffhaobdb76512011-09-07 11:43:16 -07002338 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 case Instruction::INVOKE_DIRECT_RANGE: {
2340 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2341 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2342 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002343 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 * Some additional checks when calling a constructor. We know from the invocation arg check
2345 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2346 * that to require that called_method->klass is the same as this->klass or this->super,
2347 * allowing the latter only if the "this" argument is the same as the "this" argument to
2348 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002349 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 if (called_method->IsConstructor()) {
2351 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2352 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354
2355 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 if (this_type.IsZero()) {
2357 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002361 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002362
2363 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2365 if (this_class != method_->GetDeclaringClass()) {
2366 Fail(VERIFY_ERROR_GENERIC)
2367 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 }
2370 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 }
2374
2375 /* arg must be an uninitialized reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002376 if (this_type.IsInitialized()) {
2377 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2378 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 }
2381
2382 /*
2383 * Replace the uninitialized reference with an initialized
jeffhaod1f0fde2011-09-08 17:25:33 -07002384 * one, and clear the entry in the uninit map. We need to
jeffhaobdb76512011-09-07 11:43:16 -07002385 * do this for all registers that have the same object
2386 * instance in them, not just the "this" register.
2387 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 work_line_->MarkRefsAsInitialized(this_type);
2389 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002391 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2393 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002394 just_set_result = true;
2395 }
2396 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 }
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 case Instruction::INVOKE_STATIC_RANGE: {
2400 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2401 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2402 if (failure_ == VERIFY_ERROR_NONE) {
2403 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2404 work_line_->SetResultRegisterType(return_type);
2405 just_set_result = true;
2406 }
jeffhaobdb76512011-09-07 11:43:16 -07002407 }
2408 break;
2409 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 case Instruction::INVOKE_INTERFACE_RANGE: {
2411 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2412 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2413 if (failure_ == VERIFY_ERROR_NONE) {
2414 Class* called_interface = abs_method->GetDeclaringClass();
2415 if (!called_interface->IsInterface()) {
2416 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2417 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
jeffhaobdb76512011-09-07 11:43:16 -07002419 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 /* Get the type of the "this" arg, which should either be a sub-interface of called
2421 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002422 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2424 if (failure_ == VERIFY_ERROR_NONE) {
2425 if (this_type.IsZero()) {
2426 /* null pointer always passes (and always fails at runtime) */
2427 } else {
2428 Class* this_class = this_type.GetClass();
2429 if (this_type.IsUninitializedReference() || this_class == NULL) {
2430 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized";
2431 break;
2432 }
2433 if (!this_class->IsObjectClass() && !called_interface->IsAssignableFrom(this_class)) {
2434 Fail(VERIFY_ERROR_GENERIC) << "unable to match abstract method '"
2435 << PrettyMethod(abs_method) << "' with "
2436 << PrettyDescriptor(this_class->GetDescriptor())
2437 << " interfaces";
2438 break;
2439 }
2440 }
jeffhaobdb76512011-09-07 11:43:16 -07002441 }
2442 }
jeffhaobdb76512011-09-07 11:43:16 -07002443 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 * We don't have an object instance, so we can't find the concrete method. However, all of
2445 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002446 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 const RegType& return_type = reg_types_.FromClass(abs_method->GetReturnType());
2448 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002449 just_set_result = true;
2450 }
2451 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 }
jeffhaobdb76512011-09-07 11:43:16 -07002453 case Instruction::NEG_INT:
2454 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::NEG_LONG:
2458 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002463 break;
2464 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512
2513 case Instruction::ADD_INT:
2514 case Instruction::SUB_INT:
2515 case Instruction::MUL_INT:
2516 case Instruction::REM_INT:
2517 case Instruction::DIV_INT:
2518 case Instruction::SHL_INT:
2519 case Instruction::SHR_INT:
2520 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::AND_INT:
2524 case Instruction::OR_INT:
2525 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::ADD_LONG:
2529 case Instruction::SUB_LONG:
2530 case Instruction::MUL_LONG:
2531 case Instruction::DIV_LONG:
2532 case Instruction::REM_LONG:
2533 case Instruction::AND_LONG:
2534 case Instruction::OR_LONG:
2535 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::SHL_LONG:
2539 case Instruction::SHR_LONG:
2540 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 /* shift distance is Int, making these different from other binary operations */
2542 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::ADD_FLOAT:
2545 case Instruction::SUB_FLOAT:
2546 case Instruction::MUL_FLOAT:
2547 case Instruction::DIV_FLOAT:
2548 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 case Instruction::ADD_DOUBLE:
2552 case Instruction::SUB_DOUBLE:
2553 case Instruction::MUL_DOUBLE:
2554 case Instruction::DIV_DOUBLE:
2555 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::ADD_INT_2ADDR:
2559 case Instruction::SUB_INT_2ADDR:
2560 case Instruction::MUL_INT_2ADDR:
2561 case Instruction::REM_INT_2ADDR:
2562 case Instruction::SHL_INT_2ADDR:
2563 case Instruction::SHR_INT_2ADDR:
2564 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::AND_INT_2ADDR:
2568 case Instruction::OR_INT_2ADDR:
2569 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::ADD_LONG_2ADDR:
2576 case Instruction::SUB_LONG_2ADDR:
2577 case Instruction::MUL_LONG_2ADDR:
2578 case Instruction::DIV_LONG_2ADDR:
2579 case Instruction::REM_LONG_2ADDR:
2580 case Instruction::AND_LONG_2ADDR:
2581 case Instruction::OR_LONG_2ADDR:
2582 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::SHL_LONG_2ADDR:
2586 case Instruction::SHR_LONG_2ADDR:
2587 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::ADD_FLOAT_2ADDR:
2591 case Instruction::SUB_FLOAT_2ADDR:
2592 case Instruction::MUL_FLOAT_2ADDR:
2593 case Instruction::DIV_FLOAT_2ADDR:
2594 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::ADD_DOUBLE_2ADDR:
2598 case Instruction::SUB_DOUBLE_2ADDR:
2599 case Instruction::MUL_DOUBLE_2ADDR:
2600 case Instruction::DIV_DOUBLE_2ADDR:
2601 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::ADD_INT_LIT16:
2605 case Instruction::RSUB_INT:
2606 case Instruction::MUL_INT_LIT16:
2607 case Instruction::DIV_INT_LIT16:
2608 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::AND_INT_LIT16:
2612 case Instruction::OR_INT_LIT16:
2613 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::ADD_INT_LIT8:
2617 case Instruction::RSUB_INT_LIT8:
2618 case Instruction::MUL_INT_LIT8:
2619 case Instruction::DIV_INT_LIT8:
2620 case Instruction::REM_INT_LIT8:
2621 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::AND_INT_LIT8:
2627 case Instruction::OR_INT_LIT8:
2628 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631
2632 /*
2633 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002634 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002635 * inserted in the course of verification, we can expect to see it here.
2636 */
jeffhaob4df5142011-09-19 20:25:32 -07002637 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::UNUSED_EE:
2642 case Instruction::UNUSED_EF:
2643 case Instruction::UNUSED_F2:
2644 case Instruction::UNUSED_F3:
2645 case Instruction::UNUSED_F4:
2646 case Instruction::UNUSED_F5:
2647 case Instruction::UNUSED_F6:
2648 case Instruction::UNUSED_F7:
2649 case Instruction::UNUSED_F8:
2650 case Instruction::UNUSED_F9:
2651 case Instruction::UNUSED_FA:
2652 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002653 case Instruction::UNUSED_F0:
2654 case Instruction::UNUSED_F1:
2655 case Instruction::UNUSED_E3:
2656 case Instruction::UNUSED_E8:
2657 case Instruction::UNUSED_E7:
2658 case Instruction::UNUSED_E4:
2659 case Instruction::UNUSED_E9:
2660 case Instruction::UNUSED_FC:
2661 case Instruction::UNUSED_E5:
2662 case Instruction::UNUSED_EA:
2663 case Instruction::UNUSED_FD:
2664 case Instruction::UNUSED_E6:
2665 case Instruction::UNUSED_EB:
2666 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002667 case Instruction::UNUSED_3E:
2668 case Instruction::UNUSED_3F:
2669 case Instruction::UNUSED_40:
2670 case Instruction::UNUSED_41:
2671 case Instruction::UNUSED_42:
2672 case Instruction::UNUSED_43:
2673 case Instruction::UNUSED_73:
2674 case Instruction::UNUSED_79:
2675 case Instruction::UNUSED_7A:
2676 case Instruction::UNUSED_EC:
2677 case Instruction::UNUSED_FF:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << *inst;
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680
2681 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002682 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002683 * complain if an instruction is missing (which is desirable).
2684 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002686
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 if (failure_ != VERIFY_ERROR_NONE) {
2688 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002689 /* immediate failure, reject class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 fail_messages_ << std::endl << "Rejecting opcode " << *inst;
jeffhaobdb76512011-09-07 11:43:16 -07002691 return false;
2692 } else {
2693 /* replace opcode and continue on */
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 fail_messages_ << std::endl << "Replacing opcode " << *inst;
2695 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002696 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002698 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002700 opcode_flag = Instruction::kThrow;
2701 }
2702 }
jeffhaobdb76512011-09-07 11:43:16 -07002703 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 * If we didn't just set the result register, clear it out. This ensures that you can only use
2705 * "move-result" immediately after the result is set. (We could check this statically, but it's
2706 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002707 */
2708 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002710 }
2711
jeffhaoa0a764a2011-09-16 10:43:38 -07002712 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002713 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2715 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2716 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002717 return false;
2718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2720 // next instruction isn't one.
2721 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002722 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 }
2724 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2725 if (next_line != NULL) {
2726 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2727 // needed.
2728 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002729 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 }
jeffhaobdb76512011-09-07 11:43:16 -07002731 } else {
2732 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 * We're not recording register data for the next instruction, so we don't know what the prior
2734 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002735 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002737 }
2738 }
2739
2740 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002741 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002742 *
2743 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002744 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002745 * somebody could get a reference field, check it for zero, and if the
2746 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002747 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002748 * that, and will reject the code.
2749 *
2750 * TODO: avoid re-fetching the branch target
2751 */
2752 if ((opcode_flag & Instruction::kBranch) != 0) {
2753 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002755 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002757 return false;
2758 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002759 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002761 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 }
jeffhaobdb76512011-09-07 11:43:16 -07002763 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002765 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 }
jeffhaobdb76512011-09-07 11:43:16 -07002767 }
2768
2769 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002770 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002771 *
2772 * We've already verified that the table is structurally sound, so we
2773 * just need to walk through and tag the targets.
2774 */
2775 if ((opcode_flag & Instruction::kSwitch) != 0) {
2776 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2777 const uint16_t* switch_insns = insns + offset_to_switch;
2778 int switch_count = switch_insns[1];
2779 int offset_to_targets, targ;
2780
2781 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2782 /* 0 = sig, 1 = count, 2/3 = first key */
2783 offset_to_targets = 4;
2784 } else {
2785 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002786 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002787 offset_to_targets = 2 + 2 * switch_count;
2788 }
2789
2790 /* verify each switch target */
2791 for (targ = 0; targ < switch_count; targ++) {
2792 int offset;
2793 uint32_t abs_offset;
2794
2795 /* offsets are 32-bit, and only partly endian-swapped */
2796 offset = switch_insns[offset_to_targets + targ * 2] |
2797 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 abs_offset = work_insn_idx_ + offset;
2799 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2800 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002801 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 }
2803 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002804 return false;
2805 }
2806 }
2807
2808 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2810 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002811 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2813 bool within_catch_all = false;
2814 DexFile::CatchHandlerIterator iterator =
2815 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002816
2817 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2819 within_catch_all = true;
2820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2823 * "work_regs", because at runtime the exception will be thrown before the instruction
2824 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002825 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002827 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 }
jeffhaobdb76512011-09-07 11:43:16 -07002829 }
2830
2831 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2833 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002834 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 * The state in work_line reflects the post-execution state. If the current instruction is a
2838 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002839 * it will do so before grabbing the lock).
2840 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2842 Fail(VERIFY_ERROR_GENERIC)
2843 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002844 return false;
2845 }
2846 }
2847 }
2848
jeffhaod1f0fde2011-09-08 17:25:33 -07002849 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 if ((opcode_flag & Instruction::kReturn) != 0) {
2851 if(!work_line_->VerifyMonitorStackEmpty()) {
2852 return false;
2853 }
jeffhaobdb76512011-09-07 11:43:16 -07002854 }
2855
2856 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002857 * Update start_guess. Advance to the next instruction of that's
2858 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002859 * neither of those exists we're in a return or throw; leave start_guess
2860 * alone and let the caller sort it out.
2861 */
2862 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002864 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2865 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002867 }
2868
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2870 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002871
2872 return true;
2873}
2874
Ian Rogersd81871c2011-10-03 13:57:23 -07002875Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2876 const Class* referrer = method_->GetDeclaringClass();
2877 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2878 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002879
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 if (res_class == NULL) {
2881 Thread::Current()->ClearException();
2882 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2883 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2884 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2885 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2886 << res_class->GetDescriptor()->ToModifiedUtf8();
2887 }
2888 return res_class;
2889}
2890
2891Class* DexVerifier::GetCaughtExceptionType() {
2892 Class* common_super = NULL;
2893 if (code_item_->tries_size_ != 0) {
2894 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2895 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2896 for (uint32_t i = 0; i < handlers_size; i++) {
2897 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2898 for (; !iterator.HasNext(); iterator.Next()) {
2899 DexFile::CatchHandlerItem handler = iterator.Get();
2900 if (handler.address_ == (uint32_t) work_insn_idx_) {
2901 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2902 common_super = JavaLangThrowable();
2903 } else {
2904 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2905 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2906 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2907 * test, so is essentially harmless.
2908 */
2909 if (klass == NULL) {
2910 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2911 << handler.type_idx_ << " ("
2912 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2913 return NULL;
2914 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2915 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2916 return NULL;
2917 } else if (common_super == NULL) {
2918 common_super = klass;
2919 } else {
2920 common_super = RegType::ClassJoin(common_super, klass);
2921 }
2922 }
2923 }
2924 }
2925 handlers_ptr = iterator.GetData();
2926 }
2927 }
2928 if (common_super == NULL) {
2929 /* no catch blocks, or no catches with classes we can find */
2930 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2931 }
2932 return common_super;
2933}
2934
2935Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2936 Class* referrer = method_->GetDeclaringClass();
2937 DexCache* dex_cache = referrer->GetDexCache();
2938 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2939 if (res_method == NULL) {
2940 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2941 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2942 if (klass == NULL) {
2943 DCHECK(failure_ != VERIFY_ERROR_NONE);
2944 return NULL;
2945 }
2946 const char* name = dex_file_->dexStringById(method_id.name_idx_);
2947 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2948 if (is_direct) {
2949 res_method = klass->FindDirectMethod(name, signature);
2950 } else if (klass->IsInterface()) {
2951 res_method = klass->FindInterfaceMethod(name, signature);
2952 } else {
2953 res_method = klass->FindVirtualMethod(name, signature);
2954 }
2955 if (res_method != NULL) {
2956 dex_cache->SetResolvedMethod(method_idx, res_method);
2957 } else {
2958 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2959 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2960 << " " << signature;
2961 return NULL;
2962 }
2963 }
2964 /* Check if access is allowed. */
2965 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2966 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2967 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2968 return NULL;
2969 }
2970 return res_method;
2971}
2972
2973Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2974 MethodType method_type, bool is_range, bool is_super) {
2975 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2976 // we're making.
2977 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2978 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2979 if (res_method == NULL) {
2980 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2981 const char* method_name = dex_file_->GetMethodName(method_id);
2982 std::string method_signature = dex_file_->GetMethodSignature(method_id);
2983 const char* class_descriptor = dex_file_->GetMethodClassDescriptor(method_id);
2984 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve method " << dec_insn.vB_ << ": "
2985 << class_descriptor << "." << method_name << " " << method_signature;
2986 return NULL;
2987 }
2988 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2989 // enforce them here.
2990 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
2991 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
2992 << PrettyMethod(res_method);
2993 return NULL;
2994 }
2995 // See if the method type implied by the invoke instruction matches the access flags for the
2996 // target method.
2997 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2998 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2999 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3000 ) {
3001 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3002 << PrettyMethod(res_method);
3003 return NULL;
3004 }
3005 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3006 // has a vtable entry for the target method.
3007 if (is_super) {
3008 DCHECK(method_type == METHOD_VIRTUAL);
3009 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3010 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3011 if (super == NULL) { // Only Object has no super class
3012 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3013 << " to super " << PrettyMethod(res_method);
3014 } else {
3015 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3016 << " to super " << PrettyDescriptor(super->GetDescriptor())
3017 << "." << res_method->GetName()->ToModifiedUtf8()
3018 << " " << res_method->GetSignature()->ToModifiedUtf8();
3019
3020 }
3021 return NULL;
3022 }
3023 }
3024 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3025 // match the call to the signature. Also, we might might be calling through an abstract method
3026 // definition (which doesn't have register count values).
3027 int expected_args = dec_insn.vA_;
3028 /* caught by static verifier */
3029 DCHECK(is_range || expected_args <= 5);
3030 if (expected_args > code_item_->outs_size_) {
3031 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3032 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3033 return NULL;
3034 }
3035 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3036 if (sig[0] != '(') {
3037 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3038 << " as descriptor doesn't start with '(': " << sig;
3039 return NULL;
3040 }
jeffhaobdb76512011-09-07 11:43:16 -07003041 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 * Check the "this" argument, which must be an instance of the class
3043 * that declared the method. For an interface class, we don't do the
3044 * full interface merge, so we can't do a rigorous check here (which
3045 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003046 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 int actual_args = 0;
3048 if (!res_method->IsStatic()) {
3049 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3050 if (failure_ != VERIFY_ERROR_NONE) {
3051 return NULL;
3052 }
3053 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3054 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3055 return NULL;
3056 }
3057 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3058 Class* actual_this_ref = actual_arg_type.GetClass();
3059 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
3060 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '"
3061 << PrettyDescriptor(actual_this_ref->GetDescriptor()) << "' not instance of '"
3062 << PrettyDescriptor(res_method->GetDeclaringClass()->GetDescriptor()) << "'";
3063 return NULL;
3064 }
3065 }
3066 actual_args++;
3067 }
3068 /*
3069 * Process the target method's signature. This signature may or may not
3070 * have been verified, so we can't assume it's properly formed.
3071 */
3072 size_t sig_offset = 0;
3073 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3074 if (actual_args >= expected_args) {
3075 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3076 << "'. Expected " << expected_args << " args, found more ("
3077 << sig.substr(sig_offset) << ")";
3078 return NULL;
3079 }
3080 std::string descriptor;
3081 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3082 size_t end;
3083 if (sig[sig_offset] == 'L') {
3084 end = sig.find(';', sig_offset);
3085 } else {
3086 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3087 if (sig[end] == 'L') {
3088 end = sig.find(';', end);
3089 }
3090 }
3091 if (end == std::string::npos) {
3092 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3093 << "bad signature component '" << sig << "' (missing ';')";
3094 return NULL;
3095 }
3096 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3097 sig_offset = end;
3098 } else {
3099 descriptor = sig[sig_offset];
3100 }
3101 const RegType& reg_type =
3102 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
3103 if (reg_type.IsUnknown()) {
3104 DCHECK(Thread::Current()->IsExceptionPending());
3105 Thread::Current()->ClearException();
3106 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3107 << " bad descriptor '" << descriptor << "' in signature " << sig;
3108 return NULL;
3109 } else {
3110 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3111 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3112 return NULL;
3113 }
3114 }
3115 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3116 }
3117 if (sig[sig_offset] != ')') {
3118 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3119 return NULL;
3120 }
3121 if (actual_args != expected_args) {
3122 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3123 << " expected " << expected_args << " args, found " << actual_args;
3124 return NULL;
3125 } else {
3126 return res_method;
3127 }
3128}
3129
3130void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3131 const RegType& insn_type, bool is_primitive) {
3132 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3133 if (!index_type.IsArrayIndexTypes()) {
3134 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3135 } else {
3136 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3137 if (failure_ == VERIFY_ERROR_NONE) {
3138 if (array_class == NULL) {
3139 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3140 // instruction type. TODO: have a proper notion of bottom here.
3141 if (!is_primitive || insn_type.IsCategory1Types()) {
3142 // Reference or category 1
3143 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3144 } else {
3145 // Category 2
3146 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3147 }
3148 } else {
3149 /* verify the class */
3150 Class* component_class = array_class->GetComponentType();
3151 const RegType& component_type = reg_types_.FromClass(component_class);
3152 if (!array_class->IsArrayClass()) {
3153 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3154 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3155 } else if (component_class->IsPrimitive() && !is_primitive) {
3156 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3157 << PrettyDescriptor(array_class->GetDescriptor())
3158 << " source for aget-object";
3159 } else if (!component_class->IsPrimitive() && is_primitive) {
3160 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3161 << PrettyDescriptor(array_class->GetDescriptor())
3162 << " source for category 1 aget";
3163 } else if (is_primitive && !insn_type.Equals(component_type) &&
3164 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3165 (insn_type.IsLong() && component_type.IsDouble()))) {
3166 Fail(VERIFY_ERROR_GENERIC) << "array type "
3167 << PrettyDescriptor(array_class->GetDescriptor())
3168 << " incompatible with aget of type " << insn_type;
3169 } else {
3170 // Use knowledge of the field type which is stronger than the type inferred from the
3171 // instruction, which can't differentiate object types and ints from floats, longs from
3172 // doubles.
3173 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3174 }
3175 }
3176 }
3177 }
3178}
3179
3180void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3181 const RegType& insn_type, bool is_primitive) {
3182 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3183 if (!index_type.IsArrayIndexTypes()) {
3184 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3185 } else {
3186 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3187 if (failure_ == VERIFY_ERROR_NONE) {
3188 if (array_class == NULL) {
3189 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3190 // instruction type.
3191 } else {
3192 /* verify the class */
3193 Class* component_class = array_class->GetComponentType();
3194 const RegType& component_type = reg_types_.FromClass(component_class);
3195 if (!array_class->IsArrayClass()) {
3196 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3197 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3198 } else if (component_class->IsPrimitive() && !is_primitive) {
3199 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3200 << PrettyDescriptor(array_class->GetDescriptor())
3201 << " source for aput-object";
3202 } else if (!component_class->IsPrimitive() && is_primitive) {
3203 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3204 << PrettyDescriptor(array_class->GetDescriptor())
3205 << " source for category 1 aput";
3206 } else if (is_primitive && !insn_type.Equals(component_type) &&
3207 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3208 (insn_type.IsLong() && component_type.IsDouble()))) {
3209 Fail(VERIFY_ERROR_GENERIC) << "array type "
3210 << PrettyDescriptor(array_class->GetDescriptor())
3211 << " incompatible with aput of type " << insn_type;
3212 } else {
3213 // The instruction agrees with the type of array, confirm the value to be stored does too
3214 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3215 }
3216 }
3217 }
3218 }
3219}
3220
3221Field* DexVerifier::GetStaticField(int field_idx) {
3222 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3223 if (field == NULL) {
3224 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3225 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3226 << dex_file_->GetFieldName(field_id) << ") in "
3227 << dex_file_->GetFieldClassDescriptor(field_id);
3228 DCHECK(Thread::Current()->IsExceptionPending());
3229 Thread::Current()->ClearException();
3230 return NULL;
3231 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3232 field->GetAccessFlags())) {
3233 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3234 << " from " << PrettyClass(method_->GetDeclaringClass());
3235 return NULL;
3236 } else if (!field->IsStatic()) {
3237 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3238 return NULL;
3239 } else {
3240 return field;
3241 }
3242}
3243
3244void DexVerifier::VerifySGet(const Instruction::DecodedInstruction& dec_insn,
3245 const RegType& insn_type, bool is_primitive) {
3246 Field* field = GetStaticField(dec_insn.vB_);
3247 if (field != NULL) {
3248 DCHECK(field->GetDeclaringClass()->IsResolved());
3249 Class* field_class = field->GetType();
3250 Class* insn_class = insn_type.GetClass();
3251 if (is_primitive) {
3252 if (field_class == insn_class ||
3253 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3254 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3255 // expected that read is of the correct primitive type or that int reads are reading
3256 // floats or long reads are reading doubles
3257 } else {
3258 // This is a global failure rather than a class change failure as the instructions and
3259 // the descriptors for the type should have been consistent within the same file at
3260 // compile time
3261 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3262 << " to be of type " << PrettyClass(insn_class)
3263 << " but found type " << PrettyClass(field_class) << " in sget";
3264 return;
3265 }
3266 } else {
3267 if (!insn_class->IsAssignableFrom(field_class)) {
3268 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3269 << " to be of type " << PrettyClass(insn_class)
3270 << " but found type " << PrettyClass(field_class)
3271 << " in sget-object";
3272 return;
3273 }
3274 }
3275 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3276 }
3277}
3278
3279void DexVerifier::VerifySPut(const Instruction::DecodedInstruction& dec_insn,
3280 const RegType& insn_type, bool is_primitive) {
3281 Field* field = GetStaticField(dec_insn.vB_);
3282 if (field != NULL) {
3283 DCHECK(field->GetDeclaringClass()->IsResolved());
3284 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3285 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final static field " << PrettyField(field)
3286 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3287 return;
3288 }
3289 Class* field_class = field->GetType();
3290 Class* insn_class = insn_type.GetClass();
3291 if (is_primitive) {
3292 if (field_class == insn_class ||
3293 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3294 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3295 // expected that read is of the correct primitive type or that int reads are reading
3296 // floats or long reads are reading doubles
3297 } else {
3298 // This is a global failure rather than a class change failure as the instructions and
3299 // the descriptors for the type should have been consistent within the same file at
3300 // compile time
3301 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3302 << " to be of type " << PrettyClass(insn_class)
3303 << " but found type " << PrettyClass(field_class) << " in sput";
3304 return;
3305 }
3306 } else {
3307 if (!insn_class->IsAssignableFrom(field_class)) {
3308 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3309 << " to be compatible with type " << insn_type
3310 << " but found type " << PrettyClass(field_class)
3311 << " in sput-object";
3312 return;
3313 }
3314 }
3315 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3316 }
3317}
3318
3319Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3320 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3321 if (field == NULL) {
3322 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3323 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3324 << dex_file_->GetFieldName(field_id) << ") in "
3325 << dex_file_->GetFieldClassDescriptor(field_id);
3326 DCHECK(Thread::Current()->IsExceptionPending());
3327 Thread::Current()->ClearException();
3328 return NULL;
3329 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3330 field->GetAccessFlags())) {
3331 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3332 << " from " << PrettyClass(method_->GetDeclaringClass());
3333 return NULL;
3334 } else if (field->IsStatic()) {
3335 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3336 << " to not be static";
3337 return NULL;
3338 } else if (obj_type.IsZero()) {
3339 // Cannot infer and check type, however, access will cause null pointer exception
3340 return field;
3341 } else if(obj_type.IsUninitializedReference() &&
3342 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3343 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3344 // Field accesses through uninitialized references are only allowable for constructors where
3345 // the field is declared in this class
3346 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3347 << " of a not fully initialized object within the context of "
3348 << PrettyMethod(method_);
3349 return NULL;
3350 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3351 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3352 // of C1. For resolution to occur the declared class of the field must be compatible with
3353 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3354 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3355 << " from object of type " << PrettyClass(obj_type.GetClass());
3356 return NULL;
3357 } else {
3358 return field;
3359 }
3360}
3361
3362void DexVerifier::VerifyIGet(const Instruction::DecodedInstruction& dec_insn,
3363 const RegType& insn_type, bool is_primitive) {
3364 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3365 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3366 if (field != NULL) {
3367 DCHECK(field->GetDeclaringClass()->IsResolved());
3368 Class* field_class = field->GetType();
3369 Class* insn_class = insn_type.GetClass();
3370 if (is_primitive) {
3371 if (field_class == insn_class ||
3372 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3373 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3374 // expected that read is of the correct primitive type or that int reads are reading
3375 // floats or long reads are reading doubles
3376 } else {
3377 // This is a global failure rather than a class change failure as the instructions and
3378 // the descriptors for the type should have been consistent within the same file at
3379 // compile time
3380 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3381 << " to be of type " << PrettyClass(insn_class)
3382 << " but found type " << PrettyClass(field_class) << " in iget";
3383 return;
3384 }
3385 } else {
3386 if (!insn_class->IsAssignableFrom(field_class)) {
3387 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3388 << " to be compatible with type " << PrettyClass(insn_class)
3389 << " but found type " << PrettyClass(field_class) << " in iget-object";
3390 return;
3391 }
3392 }
3393 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3394 }
3395}
3396
3397void DexVerifier::VerifyIPut(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) {
3402 DCHECK(field->GetDeclaringClass()->IsResolved());
3403 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3404 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3405 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3406 return;
3407 }
3408 Class* field_class = field->GetType();
3409 Class* insn_class = insn_type.GetClass();
3410 if (is_primitive) {
3411 if (field_class == insn_class ||
3412 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3413 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3414 // expected that read is of the correct primitive type or that int reads are reading
3415 // floats or long reads are reading doubles
3416 } else {
3417 // This is a global failure rather than a class change failure as the instructions and
3418 // the descriptors for the type should have been consistent within the same file at
3419 // compile time
3420 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3421 << " to be of type " << PrettyClass(insn_class)
3422 << " but found type " << PrettyClass(field_class) << " in iput";
3423 return;
3424 }
3425 } else {
3426 if (!insn_class->IsAssignableFrom(field_class)) {
3427 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3428 << " to be compatible with type " << PrettyClass(insn_class)
3429 << " but found type " << PrettyClass(field_class)
3430 << " in iput-object";
3431 return;
3432 }
3433 }
3434 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3435 }
3436}
3437
3438bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3439 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3440 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3441 return false;
3442 }
3443 return true;
3444}
3445
3446void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3447 Class* res_class, bool is_range) {
3448 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3449 /*
3450 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3451 * list and fail. It's legal, if silly, for arg_count to be zero.
3452 */
3453 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3454 uint32_t arg_count = dec_insn.vA_;
3455 for (size_t ui = 0; ui < arg_count; ui++) {
3456 uint32_t get_reg;
3457
3458 if (is_range)
3459 get_reg = dec_insn.vC_ + ui;
3460 else
3461 get_reg = dec_insn.arg_[ui];
3462
3463 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3464 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3465 << ") not valid";
3466 return;
3467 }
3468 }
3469}
3470
3471void DexVerifier::ReplaceFailingInstruction() {
3472 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3473 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3474 VerifyErrorRefType ref_type;
3475 switch (inst->Opcode()) {
3476 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003477 case Instruction::CHECK_CAST:
3478 case Instruction::INSTANCE_OF:
3479 case Instruction::NEW_INSTANCE:
3480 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003482 case Instruction::FILLED_NEW_ARRAY_RANGE:
3483 ref_type = VERIFY_ERROR_REF_CLASS;
3484 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003486 case Instruction::IGET_BOOLEAN:
3487 case Instruction::IGET_BYTE:
3488 case Instruction::IGET_CHAR:
3489 case Instruction::IGET_SHORT:
3490 case Instruction::IGET_WIDE:
3491 case Instruction::IGET_OBJECT:
3492 case Instruction::IPUT:
3493 case Instruction::IPUT_BOOLEAN:
3494 case Instruction::IPUT_BYTE:
3495 case Instruction::IPUT_CHAR:
3496 case Instruction::IPUT_SHORT:
3497 case Instruction::IPUT_WIDE:
3498 case Instruction::IPUT_OBJECT:
3499 case Instruction::SGET:
3500 case Instruction::SGET_BOOLEAN:
3501 case Instruction::SGET_BYTE:
3502 case Instruction::SGET_CHAR:
3503 case Instruction::SGET_SHORT:
3504 case Instruction::SGET_WIDE:
3505 case Instruction::SGET_OBJECT:
3506 case Instruction::SPUT:
3507 case Instruction::SPUT_BOOLEAN:
3508 case Instruction::SPUT_BYTE:
3509 case Instruction::SPUT_CHAR:
3510 case Instruction::SPUT_SHORT:
3511 case Instruction::SPUT_WIDE:
3512 case Instruction::SPUT_OBJECT:
3513 ref_type = VERIFY_ERROR_REF_FIELD;
3514 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003516 case Instruction::INVOKE_VIRTUAL_RANGE:
3517 case Instruction::INVOKE_SUPER:
3518 case Instruction::INVOKE_SUPER_RANGE:
3519 case Instruction::INVOKE_DIRECT:
3520 case Instruction::INVOKE_DIRECT_RANGE:
3521 case Instruction::INVOKE_STATIC:
3522 case Instruction::INVOKE_STATIC_RANGE:
3523 case Instruction::INVOKE_INTERFACE:
3524 case Instruction::INVOKE_INTERFACE_RANGE:
3525 ref_type = VERIFY_ERROR_REF_METHOD;
3526 break;
jeffhaobdb76512011-09-07 11:43:16 -07003527 default:
Ian Rogersd81871c2011-10-03 13:57:23 -07003528 LOG(FATAL) << "Error: verifier asked to replace instruction " << *inst;
jeffhaobdb76512011-09-07 11:43:16 -07003529 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3532 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3533 // instruction, so assert it.
3534 size_t width = inst->SizeInCodeUnits();
3535 CHECK_GT(width, 1u);
3536 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3537 // NOPs
3538 for (size_t i = 2; i < width; i++) {
3539 insns[work_insn_idx_ + i] = Instruction::NOP;
3540 }
3541 // Encode the opcode, with the failure code in the high byte
3542 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3543 (failure_ << 8) | // AA - component
3544 (ref_type << (8 + kVerifyErrorRefTypeShift));
3545 insns[work_insn_idx_] = new_instruction;
3546 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3547 // rewrote, so nothing to do here.
jeffhaobdb76512011-09-07 11:43:16 -07003548}
jeffhaoba5ebb92011-08-25 17:24:37 -07003549
Ian Rogersd81871c2011-10-03 13:57:23 -07003550bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3551 const bool merge_debug = true;
3552 bool changed = true;
3553 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3554 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003555 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 * We haven't processed this instruction before, and we haven't touched the registers here, so
3557 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3558 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003559 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003560 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003561 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3563 copy->CopyFromLine(target_line);
3564 changed = target_line->MergeRegisters(merge_line);
3565 if (failure_ != VERIFY_ERROR_NONE) {
3566 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003567 }
jeffhaobdb76512011-09-07 11:43:16 -07003568 if (changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3570 << *copy.get() << " MERGE" << std::endl
3571 << *merge_line << " ==" << std::endl
3572 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003573 }
3574 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 if (changed) {
3576 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003577 }
3578 return true;
3579}
3580
Ian Rogersd81871c2011-10-03 13:57:23 -07003581void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3582 size_t* log2_max_gc_pc) {
3583 size_t local_gc_points = 0;
3584 size_t max_insn = 0;
3585 size_t max_ref_reg = -1;
3586 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3587 if (insn_flags_[i].IsGcPoint()) {
3588 local_gc_points++;
3589 max_insn = i;
3590 RegisterLine* line = reg_table_.GetLine(i);
3591 max_ref_reg = line->GetMaxReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003592 }
3593 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 *gc_points = local_gc_points;
3595 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3596 size_t i = 0;
3597 while ((1U << i) < max_insn) {
3598 i++;
3599 }
3600 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003601}
3602
Ian Rogersd81871c2011-10-03 13:57:23 -07003603ByteArray* DexVerifier::GenerateGcMap() {
3604 size_t num_entries, ref_bitmap_bits, pc_bits;
3605 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3606 // There's a single byte to encode the size of each bitmap
3607 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3608 // TODO: either a better GC map format or per method failures
3609 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3610 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003611 return NULL;
3612 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3614 // There are 2 bytes to encode the number of entries
3615 if (num_entries >= 65536) {
3616 // TODO: either a better GC map format or per method failures
3617 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3618 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003619 return NULL;
3620 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003622 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003624 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 pc_bytes = 1;
3626 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003627 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003629 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 // TODO: either a better GC map format or per method failures
3631 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3632 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3633 return NULL;
3634 }
3635 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3636 ByteArray* table = ByteArray::Alloc(table_size);
3637 if (table == NULL) {
3638 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3639 return NULL;
3640 }
3641 // Write table header
3642 table->Set(0, format);
3643 table->Set(1, ref_bitmap_bytes);
3644 table->Set(2, num_entries & 0xFF);
3645 table->Set(3, (num_entries >> 8) & 0xFF);
3646 // Write table data
3647 size_t offset = 4;
3648 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3649 if (insn_flags_[i].IsGcPoint()) {
3650 table->Set(offset, i & 0xFF);
3651 offset++;
3652 if (pc_bytes == 2) {
3653 table->Set(offset, (i >> 8) & 0xFF);
3654 offset++;
3655 }
3656 RegisterLine* line = reg_table_.GetLine(i);
3657 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3658 offset += ref_bitmap_bytes;
3659 }
3660 }
3661 DCHECK(offset == table_size);
3662 return table;
3663}
jeffhaoa0a764a2011-09-16 10:43:38 -07003664
Ian Rogersd81871c2011-10-03 13:57:23 -07003665void DexVerifier::VerifyGcMap() {
3666 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3667 // that the table data is well formed and all references are marked (or not) in the bitmap
3668 PcToReferenceMap map(method_);
3669 size_t map_index = 0;
3670 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3671 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3672 if (insn_flags_[i].IsGcPoint()) {
3673 CHECK_LT(map_index, map.NumEntries());
3674 CHECK_EQ(map.GetPC(map_index), i);
3675 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3676 map_index++;
3677 RegisterLine* line = reg_table_.GetLine(i);
3678 for(size_t j = 0; j < code_item_->registers_size_; j++) {
3679 if (line->GetRegisterType(j).IsReference()) {
3680 CHECK_LT(j / 8, map.RegWidth());
3681 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3682 } else if ((j / 8) < map.RegWidth()) {
3683 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3684 } else {
3685 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3686 }
3687 }
3688 } else {
3689 CHECK(reg_bitmap == NULL);
3690 }
3691 }
3692}
jeffhaoa0a764a2011-09-16 10:43:38 -07003693
Ian Rogersd81871c2011-10-03 13:57:23 -07003694Class* DexVerifier::JavaLangThrowable() {
3695 if (java_lang_throwable_ == NULL) {
3696 java_lang_throwable_ =
3697 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3698 DCHECK(java_lang_throwable_ != NULL);
3699 }
3700 return java_lang_throwable_;
3701}
3702
3703const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3704 size_t num_entries = NumEntries();
3705 // Do linear or binary search?
3706 static const size_t kSearchThreshold = 8;
3707 if (num_entries < kSearchThreshold) {
3708 for (size_t i = 0; i < num_entries; i++) {
3709 if (GetPC(i) == dex_pc) {
3710 return GetBitMap(i);
3711 }
3712 }
3713 } else {
3714 int lo = 0;
3715 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003716 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003717 int mid = (hi + lo) / 2;
3718 int mid_pc = GetPC(mid);
3719 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003720 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003721 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003722 hi = mid - 1;
3723 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003725 }
3726 }
3727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 if (error_if_not_present) {
3729 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3730 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003731 return NULL;
3732}
3733
Ian Rogersd81871c2011-10-03 13:57:23 -07003734} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003735} // namespace art