blob: 5a4405587b8b49151239076c7db6245bd00225d1 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
Elliott Hughes1f359b02011-07-17 14:27:17 -07005#include <iostream>
6
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_linker.h"
jeffhaob4df5142011-09-19 20:25:32 -07008#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
10#include "dex_instruction.h"
11#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070012#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070013#include "intern_table.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070014#include "logging.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070016#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017
18namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070019namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
Ian Rogers2c8a8572011-10-24 17:11:36 -070021static const bool gDebugVerify = false;
22
Ian Rogersd81871c2011-10-03 13:57:23 -070023std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
24 return os << (int)rhs;
25}
jeffhaobdb76512011-09-07 11:43:16 -070026
Ian Rogers84fa0742011-10-25 18:13:30 -070027static const char* type_strings[] = {
28 "Unknown",
29 "Conflict",
30 "Boolean",
31 "Byte",
32 "Short",
33 "Char",
34 "Integer",
35 "Float",
36 "Long (Low Half)",
37 "Long (High Half)",
38 "Double (Low Half)",
39 "Double (High Half)",
40 "64-bit Constant (Low Half)",
41 "64-bit Constant (High Half)",
42 "32-bit Constant",
43 "Unresolved Reference",
44 "Uninitialized Reference",
45 "Uninitialized This Reference",
46 "Unresolved And Uninitialized This Reference",
47 "Reference",
48};
Ian Rogersd81871c2011-10-03 13:57:23 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070051 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
52 std::string result;
53 if (IsConstant()) {
54 uint32_t val = ConstantValue();
55 if (val == 0) {
56 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070057 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070058 if(IsConstantShort()) {
59 result = StringPrintf("32-bit Constant: %d", val);
60 } else {
61 result = StringPrintf("32-bit Constant: 0x%x", val);
62 }
63 }
64 } else {
65 result = type_strings[type_];
66 if (IsReferenceTypes()) {
67 result += ": ";
68 if (IsUnresolvedReference()) {
69 result += PrettyDescriptor(GetDescriptor());
70 } else {
71 result += PrettyDescriptor(GetClass()->GetDescriptor());
72 }
Ian Rogersd81871c2011-10-03 13:57:23 -070073 }
74 }
Ian Rogers84fa0742011-10-25 18:13:30 -070075 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070076}
77
78const RegType& RegType::HighHalf(RegTypeCache* cache) const {
79 CHECK(IsLowHalf());
80 if (type_ == kRegTypeLongLo) {
81 return cache->FromType(kRegTypeLongHi);
82 } else if (type_ == kRegTypeDoubleLo) {
83 return cache->FromType(kRegTypeDoubleHi);
84 } else {
85 return cache->FromType(kRegTypeConstHi);
86 }
87}
88
89/*
90 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
91 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
92 * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J
93 * is the deepest (lowest upper bound) parent of S and T).
94 *
95 * This operation applies for regular classes and arrays, however, for interface types there needn't
96 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
97 * introducing sets of types, however, the only operation permissible on an interface is
98 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
99 * types until an invoke-interface call on the interface typed reference at runtime and allow
100 * the perversion of Object being assignable to an interface type (note, however, that we don't
101 * allow assignment of Object or Interface to any concrete class and are therefore type safe;
102 * further the Join on a Object cannot result in a sub-class by definition).
103 */
104Class* RegType::ClassJoin(Class* s, Class* t) {
105 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
106 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
107 if (s == t) {
108 return s;
109 } else if (s->IsAssignableFrom(t)) {
110 return s;
111 } else if (t->IsAssignableFrom(s)) {
112 return t;
113 } else if (s->IsArrayClass() && t->IsArrayClass()) {
114 Class* s_ct = s->GetComponentType();
115 Class* t_ct = t->GetComponentType();
116 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
117 // Given the types aren't the same, if either array is of primitive types then the only
118 // common parent is java.lang.Object
119 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
120 DCHECK(result->IsObjectClass());
121 return result;
122 }
123 Class* common_elem = ClassJoin(s_ct, t_ct);
124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
125 const ClassLoader* class_loader = s->GetClassLoader();
126 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
127 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
128 DCHECK(array_class != NULL);
129 return array_class;
130 } else {
131 size_t s_depth = s->Depth();
132 size_t t_depth = t->Depth();
133 // Get s and t to the same depth in the hierarchy
134 if (s_depth > t_depth) {
135 while (s_depth > t_depth) {
136 s = s->GetSuperClass();
137 s_depth--;
138 }
139 } else {
140 while (t_depth > s_depth) {
141 t = t->GetSuperClass();
142 t_depth--;
143 }
144 }
145 // Go up the hierarchy until we get to the common parent
146 while (s != t) {
147 s = s->GetSuperClass();
148 t = t->GetSuperClass();
149 }
150 return s;
151 }
152}
153
154const RegType& RegType::VerifyAgainst(const RegType& check_type, RegTypeCache* reg_types) const {
155 if (Equals(check_type)) {
156 return *this;
157 } else {
158 switch (check_type.GetType()) {
159 case RegType::kRegTypeBoolean:
160 return IsBooleanTypes() ? check_type : reg_types->Conflict();
161 break;
162 case RegType::kRegTypeByte:
163 return IsByteTypes() ? check_type : reg_types->Conflict();
164 break;
165 case RegType::kRegTypeShort:
166 return IsShortTypes() ? check_type : reg_types->Conflict();
167 break;
168 case RegType::kRegTypeChar:
169 return IsCharTypes() ? check_type : reg_types->Conflict();
170 break;
171 case RegType::kRegTypeInteger:
172 return IsIntegralTypes() ? check_type : reg_types->Conflict();
173 break;
174 case RegType::kRegTypeFloat:
175 return IsFloatTypes() ? check_type : reg_types->Conflict();
176 break;
177 case RegType::kRegTypeLongLo:
178 return IsLongTypes() ? check_type : reg_types->Conflict();
179 break;
180 case RegType::kRegTypeDoubleLo:
181 return IsDoubleTypes() ? check_type : reg_types->Conflict();
182 break;
Ian Rogers84fa0742011-10-25 18:13:30 -0700183 default:
184 if (!check_type.IsReferenceTypes()) {
185 LOG(FATAL) << "Unexpected register type verification against " << check_type;
186 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 if (IsZero()) {
188 return check_type;
189 } else if (IsUninitializedReference()) {
190 return reg_types->Conflict(); // Nonsensical to Join two uninitialized classes
Ian Rogers84fa0742011-10-25 18:13:30 -0700191 } else if (IsReference() && check_type.IsReference() &&
192 (check_type.GetClass()->IsAssignableFrom(GetClass()) ||
193 (GetClass()->IsObjectClass() && check_type.GetClass()->IsInterface()))) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700194 // Either we're assignable or this is trying to assign Object to an Interface, which
Ian Rogers84fa0742011-10-25 18:13:30 -0700195 // is allowed (see comment for ClassJoin)
196 return check_type;
197 } else if (IsUnresolvedReference() && check_type.IsReference() &&
198 check_type.GetClass()->IsObjectClass()) {
199 // We're an unresolved reference being checked to see if we're an Object, which is ok
200 return *this;
201 } else if (check_type.IsUnresolvedReference() && IsReference() &&
202 GetClass()->IsObjectClass()) {
203 // As with the last case but with the types reversed
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 return check_type;
205 } else {
206 return reg_types->Conflict();
207 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700208 }
209 }
210}
211
Ian Rogers84fa0742011-10-25 18:13:30 -0700212static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
213 return a.IsConstant() ? b : a;
214}
jeffhaobdb76512011-09-07 11:43:16 -0700215
Ian Rogersd81871c2011-10-03 13:57:23 -0700216const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
217 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700218 if (IsUnknown() && incoming_type.IsUnknown()) {
219 return *this; // Unknown MERGE Unknown => Unknown
220 } else if (IsConflict()) {
221 return *this; // Conflict MERGE * => Conflict
222 } else if (incoming_type.IsConflict()) {
223 return incoming_type; // * MERGE Conflict => Conflict
224 } else if (IsUnknown() || incoming_type.IsUnknown()) {
225 return reg_types->Conflict(); // Unknown MERGE * => Conflict
226 } else if(IsConstant() && incoming_type.IsConstant()) {
227 int32_t val1 = ConstantValue();
228 int32_t val2 = incoming_type.ConstantValue();
229 if (val1 >= 0 && val2 >= 0) {
230 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
231 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700232 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700233 } else {
234 return incoming_type;
235 }
236 } else if (val1 < 0 && val2 < 0) {
237 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
238 if (val1 <= val2) {
239 return *this;
240 } else {
241 return incoming_type;
242 }
243 } else {
244 // Values are +ve and -ve, choose smallest signed type in which they both fit
245 if (IsConstantByte()) {
246 if (incoming_type.IsConstantByte()) {
247 return reg_types->ByteConstant();
248 } else if (incoming_type.IsConstantShort()) {
249 return reg_types->ShortConstant();
250 } else {
251 return reg_types->IntConstant();
252 }
253 } else if (IsConstantShort()) {
254 if (incoming_type.IsShort()) {
255 return reg_types->ShortConstant();
256 } else {
257 return reg_types->IntConstant();
258 }
259 } else {
260 return reg_types->IntConstant();
261 }
262 }
263 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
264 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
265 return reg_types->Boolean(); // boolean MERGE boolean => boolean
266 }
267 if (IsByteTypes() && incoming_type.IsByteTypes()) {
268 return reg_types->Byte(); // byte MERGE byte => byte
269 }
270 if (IsShortTypes() && incoming_type.IsShortTypes()) {
271 return reg_types->Short(); // short MERGE short => short
272 }
273 if (IsCharTypes() && incoming_type.IsCharTypes()) {
274 return reg_types->Char(); // char MERGE char => char
275 }
276 return reg_types->Integer(); // int MERGE * => int
277 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
278 (IsLongTypes() && incoming_type.IsLongTypes()) ||
279 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
280 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
281 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
282 // check constant case was handled prior to entry
283 DCHECK(!IsConstant() || !incoming_type.IsConstant());
284 // float/long/double MERGE float/long/double_constant => float/long/double
285 return SelectNonConstant(*this, incoming_type);
286 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
287 if (IsZero() | incoming_type.IsZero()) {
288 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
289 } else if (IsUnresolvedReference() || IsUninitializedReference() ||
290 IsUninitializedThisReference()) {
291 // Can only merge an uninitialized or unresolved type with itself or 0, we've already checked
292 // these so => Conflict
293 return reg_types->Conflict();
294 } else { // Two reference types, compute Join
295 Class* c1 = GetClass();
296 Class* c2 = incoming_type.GetClass();
297 DCHECK(c1 != NULL && !c1->IsPrimitive());
298 DCHECK(c2 != NULL && !c2->IsPrimitive());
299 Class* join_class = ClassJoin(c1, c2);
300 if (c1 == join_class) {
301 return *this;
302 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700303 return incoming_type;
304 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700305 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700306 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700307 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700308 } else {
309 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 }
311}
312
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700313static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700315 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
316 case Primitive::kPrimByte: return RegType::kRegTypeByte;
317 case Primitive::kPrimShort: return RegType::kRegTypeShort;
318 case Primitive::kPrimChar: return RegType::kRegTypeChar;
319 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
320 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
321 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
322 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
323 case Primitive::kPrimVoid:
324 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 }
326}
327
328static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
329 if (descriptor.length() == 1) {
330 switch (descriptor[0]) {
331 case 'Z': return RegType::kRegTypeBoolean;
332 case 'B': return RegType::kRegTypeByte;
333 case 'S': return RegType::kRegTypeShort;
334 case 'C': return RegType::kRegTypeChar;
335 case 'I': return RegType::kRegTypeInteger;
336 case 'J': return RegType::kRegTypeLongLo;
337 case 'F': return RegType::kRegTypeFloat;
338 case 'D': return RegType::kRegTypeDoubleLo;
339 case 'V':
340 default: return RegType::kRegTypeUnknown;
341 }
342 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
343 return RegType::kRegTypeReference;
344 } else {
345 return RegType::kRegTypeUnknown;
346 }
347}
348
349std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700350 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 return os;
352}
353
354const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
355 const std::string& descriptor) {
356 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
357}
358
359const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
360 const std::string& descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700361 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 // entries should be sized greater than primitive types
363 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
364 RegType* entry = entries_[type];
365 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700366 Class* klass = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -0700367 if (descriptor.size() != 0) {
368 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
369 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700370 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 entries_[type] = entry;
372 }
373 return *entry;
374 } else {
375 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers84fa0742011-10-25 18:13:30 -0700376 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700378 // check resolved and unresolved references, ignore uninitialized references
379 if (cur_entry->IsReference() && cur_entry->GetClass()->GetDescriptor()->Equals(descriptor)) {
380 return *cur_entry;
381 } else if (cur_entry->IsUnresolvedReference() &&
382 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 return *cur_entry;
384 }
385 }
386 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700387 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700388 // Able to resolve so create resolved register type
389 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700390 entries_.push_back(entry);
391 return *entry;
392 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700393 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700394 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700395 Thread::Current()->ClearException();
396 String* string_descriptor =
397 Runtime::Current()->GetInternTable()->InternStrong(descriptor.c_str());
398 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
399 entries_.size());
400 entries_.push_back(entry);
401 return *entry;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700402 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700403 }
404}
405
406const RegType& RegTypeCache::FromClass(Class* klass) {
407 if (klass->IsPrimitive()) {
408 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
409 // entries should be sized greater than primitive types
410 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
411 RegType* entry = entries_[type];
412 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700413 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700414 entries_[type] = entry;
415 }
416 return *entry;
417 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700418 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700419 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700420 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 return *cur_entry;
422 }
423 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700424 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 entries_.push_back(entry);
426 return *entry;
427 }
428}
429
430const RegType& RegTypeCache::Uninitialized(Class* klass, uint32_t allocation_pc) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700431 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700432 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700433 if (cur_entry->IsUninitializedReference() && cur_entry->GetAllocationPc() == allocation_pc &&
434 cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 return *cur_entry;
436 }
437 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700438 RegType* entry = new RegType(RegType::kRegTypeUninitializedReference, klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 entries_.push_back(entry);
440 return *entry;
441}
442
443const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700444 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700445 RegType* cur_entry = entries_[i];
446 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
447 return *cur_entry;
448 }
449 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700450 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 entries_.size());
452 entries_.push_back(entry);
453 return *entry;
454}
455
456const RegType& RegTypeCache::FromType(RegType::Type type) {
457 CHECK(type < RegType::kRegTypeReference);
458 switch (type) {
459 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
460 case RegType::kRegTypeByte: return From(type, NULL, "B");
461 case RegType::kRegTypeShort: return From(type, NULL, "S");
462 case RegType::kRegTypeChar: return From(type, NULL, "C");
463 case RegType::kRegTypeInteger: return From(type, NULL, "I");
464 case RegType::kRegTypeFloat: return From(type, NULL, "F");
465 case RegType::kRegTypeLongLo:
466 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
467 case RegType::kRegTypeDoubleLo:
468 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
469 default: return From(type, NULL, "");
470 }
471}
472
473const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700474 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
475 RegType* cur_entry = entries_[i];
476 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
477 return *cur_entry;
478 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700480 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
481 entries_.push_back(entry);
482 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700483}
484
485bool RegisterLine::CheckConstructorReturn() const {
486 for (size_t i = 0; i < num_regs_; i++) {
487 if (GetRegisterType(i).IsUninitializedThisReference()) {
488 verifier_->Fail(VERIFY_ERROR_GENERIC)
489 << "Constructor returning without calling superclass constructor";
490 return false;
491 }
492 }
493 return true;
494}
495
496void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
497 DCHECK(vdst < num_regs_);
498 if (new_type.IsLowHalf()) {
499 line_[vdst] = new_type.GetId();
500 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
501 } else if (new_type.IsHighHalf()) {
502 /* should never set these explicitly */
503 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
504 } else if (new_type.IsConflict()) { // should only be set during a merge
505 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
506 } else {
507 line_[vdst] = new_type.GetId();
508 }
509 // Clear the monitor entry bits for this register.
510 ClearAllRegToLockDepths(vdst);
511}
512
513void RegisterLine::SetResultTypeToUnknown() {
514 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
515 result_[0] = unknown_id;
516 result_[1] = unknown_id;
517}
518
519void RegisterLine::SetResultRegisterType(const RegType& new_type) {
520 result_[0] = new_type.GetId();
521 if(new_type.IsLowHalf()) {
522 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
523 result_[1] = new_type.GetId() + 1;
524 } else {
525 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
526 }
527}
528
529const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
530 // The register index was validated during the static pass, so we don't need to check it here.
531 DCHECK_LT(vsrc, num_regs_);
532 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
533}
534
535const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
536 if (dec_insn.vA_ < 1) {
537 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
538 return verifier_->GetRegTypeCache()->Unknown();
539 }
540 /* get the element type of the array held in vsrc */
541 const RegType& this_type = GetRegisterType(dec_insn.vC_);
542 if (!this_type.IsReferenceTypes()) {
543 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
544 << dec_insn.vC_ << " (type=" << this_type << ")";
545 return verifier_->GetRegTypeCache()->Unknown();
546 }
547 return this_type;
548}
549
550Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
551 /* get the element type of the array held in vsrc */
552 const RegType& type = GetRegisterType(vsrc);
553 /* if "always zero", we allow it to fail at runtime */
554 if (type.IsZero()) {
555 return NULL;
556 } else if (!type.IsReferenceTypes()) {
557 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
558 << " (type=" << type << ")";
559 return NULL;
560 } else if (type.IsUninitializedReference()) {
561 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
562 return NULL;
563 } else {
564 return type.GetClass();
565 }
566}
567
568bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
569 // Verify the src register type against the check type refining the type of the register
570 const RegType& src_type = GetRegisterType(vsrc);
571 const RegType& lub_type = src_type.VerifyAgainst(check_type, verifier_->GetRegTypeCache());
572 if (lub_type.IsConflict()) {
573 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
574 << " but expected " << check_type;
575 return false;
576 }
577 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
578 // precise than the subtype in vsrc so leave it for reference types. For primitive types
579 // if they are a defined type then they are as precise as we can get, however, for constant
580 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
581 return true;
582}
583
584void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
585 Class* klass = uninit_type.GetClass();
586 if (klass == NULL) {
587 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Unable to find type=" << uninit_type;
588 } else {
589 const RegType& init_type = verifier_->GetRegTypeCache()->FromClass(klass);
590 size_t changed = 0;
591 for (size_t i = 0; i < num_regs_; i++) {
592 if (GetRegisterType(i).Equals(uninit_type)) {
593 line_[i] = init_type.GetId();
594 changed++;
595 }
596 }
597 DCHECK_GT(changed, 0u);
598 }
599}
600
601void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
602 for (size_t i = 0; i < num_regs_; i++) {
603 if (GetRegisterType(i).Equals(uninit_type)) {
604 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
605 ClearAllRegToLockDepths(i);
606 }
607 }
608}
609
610void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
611 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
612 const RegType& type = GetRegisterType(vsrc);
613 SetRegisterType(vdst, type);
614 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
615 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
616 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
617 << " cat=" << static_cast<int>(cat);
618 } else if (cat == kTypeCategoryRef) {
619 CopyRegToLockDepth(vdst, vsrc);
620 }
621}
622
623void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
624 const RegType& type_l = GetRegisterType(vsrc);
625 const RegType& type_h = GetRegisterType(vsrc + 1);
626
627 if (!type_l.CheckWidePair(type_h)) {
628 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
629 << " type=" << type_l << "/" << type_h;
630 } else {
631 SetRegisterType(vdst, type_l); // implicitly sets the second half
632 }
633}
634
635void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
636 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
637 if ((!is_reference && !type.IsCategory1Types()) ||
638 (is_reference && !type.IsReferenceTypes())) {
639 verifier_->Fail(VERIFY_ERROR_GENERIC)
640 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
641 } else {
642 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
643 SetRegisterType(vdst, type);
644 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
645 }
646}
647
648/*
649 * Implement "move-result-wide". Copy the category-2 value from the result
650 * register to another register, and reset the result register.
651 */
652void RegisterLine::CopyResultRegister2(uint32_t vdst) {
653 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
654 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
655 if (!type_l.IsCategory2Types()) {
656 verifier_->Fail(VERIFY_ERROR_GENERIC)
657 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
658 } else {
659 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
660 SetRegisterType(vdst, type_l); // also sets the high
661 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
662 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
663 }
664}
665
666void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
667 const RegType& dst_type, const RegType& src_type) {
668 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
669 SetRegisterType(dec_insn.vA_, dst_type);
670 }
671}
672
673void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
674 const RegType& dst_type,
675 const RegType& src_type1, const RegType& src_type2,
676 bool check_boolean_op) {
677 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
678 VerifyRegisterType(dec_insn.vC_, src_type2)) {
679 if (check_boolean_op) {
680 DCHECK(dst_type.IsInteger());
681 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
682 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
683 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
684 return;
685 }
686 }
687 SetRegisterType(dec_insn.vA_, dst_type);
688 }
689}
690
691void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
692 const RegType& dst_type, const RegType& src_type1,
693 const RegType& src_type2, bool check_boolean_op) {
694 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
695 VerifyRegisterType(dec_insn.vB_, src_type2)) {
696 if (check_boolean_op) {
697 DCHECK(dst_type.IsInteger());
698 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
699 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
700 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
701 return;
702 }
703 }
704 SetRegisterType(dec_insn.vA_, dst_type);
705 }
706}
707
708void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
709 const RegType& dst_type, const RegType& src_type,
710 bool check_boolean_op) {
711 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
712 if (check_boolean_op) {
713 DCHECK(dst_type.IsInteger());
714 /* check vB with the call, then check the constant manually */
715 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
716 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
717 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
718 return;
719 }
720 }
721 SetRegisterType(dec_insn.vA_, dst_type);
722 }
723}
724
725void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
726 const RegType& reg_type = GetRegisterType(reg_idx);
727 if (!reg_type.IsReferenceTypes()) {
728 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
729 } else {
730 SetRegToLockDepth(reg_idx, monitors_.size());
731 monitors_.push(insn_idx);
732 }
733}
734
735void RegisterLine::PopMonitor(uint32_t reg_idx) {
736 const RegType& reg_type = GetRegisterType(reg_idx);
737 if (!reg_type.IsReferenceTypes()) {
738 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
739 } else if (monitors_.empty()) {
740 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
741 } else {
742 monitors_.pop();
743 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
744 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
745 // format "036" the constant collector may create unlocks on the same object but referenced
746 // via different registers.
747 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
748 : verifier_->LogVerifyInfo())
749 << "monitor-exit not unlocking the top of the monitor stack";
750 } else {
751 // Record the register was unlocked
752 ClearRegToLockDepth(reg_idx, monitors_.size());
753 }
754 }
755}
756
757bool RegisterLine::VerifyMonitorStackEmpty() {
758 if (MonitorStackDepth() != 0) {
759 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
760 return false;
761 } else {
762 return true;
763 }
764}
765
766bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
767 bool changed = false;
768 for (size_t idx = 0; idx < num_regs_; idx++) {
769 if (line_[idx] != incoming_line->line_[idx]) {
770 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
771 const RegType& cur_type = GetRegisterType(idx);
772 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
773 changed = changed || !cur_type.Equals(new_type);
774 line_[idx] = new_type.GetId();
775 }
776 }
777 if(monitors_ != incoming_line->monitors_) {
778 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
779 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
780 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
781 for (uint32_t idx = 0; idx < num_regs_; idx++) {
782 size_t depths = reg_to_lock_depths_.count(idx);
783 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
784 if (depths != incoming_depths) {
785 if (depths == 0 || incoming_depths == 0) {
786 reg_to_lock_depths_.erase(idx);
787 } else {
788 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
789 << ": " << depths << " != " << incoming_depths;
790 break;
791 }
792 }
793 }
794 }
795 return changed;
796}
797
798void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
799 for (size_t i = 0; i < num_regs_; i += 8) {
800 uint8_t val = 0;
801 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
802 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700803 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 val |= 1 << j;
805 }
806 }
807 if (val != 0) {
808 DCHECK_LT(i / 8, max_bytes);
809 data[i / 8] = val;
810 }
811 }
812}
813
814std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700815 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return os;
817}
818
819
820void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
821 uint32_t insns_size, uint16_t registers_size,
822 DexVerifier* verifier) {
823 DCHECK_GT(insns_size, 0U);
824
825 for (uint32_t i = 0; i < insns_size; i++) {
826 bool interesting = false;
827 switch (mode) {
828 case kTrackRegsAll:
829 interesting = flags[i].IsOpcode();
830 break;
831 case kTrackRegsGcPoints:
832 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
833 break;
834 case kTrackRegsBranches:
835 interesting = flags[i].IsBranchTarget();
836 break;
837 default:
838 break;
839 }
840 if (interesting) {
841 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
842 }
843 }
844}
845
846bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700847 if (klass->IsVerified()) {
848 return true;
849 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 Class* super = klass->GetSuperClass();
851 if (super == NULL && !klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
852 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
853 return false;
854 }
855 if (super != NULL) {
856 if (!super->IsVerified() && !super->IsErroneous()) {
857 Runtime::Current()->GetClassLinker()->VerifyClass(super);
858 }
859 if (!super->IsVerified()) {
860 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
861 << " that attempts to sub-class corrupt class " << PrettyClass(super);
862 return false;
863 } else if (super->IsFinal()) {
864 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
865 << " that attempts to sub-class final class " << PrettyClass(super);
866 return false;
867 }
868 }
jeffhaobdb76512011-09-07 11:43:16 -0700869 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
870 Method* method = klass->GetDirectMethod(i);
871 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
873 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700874 return false;
875 }
876 }
877 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
878 Method* method = klass->GetVirtualMethod(i);
879 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
881 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700882 return false;
883 }
884 }
885 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700886}
887
jeffhaobdb76512011-09-07 11:43:16 -0700888bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 DexVerifier verifier(method);
890 bool success = verifier.Verify();
891 // We expect either success and no verification error, or failure and a generic failure to
892 // reject the class.
893 if (success) {
894 if (verifier.failure_ != VERIFY_ERROR_NONE) {
895 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
896 << verifier.fail_messages_;
897 }
898 } else {
899 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
900 << verifier.fail_messages_.str() << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700901 if (gDebugVerify) {
902 verifier.Dump(std::cout);
903 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
905 }
906 return success;
907}
908
909DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
910 method_(method), failure_(VERIFY_ERROR_NONE),
911 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700912 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
913 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 dex_file_ = &class_linker->FindDexFile(dex_cache);
915 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700916}
917
Ian Rogersd81871c2011-10-03 13:57:23 -0700918bool DexVerifier::Verify() {
919 // If there aren't any instructions, make sure that's expected, then exit successfully.
920 if (code_item_ == NULL) {
921 if (!method_->IsNative() && !method_->IsAbstract()) {
922 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700923 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 } else {
925 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700926 }
jeffhaobdb76512011-09-07 11:43:16 -0700927 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
929 if (code_item_->ins_size_ > code_item_->registers_size_) {
930 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
931 << " regs=" << code_item_->registers_size_;
932 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700933 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 // Allocate and initialize an array to hold instruction data.
935 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
936 // Run through the instructions and see if the width checks out.
937 bool result = ComputeWidthsAndCountOps();
938 // Flag instructions guarded by a "try" block and check exception handlers.
939 result = result && ScanTryCatchBlocks();
940 // Perform static instruction verification.
941 result = result && VerifyInstructions();
942 // Perform code flow analysis.
943 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700944 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700945}
946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947bool DexVerifier::ComputeWidthsAndCountOps() {
948 const uint16_t* insns = code_item_->insns_;
949 size_t insns_size = code_item_->insns_size_in_code_units_;
950 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700951 size_t new_instance_count = 0;
952 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700954
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700956 Instruction::Code opcode = inst->Opcode();
957 if (opcode == Instruction::NEW_INSTANCE) {
958 new_instance_count++;
959 } else if (opcode == Instruction::MONITOR_ENTER) {
960 monitor_enter_count++;
961 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 size_t inst_size = inst->SizeInCodeUnits();
963 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
964 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700965 inst = inst->Next();
966 }
967
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 if (dex_pc != insns_size) {
969 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
970 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700971 return false;
972 }
973
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 new_instance_count_ = new_instance_count;
975 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700976 return true;
977}
978
Ian Rogersd81871c2011-10-03 13:57:23 -0700979bool DexVerifier::ScanTryCatchBlocks() {
980 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700981 if (tries_size == 0) {
982 return true;
983 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 uint32_t insns_size = code_item_->insns_size_in_code_units_;
985 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700986
987 for (uint32_t idx = 0; idx < tries_size; idx++) {
988 const DexFile::TryItem* try_item = &tries[idx];
989 uint32_t start = try_item->start_addr_;
990 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700991 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
993 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700994 return false;
995 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 if (!insn_flags_[start].IsOpcode()) {
997 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700998 return false;
999 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 for (uint32_t dex_pc = start; dex_pc < end;
1001 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1002 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001003 }
1004 }
jeffhaobdb76512011-09-07 11:43:16 -07001005 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001007 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
1008 for (uint32_t idx = 0; idx < handlers_size; idx++) {
1009 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -07001010 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 uint32_t dex_pc= iterator.Get().address_;
1012 if (!insn_flags_[dex_pc].IsOpcode()) {
1013 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001014 return false;
1015 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -07001017 }
jeffhaobdb76512011-09-07 11:43:16 -07001018 handlers_ptr = iterator.GetData();
1019 }
jeffhaobdb76512011-09-07 11:43:16 -07001020 return true;
1021}
1022
Ian Rogersd81871c2011-10-03 13:57:23 -07001023bool DexVerifier::VerifyInstructions() {
1024 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001025
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 /* Flag the start of the method as a branch target. */
1027 insn_flags_[0].SetBranchTarget();
1028
1029 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1030 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1031 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001032 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1033 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 return false;
1035 }
1036 /* Flag instructions that are garbage collection points */
1037 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1038 insn_flags_[dex_pc].SetGcPoint();
1039 }
1040 dex_pc += inst->SizeInCodeUnits();
1041 inst = inst->Next();
1042 }
1043 return true;
1044}
1045
1046bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1047 Instruction::DecodedInstruction dec_insn(inst);
1048 bool result = true;
1049 switch (inst->GetVerifyTypeArgumentA()) {
1050 case Instruction::kVerifyRegA:
1051 result = result && CheckRegisterIndex(dec_insn.vA_);
1052 break;
1053 case Instruction::kVerifyRegAWide:
1054 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1055 break;
1056 }
1057 switch (inst->GetVerifyTypeArgumentB()) {
1058 case Instruction::kVerifyRegB:
1059 result = result && CheckRegisterIndex(dec_insn.vB_);
1060 break;
1061 case Instruction::kVerifyRegBField:
1062 result = result && CheckFieldIndex(dec_insn.vB_);
1063 break;
1064 case Instruction::kVerifyRegBMethod:
1065 result = result && CheckMethodIndex(dec_insn.vB_);
1066 break;
1067 case Instruction::kVerifyRegBNewInstance:
1068 result = result && CheckNewInstance(dec_insn.vB_);
1069 break;
1070 case Instruction::kVerifyRegBString:
1071 result = result && CheckStringIndex(dec_insn.vB_);
1072 break;
1073 case Instruction::kVerifyRegBType:
1074 result = result && CheckTypeIndex(dec_insn.vB_);
1075 break;
1076 case Instruction::kVerifyRegBWide:
1077 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1078 break;
1079 }
1080 switch (inst->GetVerifyTypeArgumentC()) {
1081 case Instruction::kVerifyRegC:
1082 result = result && CheckRegisterIndex(dec_insn.vC_);
1083 break;
1084 case Instruction::kVerifyRegCField:
1085 result = result && CheckFieldIndex(dec_insn.vC_);
1086 break;
1087 case Instruction::kVerifyRegCNewArray:
1088 result = result && CheckNewArray(dec_insn.vC_);
1089 break;
1090 case Instruction::kVerifyRegCType:
1091 result = result && CheckTypeIndex(dec_insn.vC_);
1092 break;
1093 case Instruction::kVerifyRegCWide:
1094 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1095 break;
1096 }
1097 switch (inst->GetVerifyExtraFlags()) {
1098 case Instruction::kVerifyArrayData:
1099 result = result && CheckArrayData(code_offset);
1100 break;
1101 case Instruction::kVerifyBranchTarget:
1102 result = result && CheckBranchTarget(code_offset);
1103 break;
1104 case Instruction::kVerifySwitchTargets:
1105 result = result && CheckSwitchTargets(code_offset);
1106 break;
1107 case Instruction::kVerifyVarArg:
1108 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1109 break;
1110 case Instruction::kVerifyVarArgRange:
1111 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1112 break;
1113 case Instruction::kVerifyError:
1114 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1115 result = false;
1116 break;
1117 }
1118 return result;
1119}
1120
1121bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1122 if (idx >= code_item_->registers_size_) {
1123 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1124 << code_item_->registers_size_ << ")";
1125 return false;
1126 }
1127 return true;
1128}
1129
1130bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1131 if (idx + 1 >= code_item_->registers_size_) {
1132 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1133 << "+1 >= " << code_item_->registers_size_ << ")";
1134 return false;
1135 }
1136 return true;
1137}
1138
1139bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1140 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1141 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1142 << dex_file_->GetHeader().field_ids_size_ << ")";
1143 return false;
1144 }
1145 return true;
1146}
1147
1148bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1149 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1150 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1151 << dex_file_->GetHeader().method_ids_size_ << ")";
1152 return false;
1153 }
1154 return true;
1155}
1156
1157bool DexVerifier::CheckNewInstance(uint32_t idx) {
1158 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1159 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1160 << dex_file_->GetHeader().type_ids_size_ << ")";
1161 return false;
1162 }
1163 // We don't need the actual class, just a pointer to the class name.
1164 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1165 if (descriptor[0] != 'L') {
1166 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1167 return false;
1168 }
1169 return true;
1170}
1171
1172bool DexVerifier::CheckStringIndex(uint32_t idx) {
1173 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1174 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1175 << dex_file_->GetHeader().string_ids_size_ << ")";
1176 return false;
1177 }
1178 return true;
1179}
1180
1181bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1182 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1183 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1184 << dex_file_->GetHeader().type_ids_size_ << ")";
1185 return false;
1186 }
1187 return true;
1188}
1189
1190bool DexVerifier::CheckNewArray(uint32_t idx) {
1191 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1192 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1193 << dex_file_->GetHeader().type_ids_size_ << ")";
1194 return false;
1195 }
1196 int bracket_count = 0;
1197 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1198 const char* cp = descriptor;
1199 while (*cp++ == '[') {
1200 bracket_count++;
1201 }
1202 if (bracket_count == 0) {
1203 /* The given class must be an array type. */
1204 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1205 return false;
1206 } else if (bracket_count > 255) {
1207 /* It is illegal to create an array of more than 255 dimensions. */
1208 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1209 return false;
1210 }
1211 return true;
1212}
1213
1214bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1215 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1216 const uint16_t* insns = code_item_->insns_ + cur_offset;
1217 const uint16_t* array_data;
1218 int32_t array_data_offset;
1219
1220 DCHECK_LT(cur_offset, insn_count);
1221 /* make sure the start of the array data table is in range */
1222 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1223 if ((int32_t) cur_offset + array_data_offset < 0 ||
1224 cur_offset + array_data_offset + 2 >= insn_count) {
1225 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1226 << ", data offset " << array_data_offset << ", count " << insn_count;
1227 return false;
1228 }
1229 /* offset to array data table is a relative branch-style offset */
1230 array_data = insns + array_data_offset;
1231 /* make sure the table is 32-bit aligned */
1232 if ((((uint32_t) array_data) & 0x03) != 0) {
1233 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1234 << ", data offset " << array_data_offset;
1235 return false;
1236 }
1237 uint32_t value_width = array_data[1];
1238 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1239 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1240 /* make sure the end of the switch is in range */
1241 if (cur_offset + array_data_offset + table_size > insn_count) {
1242 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1243 << ", data offset " << array_data_offset << ", end "
1244 << cur_offset + array_data_offset + table_size
1245 << ", count " << insn_count;
1246 return false;
1247 }
1248 return true;
1249}
1250
1251bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1252 int32_t offset;
1253 bool isConditional, selfOkay;
1254 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1255 return false;
1256 }
1257 if (!selfOkay && offset == 0) {
1258 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1259 return false;
1260 }
1261 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1262 // identical "wrap-around" behavior, but it's unwise to depend on that.
1263 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1264 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1265 return false;
1266 }
1267 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1268 int32_t abs_offset = cur_offset + offset;
1269 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1270 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1271 << (void*) abs_offset << ") at " << (void*) cur_offset;
1272 return false;
1273 }
1274 insn_flags_[abs_offset].SetBranchTarget();
1275 return true;
1276}
1277
1278bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1279 bool* selfOkay) {
1280 const uint16_t* insns = code_item_->insns_ + cur_offset;
1281 *pConditional = false;
1282 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001283 switch (*insns & 0xff) {
1284 case Instruction::GOTO:
1285 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001286 break;
1287 case Instruction::GOTO_32:
1288 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001289 *selfOkay = true;
1290 break;
1291 case Instruction::GOTO_16:
1292 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001293 break;
1294 case Instruction::IF_EQ:
1295 case Instruction::IF_NE:
1296 case Instruction::IF_LT:
1297 case Instruction::IF_GE:
1298 case Instruction::IF_GT:
1299 case Instruction::IF_LE:
1300 case Instruction::IF_EQZ:
1301 case Instruction::IF_NEZ:
1302 case Instruction::IF_LTZ:
1303 case Instruction::IF_GEZ:
1304 case Instruction::IF_GTZ:
1305 case Instruction::IF_LEZ:
1306 *pOffset = (int16_t) insns[1];
1307 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 break;
1309 default:
1310 return false;
1311 break;
1312 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001313 return true;
1314}
1315
Ian Rogersd81871c2011-10-03 13:57:23 -07001316bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1317 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001318 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1322 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1323 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1324 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 return false;
1326 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001327 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 /* make sure the table is 32-bit aligned */
1330 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1332 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 return false;
1334 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 uint32_t switch_count = switch_insns[1];
1336 int32_t keys_offset, targets_offset;
1337 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001338 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1339 /* 0=sig, 1=count, 2/3=firstKey */
1340 targets_offset = 4;
1341 keys_offset = -1;
1342 expected_signature = Instruction::kPackedSwitchSignature;
1343 } else {
1344 /* 0=sig, 1=count, 2..count*2 = keys */
1345 keys_offset = 2;
1346 targets_offset = 2 + 2 * switch_count;
1347 expected_signature = Instruction::kSparseSwitchSignature;
1348 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001350 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1352 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 return false;
1354 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 /* make sure the end of the switch is in range */
1356 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1358 << switch_offset << ", end "
1359 << (cur_offset + switch_offset + table_size)
1360 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001361 return false;
1362 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 /* for a sparse switch, verify the keys are in ascending order */
1364 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1366 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1368 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1369 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1371 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001372 return false;
1373 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 last_key = key;
1375 }
1376 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001377 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 for (uint32_t targ = 0; targ < switch_count; targ++) {
1379 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1380 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1381 int32_t abs_offset = cur_offset + offset;
1382 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1383 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1384 << (void*) abs_offset << ") at "
1385 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001386 return false;
1387 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 insn_flags_[abs_offset].SetBranchTarget();
1389 }
1390 return true;
1391}
1392
1393bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1394 if (vA > 5) {
1395 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1396 return false;
1397 }
1398 uint16_t registers_size = code_item_->registers_size_;
1399 for (uint32_t idx = 0; idx < vA; idx++) {
1400 if (arg[idx] > registers_size) {
1401 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1402 << ") in non-range invoke (> " << registers_size << ")";
1403 return false;
1404 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001405 }
1406
1407 return true;
1408}
1409
Ian Rogersd81871c2011-10-03 13:57:23 -07001410bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1411 uint16_t registers_size = code_item_->registers_size_;
1412 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1413 // integer overflow when adding them here.
1414 if (vA + vC > registers_size) {
1415 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1416 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 return false;
1418 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001419 return true;
1420}
1421
Ian Rogersd81871c2011-10-03 13:57:23 -07001422bool DexVerifier::VerifyCodeFlow() {
1423 uint16_t registers_size = code_item_->registers_size_;
1424 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001425
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 if (registers_size * insns_size > 4*1024*1024) {
1427 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1428 << " insns_size=" << insns_size << ")";
1429 }
1430 /* Create and initialize table holding register status */
1431 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1432 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001433
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 work_line_.reset(new RegisterLine(registers_size, this));
1435 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001436
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 /* Initialize register types of method arguments. */
1438 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001439 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1440 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 return false;
1442 }
1443 /* Perform code flow verification. */
1444 if (!CodeFlowVerifyMethod()) {
1445 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001446 }
1447
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 /* Generate a register map and add it to the method. */
1449 ByteArray* map = GenerateGcMap();
1450 if (map == NULL) {
1451 return false; // Not a real failure, but a failure to encode
1452 }
1453 method_->SetGcMap(map);
1454#ifndef NDEBUG
1455 VerifyGcMap();
1456#endif
jeffhaobdb76512011-09-07 11:43:16 -07001457 return true;
1458}
1459
Ian Rogersd81871c2011-10-03 13:57:23 -07001460void DexVerifier::Dump(std::ostream& os) {
1461 if (method_->IsNative()) {
1462 os << "Native method" << std::endl;
1463 return;
jeffhaobdb76512011-09-07 11:43:16 -07001464 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 DCHECK(code_item_ != NULL);
1466 const Instruction* inst = Instruction::At(code_item_->insns_);
1467 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1468 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001469 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1470 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1472 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001473 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001474 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 inst = inst->Next();
1476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477}
1478
Ian Rogersd81871c2011-10-03 13:57:23 -07001479static bool IsPrimitiveDescriptor(char descriptor) {
1480 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001481 case 'I':
1482 case 'C':
1483 case 'S':
1484 case 'B':
1485 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001486 case 'F':
1487 case 'D':
1488 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001490 default:
1491 return false;
1492 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001493}
1494
Ian Rogersd81871c2011-10-03 13:57:23 -07001495bool DexVerifier::SetTypesFromSignature() {
1496 RegisterLine* reg_line = reg_table_.GetLine(0);
1497 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1498 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001499
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1501 //Include the "this" pointer.
1502 size_t cur_arg = 0;
1503 if (!method_->IsStatic()) {
1504 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1505 // argument as uninitialized. This restricts field access until the superclass constructor is
1506 // called.
1507 Class* declaring_class = method_->GetDeclaringClass();
1508 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1509 reg_line->SetRegisterType(arg_start + cur_arg,
1510 reg_types_.UninitializedThisArgument(declaring_class));
1511 } else {
1512 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001515 }
1516
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1518 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1519
1520 for (; iterator.HasNext(); iterator.Next()) {
1521 const char* descriptor = iterator.GetDescriptor();
1522 if (descriptor == NULL) {
1523 LOG(FATAL) << "Null descriptor";
1524 }
1525 if (cur_arg >= expected_args) {
1526 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1527 << " args, found more (" << descriptor << ")";
1528 return false;
1529 }
1530 switch (descriptor[0]) {
1531 case 'L':
1532 case '[':
1533 // We assume that reference arguments are initialized. The only way it could be otherwise
1534 // (assuming the caller was verified) is if the current method is <init>, but in that case
1535 // it's effectively considered initialized the instant we reach here (in the sense that we
1536 // can return without doing anything or call virtual methods).
1537 {
1538 const RegType& reg_type =
1539 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001540 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 }
1542 break;
1543 case 'Z':
1544 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1545 break;
1546 case 'C':
1547 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1548 break;
1549 case 'B':
1550 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1551 break;
1552 case 'I':
1553 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1554 break;
1555 case 'S':
1556 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1557 break;
1558 case 'F':
1559 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1560 break;
1561 case 'J':
1562 case 'D': {
1563 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1564 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1565 cur_arg++;
1566 break;
1567 }
1568 default:
1569 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1570 return false;
1571 }
1572 cur_arg++;
1573 }
1574 if (cur_arg != expected_args) {
1575 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1576 return false;
1577 }
1578 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1579 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1580 // format. Only major difference from the method argument format is that 'V' is supported.
1581 bool result;
1582 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1583 result = descriptor[1] == '\0';
1584 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1585 size_t i = 0;
1586 do {
1587 i++;
1588 } while (descriptor[i] == '['); // process leading [
1589 if (descriptor[i] == 'L') { // object array
1590 do {
1591 i++; // find closing ;
1592 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1593 result = descriptor[i] == ';';
1594 } else { // primitive array
1595 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1596 }
1597 } else if (descriptor[0] == 'L') {
1598 // could be more thorough here, but shouldn't be required
1599 size_t i = 0;
1600 do {
1601 i++;
1602 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1603 result = descriptor[i] == ';';
1604 } else {
1605 result = false;
1606 }
1607 if (!result) {
1608 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1609 << descriptor << "'";
1610 }
1611 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001612}
1613
Ian Rogersd81871c2011-10-03 13:57:23 -07001614bool DexVerifier::CodeFlowVerifyMethod() {
1615 const uint16_t* insns = code_item_->insns_;
1616 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001617
jeffhaobdb76512011-09-07 11:43:16 -07001618 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 insn_flags_[0].SetChanged();
1620 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001621
jeffhaobdb76512011-09-07 11:43:16 -07001622 /* Continue until no instructions are marked "changed". */
1623 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1625 uint32_t insn_idx = start_guess;
1626 for (; insn_idx < insns_size; insn_idx++) {
1627 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001628 break;
1629 }
jeffhaobdb76512011-09-07 11:43:16 -07001630 if (insn_idx == insns_size) {
1631 if (start_guess != 0) {
1632 /* try again, starting from the top */
1633 start_guess = 0;
1634 continue;
1635 } else {
1636 /* all flags are clear */
1637 break;
1638 }
1639 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 // We carry the working set of registers from instruction to instruction. If this address can
1641 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1642 // "changed" flags, we need to load the set of registers from the table.
1643 // Because we always prefer to continue on to the next instruction, we should never have a
1644 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1645 // target.
1646 work_insn_idx_ = insn_idx;
1647 if (insn_flags_[insn_idx].IsBranchTarget()) {
1648 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001649 } else {
1650#ifndef NDEBUG
1651 /*
1652 * Sanity check: retrieve the stored register line (assuming
1653 * a full table) and make sure it actually matches.
1654 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1656 if (register_line != NULL) {
1657 if (work_line_->CompareLine(register_line) != 0) {
1658 Dump(std::cout);
1659 std::cout << info_messages_.str();
1660 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1661 << "@" << (void*)work_insn_idx_ << std::endl
1662 << " work_line=" << *work_line_ << std::endl
1663 << " expected=" << *register_line;
1664 }
jeffhaobdb76512011-09-07 11:43:16 -07001665 }
1666#endif
1667 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 if (!CodeFlowVerifyInstruction(&start_guess)) {
1669 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001670 return false;
1671 }
jeffhaobdb76512011-09-07 11:43:16 -07001672 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 insn_flags_[insn_idx].SetVisited();
1674 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001675 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001676
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001678 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001679 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001680 * (besides the wasted space), but it indicates a flaw somewhere
1681 * down the line, possibly in the verifier.
1682 *
1683 * If we've substituted "always throw" instructions into the stream,
1684 * we are almost certainly going to have some dead code.
1685 */
1686 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 uint32_t insn_idx = 0;
1688 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001689 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001690 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001691 * may or may not be preceded by a padding NOP (for alignment).
1692 */
1693 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1694 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1695 insns[insn_idx] == Instruction::kArrayDataSignature ||
1696 (insns[insn_idx] == Instruction::NOP &&
1697 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1698 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1699 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001701 }
1702
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001704 if (dead_start < 0)
1705 dead_start = insn_idx;
1706 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001708 dead_start = -1;
1709 }
1710 }
1711 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001713 }
1714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 return true;
1716}
1717
Ian Rogersd81871c2011-10-03 13:57:23 -07001718bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001719#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001721 gDvm.verifierStats.instrsReexamined++;
1722 } else {
1723 gDvm.verifierStats.instrsExamined++;
1724 }
1725#endif
1726
1727 /*
1728 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001729 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001730 * control to another statement:
1731 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001732 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001733 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001734 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001735 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001736 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001737 * throw an exception that is handled by an encompassing "try"
1738 * block.
1739 *
1740 * We can also return, in which case there is no successor instruction
1741 * from this point.
1742 *
1743 * The behavior can be determined from the OpcodeFlags.
1744 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1746 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001747 Instruction::DecodedInstruction dec_insn(inst);
1748 int opcode_flag = inst->Flag();
1749
jeffhaobdb76512011-09-07 11:43:16 -07001750 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001751 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001752 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 // Generate processing back trace to debug verifier
Ian Rogers2c8a8572011-10-24 17:11:36 -07001754 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl << *work_line_.get();
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 }
jeffhaobdb76512011-09-07 11:43:16 -07001756
1757 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001758 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001759 * can throw an exception, we will copy/merge this into the "catch"
1760 * address rather than work_line, because we don't want the result
1761 * from the "successful" code path (e.g. a check-cast that "improves"
1762 * a type) to be visible to the exception handler.
1763 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1765 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001766 } else {
1767#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001769#endif
1770 }
1771
1772 switch (dec_insn.opcode_) {
1773 case Instruction::NOP:
1774 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001775 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001776 * a signature that looks like a NOP; if we see one of these in
1777 * the course of executing code then we have a problem.
1778 */
1779 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001781 }
1782 break;
1783
1784 case Instruction::MOVE:
1785 case Instruction::MOVE_FROM16:
1786 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001788 break;
1789 case Instruction::MOVE_WIDE:
1790 case Instruction::MOVE_WIDE_FROM16:
1791 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::MOVE_OBJECT:
1795 case Instruction::MOVE_OBJECT_FROM16:
1796 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
1799
1800 /*
1801 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001802 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001803 * might want to hold the result in an actual CPU register, so the
1804 * Dalvik spec requires that these only appear immediately after an
1805 * invoke or filled-new-array.
1806 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001807 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001808 * redundant with the reset done below, but it can make the debug info
1809 * easier to read in some cases.)
1810 */
1811 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
1817 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001822 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 * This statement can only appear as the first instruction in an exception handler (though not
1824 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001825 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001826 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001828 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001830 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001832 }
1833 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1837 if (!GetMethodReturnType().IsUnknown()) {
1838 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1839 }
jeffhaobdb76512011-09-07 11:43:16 -07001840 }
1841 break;
1842 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001844 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 const RegType& return_type = GetMethodReturnType();
1846 if (!return_type.IsCategory1Types()) {
1847 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1848 } else {
1849 // Compilers may generate synthetic functions that write byte values into boolean fields.
1850 // Also, it may use integer values for boolean, byte, short, and character return types.
1851 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1852 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1853 ((return_type.IsBoolean() || return_type.IsByte() ||
1854 return_type.IsShort() || return_type.IsChar()) &&
1855 src_type.IsInteger()));
1856 /* check the register contents */
1857 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1858 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001859 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
jeffhaobdb76512011-09-07 11:43:16 -07001861 }
1862 }
1863 break;
1864 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001866 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 const RegType& return_type = GetMethodReturnType();
1868 if (!return_type.IsCategory2Types()) {
1869 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1870 } else {
1871 /* check the register contents */
1872 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1873 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001874 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 }
jeffhaobdb76512011-09-07 11:43:16 -07001876 }
1877 }
1878 break;
1879 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1881 const RegType& return_type = GetMethodReturnType();
1882 if (!return_type.IsReferenceTypes()) {
1883 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1884 } else {
1885 /* return_type is the *expected* return type, not register value */
1886 DCHECK(!return_type.IsZero());
1887 DCHECK(!return_type.IsUninitializedReference());
1888 // Verify that the reference in vAA is an instance of the type in "return_type". The Zero
1889 // type is allowed here. If the method is declared to return an interface, then any
1890 // initialized reference is acceptable.
1891 // Note GetClassFromRegister fails if the register holds an uninitialized reference, so
1892 // we do not allow them to be returned.
1893 Class* decl_class = return_type.GetClass();
1894 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
1895 if (res_class != NULL && failure_ == VERIFY_ERROR_NONE) {
1896 if (!decl_class->IsInterface() && !decl_class->IsAssignableFrom(res_class)) {
1897 Fail(VERIFY_ERROR_GENERIC) << "returning " << PrettyClassAndClassLoader(res_class)
1898 << ", declared " << PrettyClassAndClassLoader(res_class);
1899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 }
1901 }
1902 }
1903 break;
1904
1905 case Instruction::CONST_4:
1906 case Instruction::CONST_16:
1907 case Instruction::CONST:
1908 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
1911 case Instruction::CONST_HIGH16:
1912 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 work_line_->SetRegisterType(dec_insn.vA_,
1914 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001915 break;
1916 case Instruction::CONST_WIDE_16:
1917 case Instruction::CONST_WIDE_32:
1918 case Instruction::CONST_WIDE:
1919 case Instruction::CONST_WIDE_HIGH16:
1920 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
1923 case Instruction::CONST_STRING:
1924 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001928 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001930 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1932 Fail(failure_) << "unable to resolve const-class " << dec_insn.vB_
1933 << " (" << bad_class_desc << ") in "
1934 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1935 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001936 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001938 }
1939 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 }
jeffhaobdb76512011-09-07 11:43:16 -07001941 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001943 break;
1944 case Instruction::MONITOR_EXIT:
1945 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001947 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001948 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001949 * to the need to handle asynchronous exceptions, a now-deprecated
1950 * feature that Dalvik doesn't support.)
1951 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001953 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001954 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001955 * structured locking checks are working, the former would have
1956 * failed on the -enter instruction, and the latter is impossible.
1957 *
1958 * This is fortunate, because issue 3221411 prevents us from
1959 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001960 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001961 * some catch blocks (which will show up as "dead" code when
1962 * we skip them here); if we can't, then the code path could be
1963 * "live" so we still need to check it.
1964 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 opcode_flag &= ~Instruction::kThrow;
1966 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001967 break;
1968
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001970 /*
1971 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001972 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001973 * we don't try to address it.)
1974 *
1975 * If it fails, an exception is thrown, which we deal with later
1976 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1977 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001979 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
1981 Fail(failure_) << "unable to resolve check-cast " << dec_insn.vB_
1982 << " (" << bad_class_desc << ") in "
1983 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1984 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001985 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1987 if (!orig_type.IsReferenceTypes()) {
1988 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1989 } else {
1990 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 }
1993 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 }
1995 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001996 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1998 if (!tmp_type.IsReferenceTypes()) {
1999 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07002000 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 /* make sure we can resolve the class; access check is important */
2002 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2003 if (res_class == NULL) {
2004 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
2005 Fail(failure_) << "unable to resolve instance of " << dec_insn.vC_
2006 << " (" << bad_class_desc << ") in "
2007 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2008 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2009 } else {
2010 /* result is boolean */
2011 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002012 }
jeffhaobdb76512011-09-07 11:43:16 -07002013 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002014 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 }
2016 case Instruction::ARRAY_LENGTH: {
2017 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
2018 if (failure_ == VERIFY_ERROR_NONE) {
2019 if (res_class != NULL && !res_class->IsArrayClass()) {
2020 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
2021 } else {
2022 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2023 }
2024 }
2025 break;
2026 }
2027 case Instruction::NEW_INSTANCE: {
2028 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002029 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2031 Fail(failure_) << "unable to resolve new-instance " << dec_insn.vB_
2032 << " (" << bad_class_desc << ") in "
2033 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2034 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2035 } else {
2036 /* can't create an instance of an interface or abstract class */
2037 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2038 Fail(VERIFY_ERROR_INSTANTIATION)
2039 << "new-instance on primitive, interface or abstract class"
2040 << PrettyDescriptor(res_class->GetDescriptor());
2041 } else {
2042 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2043 // Any registers holding previous allocations from this address that have not yet been
2044 // initialized must be marked invalid.
2045 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2046
2047 /* add the new uninitialized reference to the register state */
2048 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2049 }
2050 }
2051 break;
2052 }
2053 case Instruction::NEW_ARRAY: {
2054 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2055 if (res_class == NULL) {
2056 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
2057 Fail(failure_) << "unable to resolve new-array " << dec_insn.vC_
2058 << " (" << bad_class_desc << ") in "
2059 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2060 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002061 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002063 } else {
2064 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002066 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002068 }
2069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
jeffhaobdb76512011-09-07 11:43:16 -07002071 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2073 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002074 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
2076 Fail(failure_) << "unable to resolve filled-array " << dec_insn.vB_
2077 << " (" << bad_class_desc << ") in "
2078 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2079 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002080 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002082 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002083 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002084 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002086 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002088 just_set_result = true;
2089 }
2090 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 }
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::CMPL_FLOAT:
2093 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2095 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2096 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::CMPL_DOUBLE:
2099 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2101 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2102 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2106 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2107 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 case Instruction::THROW: {
2110 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2111 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2112 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2113 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2114 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002115 }
2116 }
2117 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 }
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::GOTO:
2120 case Instruction::GOTO_16:
2121 case Instruction::GOTO_32:
2122 /* no effect on or use of registers */
2123 break;
2124
2125 case Instruction::PACKED_SWITCH:
2126 case Instruction::SPARSE_SWITCH:
2127 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 case Instruction::FILL_ARRAY_DATA: {
2132 /* Similar to the verification done for APUT */
2133 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2134 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002135 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 if (res_class != NULL) {
2137 Class* component_type = res_class->GetComponentType();
2138 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2139 component_type->IsPrimitiveVoid()) {
2140 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2141 << PrettyDescriptor(res_class->GetDescriptor());
2142 } else {
2143 const RegType& value_type = reg_types_.FromClass(component_type);
2144 DCHECK(!value_type.IsUnknown());
2145 // Now verify if the element width in the table matches the element width declared in
2146 // the array
2147 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2148 if (array_data[0] != Instruction::kArrayDataSignature) {
2149 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2150 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002151 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 // Since we don't compress the data in Dex, expect to see equal width of data stored
2153 // in the table and expected from the array class.
2154 if (array_data[1] != elem_width) {
2155 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2156 << " vs " << elem_width << ")";
2157 }
2158 }
2159 }
jeffhaobdb76512011-09-07 11:43:16 -07002160 }
2161 }
2162 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 }
jeffhaobdb76512011-09-07 11:43:16 -07002164 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 case Instruction::IF_NE: {
2166 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2167 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2168 bool mismatch = false;
2169 if (reg_type1.IsZero()) { // zero then integral or reference expected
2170 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2171 } else if (reg_type1.IsReferenceTypes()) { // both references?
2172 mismatch = !reg_type2.IsReferenceTypes();
2173 } else { // both integral?
2174 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2175 }
2176 if (mismatch) {
2177 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2178 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002179 }
2180 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 }
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::IF_LT:
2183 case Instruction::IF_GE:
2184 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::IF_LE: {
2186 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2187 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2188 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2189 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2190 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002191 }
2192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 case Instruction::IF_NEZ: {
2196 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2197 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2198 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 }
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::IF_LTZ:
2203 case Instruction::IF_GEZ:
2204 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 case Instruction::IF_LEZ: {
2206 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2207 if (!reg_type.IsIntegralTypes()) {
2208 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2209 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2210 }
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 }
jeffhaobdb76512011-09-07 11:43:16 -07002213 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2215 break;
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2218 break;
jeffhaobdb76512011-09-07 11:43:16 -07002219 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 VerifyAGet(dec_insn, reg_types_.Char(), true);
2221 break;
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002224 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 case Instruction::AGET:
2226 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2227 break;
jeffhaobdb76512011-09-07 11:43:16 -07002228 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 VerifyAGet(dec_insn, reg_types_.Long(), true);
2230 break;
2231 case Instruction::AGET_OBJECT:
2232 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 case Instruction::APUT_BOOLEAN:
2236 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2237 break;
2238 case Instruction::APUT_BYTE:
2239 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2240 break;
2241 case Instruction::APUT_CHAR:
2242 VerifyAPut(dec_insn, reg_types_.Char(), true);
2243 break;
2244 case Instruction::APUT_SHORT:
2245 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
2247 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256
jeffhaobdb76512011-09-07 11:43:16 -07002257 case Instruction::IGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 VerifyIGet(dec_insn, reg_types_.Boolean(), true);
2259 break;
jeffhaobdb76512011-09-07 11:43:16 -07002260 case Instruction::IGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 VerifyIGet(dec_insn, reg_types_.Byte(), true);
2262 break;
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::IGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 VerifyIGet(dec_insn, reg_types_.Char(), true);
2265 break;
jeffhaobdb76512011-09-07 11:43:16 -07002266 case Instruction::IGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 VerifyIGet(dec_insn, reg_types_.Short(), true);
2268 break;
2269 case Instruction::IGET:
2270 VerifyIGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272 case Instruction::IGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 VerifyIGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
2275 case Instruction::IGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 VerifyIGet(dec_insn, reg_types_.JavaLangObject(), false);
2277 break;
jeffhaobdb76512011-09-07 11:43:16 -07002278
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 case Instruction::IPUT_BOOLEAN:
2280 VerifyIPut(dec_insn, reg_types_.Boolean(), true);
2281 break;
2282 case Instruction::IPUT_BYTE:
2283 VerifyIPut(dec_insn, reg_types_.Byte(), true);
2284 break;
2285 case Instruction::IPUT_CHAR:
2286 VerifyIPut(dec_insn, reg_types_.Char(), true);
2287 break;
2288 case Instruction::IPUT_SHORT:
2289 VerifyIPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
2291 case Instruction::IPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 VerifyIPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::IPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 VerifyIPut(dec_insn, reg_types_.Long(), true);
2296 break;
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::IPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 VerifyIPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::SGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 VerifySGet(dec_insn, reg_types_.Boolean(), true);
2303 break;
jeffhaobdb76512011-09-07 11:43:16 -07002304 case Instruction::SGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 VerifySGet(dec_insn, reg_types_.Byte(), true);
2306 break;
jeffhaobdb76512011-09-07 11:43:16 -07002307 case Instruction::SGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 VerifySGet(dec_insn, reg_types_.Char(), true);
2309 break;
jeffhaobdb76512011-09-07 11:43:16 -07002310 case Instruction::SGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 VerifySGet(dec_insn, reg_types_.Short(), true);
2312 break;
2313 case Instruction::SGET:
2314 VerifySGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::SGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 VerifySGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::SGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 VerifySGet(dec_insn, reg_types_.JavaLangObject(), false);
2321 break;
2322
2323 case Instruction::SPUT_BOOLEAN:
2324 VerifySPut(dec_insn, reg_types_.Boolean(), true);
2325 break;
2326 case Instruction::SPUT_BYTE:
2327 VerifySPut(dec_insn, reg_types_.Byte(), true);
2328 break;
2329 case Instruction::SPUT_CHAR:
2330 VerifySPut(dec_insn, reg_types_.Char(), true);
2331 break;
2332 case Instruction::SPUT_SHORT:
2333 VerifySPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::SPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 VerifySPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::SPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 VerifySPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::SPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 VerifySPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344
2345 case Instruction::INVOKE_VIRTUAL:
2346 case Instruction::INVOKE_VIRTUAL_RANGE:
2347 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 case Instruction::INVOKE_SUPER_RANGE: {
2349 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2350 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2351 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2352 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2353 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2354 if (failure_ == VERIFY_ERROR_NONE) {
2355 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2356 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002357 just_set_result = true;
2358 }
2359 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 }
jeffhaobdb76512011-09-07 11:43:16 -07002361 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 case Instruction::INVOKE_DIRECT_RANGE: {
2363 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2364 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2365 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002366 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 * Some additional checks when calling a constructor. We know from the invocation arg check
2368 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2369 * that to require that called_method->klass is the same as this->klass or this->super,
2370 * allowing the latter only if the "this" argument is the same as the "this" argument to
2371 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002372 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 if (called_method->IsConstructor()) {
2374 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2375 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377
2378 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 if (this_type.IsZero()) {
2380 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002384 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002385
2386 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2388 if (this_class != method_->GetDeclaringClass()) {
2389 Fail(VERIFY_ERROR_GENERIC)
2390 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 }
2393 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396 }
2397
2398 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002399 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2401 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 }
2404
2405 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002406 * Replace the uninitialized reference with an initialized one. We need to do this for all
2407 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002408 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 work_line_->MarkRefsAsInitialized(this_type);
2410 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002412 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2414 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002415 just_set_result = true;
2416 }
2417 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 }
jeffhaobdb76512011-09-07 11:43:16 -07002419 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::INVOKE_STATIC_RANGE: {
2421 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2422 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2423 if (failure_ == VERIFY_ERROR_NONE) {
2424 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2425 work_line_->SetResultRegisterType(return_type);
2426 just_set_result = true;
2427 }
jeffhaobdb76512011-09-07 11:43:16 -07002428 }
2429 break;
2430 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 case Instruction::INVOKE_INTERFACE_RANGE: {
2432 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2433 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2434 if (failure_ == VERIFY_ERROR_NONE) {
2435 Class* called_interface = abs_method->GetDeclaringClass();
2436 if (!called_interface->IsInterface()) {
2437 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2438 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
jeffhaobdb76512011-09-07 11:43:16 -07002440 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 /* Get the type of the "this" arg, which should either be a sub-interface of called
2442 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002443 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2445 if (failure_ == VERIFY_ERROR_NONE) {
2446 if (this_type.IsZero()) {
2447 /* null pointer always passes (and always fails at runtime) */
2448 } else {
2449 Class* this_class = this_type.GetClass();
2450 if (this_type.IsUninitializedReference() || this_class == NULL) {
2451 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized";
2452 break;
2453 }
2454 if (!this_class->IsObjectClass() && !called_interface->IsAssignableFrom(this_class)) {
2455 Fail(VERIFY_ERROR_GENERIC) << "unable to match abstract method '"
2456 << PrettyMethod(abs_method) << "' with "
2457 << PrettyDescriptor(this_class->GetDescriptor())
2458 << " interfaces";
2459 break;
2460 }
2461 }
jeffhaobdb76512011-09-07 11:43:16 -07002462 }
2463 }
jeffhaobdb76512011-09-07 11:43:16 -07002464 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 * We don't have an object instance, so we can't find the concrete method. However, all of
2466 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002467 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 const RegType& return_type = reg_types_.FromClass(abs_method->GetReturnType());
2469 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002470 just_set_result = true;
2471 }
2472 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 }
jeffhaobdb76512011-09-07 11:43:16 -07002474 case Instruction::NEG_INT:
2475 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002477 break;
2478 case Instruction::NEG_LONG:
2479 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533
2534 case Instruction::ADD_INT:
2535 case Instruction::SUB_INT:
2536 case Instruction::MUL_INT:
2537 case Instruction::REM_INT:
2538 case Instruction::DIV_INT:
2539 case Instruction::SHL_INT:
2540 case Instruction::SHR_INT:
2541 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::AND_INT:
2545 case Instruction::OR_INT:
2546 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::ADD_LONG:
2550 case Instruction::SUB_LONG:
2551 case Instruction::MUL_LONG:
2552 case Instruction::DIV_LONG:
2553 case Instruction::REM_LONG:
2554 case Instruction::AND_LONG:
2555 case Instruction::OR_LONG:
2556 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559 case Instruction::SHL_LONG:
2560 case Instruction::SHR_LONG:
2561 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 /* shift distance is Int, making these different from other binary operations */
2563 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
2565 case Instruction::ADD_FLOAT:
2566 case Instruction::SUB_FLOAT:
2567 case Instruction::MUL_FLOAT:
2568 case Instruction::DIV_FLOAT:
2569 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::ADD_DOUBLE:
2573 case Instruction::SUB_DOUBLE:
2574 case Instruction::MUL_DOUBLE:
2575 case Instruction::DIV_DOUBLE:
2576 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::ADD_INT_2ADDR:
2580 case Instruction::SUB_INT_2ADDR:
2581 case Instruction::MUL_INT_2ADDR:
2582 case Instruction::REM_INT_2ADDR:
2583 case Instruction::SHL_INT_2ADDR:
2584 case Instruction::SHR_INT_2ADDR:
2585 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::AND_INT_2ADDR:
2589 case Instruction::OR_INT_2ADDR:
2590 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 case Instruction::ADD_LONG_2ADDR:
2597 case Instruction::SUB_LONG_2ADDR:
2598 case Instruction::MUL_LONG_2ADDR:
2599 case Instruction::DIV_LONG_2ADDR:
2600 case Instruction::REM_LONG_2ADDR:
2601 case Instruction::AND_LONG_2ADDR:
2602 case Instruction::OR_LONG_2ADDR:
2603 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::SHL_LONG_2ADDR:
2607 case Instruction::SHR_LONG_2ADDR:
2608 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::ADD_FLOAT_2ADDR:
2612 case Instruction::SUB_FLOAT_2ADDR:
2613 case Instruction::MUL_FLOAT_2ADDR:
2614 case Instruction::DIV_FLOAT_2ADDR:
2615 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::ADD_DOUBLE_2ADDR:
2619 case Instruction::SUB_DOUBLE_2ADDR:
2620 case Instruction::MUL_DOUBLE_2ADDR:
2621 case Instruction::DIV_DOUBLE_2ADDR:
2622 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::ADD_INT_LIT16:
2626 case Instruction::RSUB_INT:
2627 case Instruction::MUL_INT_LIT16:
2628 case Instruction::DIV_INT_LIT16:
2629 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::AND_INT_LIT16:
2633 case Instruction::OR_INT_LIT16:
2634 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::ADD_INT_LIT8:
2638 case Instruction::RSUB_INT_LIT8:
2639 case Instruction::MUL_INT_LIT8:
2640 case Instruction::DIV_INT_LIT8:
2641 case Instruction::REM_INT_LIT8:
2642 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002643 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::AND_INT_LIT8:
2648 case Instruction::OR_INT_LIT8:
2649 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652
2653 /*
2654 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002655 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002656 * inserted in the course of verification, we can expect to see it here.
2657 */
jeffhaob4df5142011-09-19 20:25:32 -07002658 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002662 case Instruction::UNUSED_EE:
2663 case Instruction::UNUSED_EF:
2664 case Instruction::UNUSED_F2:
2665 case Instruction::UNUSED_F3:
2666 case Instruction::UNUSED_F4:
2667 case Instruction::UNUSED_F5:
2668 case Instruction::UNUSED_F6:
2669 case Instruction::UNUSED_F7:
2670 case Instruction::UNUSED_F8:
2671 case Instruction::UNUSED_F9:
2672 case Instruction::UNUSED_FA:
2673 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002674 case Instruction::UNUSED_F0:
2675 case Instruction::UNUSED_F1:
2676 case Instruction::UNUSED_E3:
2677 case Instruction::UNUSED_E8:
2678 case Instruction::UNUSED_E7:
2679 case Instruction::UNUSED_E4:
2680 case Instruction::UNUSED_E9:
2681 case Instruction::UNUSED_FC:
2682 case Instruction::UNUSED_E5:
2683 case Instruction::UNUSED_EA:
2684 case Instruction::UNUSED_FD:
2685 case Instruction::UNUSED_E6:
2686 case Instruction::UNUSED_EB:
2687 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002688 case Instruction::UNUSED_3E:
2689 case Instruction::UNUSED_3F:
2690 case Instruction::UNUSED_40:
2691 case Instruction::UNUSED_41:
2692 case Instruction::UNUSED_42:
2693 case Instruction::UNUSED_43:
2694 case Instruction::UNUSED_73:
2695 case Instruction::UNUSED_79:
2696 case Instruction::UNUSED_7A:
2697 case Instruction::UNUSED_EC:
2698 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002699 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701
2702 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002703 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002704 * complain if an instruction is missing (which is desirable).
2705 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002707
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 if (failure_ != VERIFY_ERROR_NONE) {
2709 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002710 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002711 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002712 return false;
2713 } else {
2714 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002715 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002717 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002719 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002721 opcode_flag = Instruction::kThrow;
2722 }
2723 }
jeffhaobdb76512011-09-07 11:43:16 -07002724 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 * If we didn't just set the result register, clear it out. This ensures that you can only use
2726 * "move-result" immediately after the result is set. (We could check this statically, but it's
2727 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002728 */
2729 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002731 }
2732
jeffhaoa0a764a2011-09-16 10:43:38 -07002733 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002734 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2736 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2737 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002738 return false;
2739 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2741 // next instruction isn't one.
2742 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002743 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 }
2745 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2746 if (next_line != NULL) {
2747 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2748 // needed.
2749 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002750 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 }
jeffhaobdb76512011-09-07 11:43:16 -07002752 } else {
2753 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 * We're not recording register data for the next instruction, so we don't know what the prior
2755 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002756 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002758 }
2759 }
2760
2761 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002762 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002763 *
2764 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002765 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002766 * somebody could get a reference field, check it for zero, and if the
2767 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002768 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002769 * that, and will reject the code.
2770 *
2771 * TODO: avoid re-fetching the branch target
2772 */
2773 if ((opcode_flag & Instruction::kBranch) != 0) {
2774 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002776 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002778 return false;
2779 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002780 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002782 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 }
jeffhaobdb76512011-09-07 11:43:16 -07002784 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002786 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 }
jeffhaobdb76512011-09-07 11:43:16 -07002788 }
2789
2790 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002791 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002792 *
2793 * We've already verified that the table is structurally sound, so we
2794 * just need to walk through and tag the targets.
2795 */
2796 if ((opcode_flag & Instruction::kSwitch) != 0) {
2797 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2798 const uint16_t* switch_insns = insns + offset_to_switch;
2799 int switch_count = switch_insns[1];
2800 int offset_to_targets, targ;
2801
2802 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2803 /* 0 = sig, 1 = count, 2/3 = first key */
2804 offset_to_targets = 4;
2805 } else {
2806 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002807 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002808 offset_to_targets = 2 + 2 * switch_count;
2809 }
2810
2811 /* verify each switch target */
2812 for (targ = 0; targ < switch_count; targ++) {
2813 int offset;
2814 uint32_t abs_offset;
2815
2816 /* offsets are 32-bit, and only partly endian-swapped */
2817 offset = switch_insns[offset_to_targets + targ * 2] |
2818 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 abs_offset = work_insn_idx_ + offset;
2820 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2821 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002822 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 }
2824 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002825 return false;
2826 }
2827 }
2828
2829 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2831 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002832 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2834 bool within_catch_all = false;
2835 DexFile::CatchHandlerIterator iterator =
2836 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002837
2838 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2840 within_catch_all = true;
2841 }
jeffhaobdb76512011-09-07 11:43:16 -07002842 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2844 * "work_regs", because at runtime the exception will be thrown before the instruction
2845 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002846 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002848 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 }
jeffhaobdb76512011-09-07 11:43:16 -07002850 }
2851
2852 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2854 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002855 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002857 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 * The state in work_line reflects the post-execution state. If the current instruction is a
2859 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002860 * it will do so before grabbing the lock).
2861 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2863 Fail(VERIFY_ERROR_GENERIC)
2864 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002865 return false;
2866 }
2867 }
2868 }
2869
jeffhaod1f0fde2011-09-08 17:25:33 -07002870 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 if ((opcode_flag & Instruction::kReturn) != 0) {
2872 if(!work_line_->VerifyMonitorStackEmpty()) {
2873 return false;
2874 }
jeffhaobdb76512011-09-07 11:43:16 -07002875 }
2876
2877 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002878 * Update start_guess. Advance to the next instruction of that's
2879 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002880 * neither of those exists we're in a return or throw; leave start_guess
2881 * alone and let the caller sort it out.
2882 */
2883 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002885 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2886 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002888 }
2889
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2891 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002892
2893 return true;
2894}
2895
Ian Rogersd81871c2011-10-03 13:57:23 -07002896Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2897 const Class* referrer = method_->GetDeclaringClass();
2898 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2899 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002900
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 if (res_class == NULL) {
2902 Thread::Current()->ClearException();
2903 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2904 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2905 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2906 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2907 << res_class->GetDescriptor()->ToModifiedUtf8();
2908 }
2909 return res_class;
2910}
2911
2912Class* DexVerifier::GetCaughtExceptionType() {
2913 Class* common_super = NULL;
2914 if (code_item_->tries_size_ != 0) {
2915 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2916 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2917 for (uint32_t i = 0; i < handlers_size; i++) {
2918 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2919 for (; !iterator.HasNext(); iterator.Next()) {
2920 DexFile::CatchHandlerItem handler = iterator.Get();
2921 if (handler.address_ == (uint32_t) work_insn_idx_) {
2922 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2923 common_super = JavaLangThrowable();
2924 } else {
2925 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2926 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2927 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2928 * test, so is essentially harmless.
2929 */
2930 if (klass == NULL) {
2931 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2932 << handler.type_idx_ << " ("
2933 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2934 return NULL;
2935 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2936 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2937 return NULL;
2938 } else if (common_super == NULL) {
2939 common_super = klass;
2940 } else {
2941 common_super = RegType::ClassJoin(common_super, klass);
2942 }
2943 }
2944 }
2945 }
2946 handlers_ptr = iterator.GetData();
2947 }
2948 }
2949 if (common_super == NULL) {
2950 /* no catch blocks, or no catches with classes we can find */
2951 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2952 }
2953 return common_super;
2954}
2955
2956Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2957 Class* referrer = method_->GetDeclaringClass();
2958 DexCache* dex_cache = referrer->GetDexCache();
2959 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2960 if (res_method == NULL) {
2961 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2962 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2963 if (klass == NULL) {
2964 DCHECK(failure_ != VERIFY_ERROR_NONE);
2965 return NULL;
2966 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002967 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2969 if (is_direct) {
2970 res_method = klass->FindDirectMethod(name, signature);
2971 } else if (klass->IsInterface()) {
2972 res_method = klass->FindInterfaceMethod(name, signature);
2973 } else {
2974 res_method = klass->FindVirtualMethod(name, signature);
2975 }
2976 if (res_method != NULL) {
2977 dex_cache->SetResolvedMethod(method_idx, res_method);
2978 } else {
2979 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2980 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2981 << " " << signature;
2982 return NULL;
2983 }
2984 }
2985 /* Check if access is allowed. */
2986 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2987 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2988 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2989 return NULL;
2990 }
2991 return res_method;
2992}
2993
2994Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2995 MethodType method_type, bool is_range, bool is_super) {
2996 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2997 // we're making.
2998 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2999 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
3000 if (res_method == NULL) {
3001 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
3002 const char* method_name = dex_file_->GetMethodName(method_id);
3003 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003004 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve method " << dec_insn.vB_ << ": "
3006 << class_descriptor << "." << method_name << " " << method_signature;
3007 return NULL;
3008 }
3009 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3010 // enforce them here.
3011 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3012 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3013 << PrettyMethod(res_method);
3014 return NULL;
3015 }
3016 // See if the method type implied by the invoke instruction matches the access flags for the
3017 // target method.
3018 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3019 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3020 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3021 ) {
3022 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3023 << PrettyMethod(res_method);
3024 return NULL;
3025 }
3026 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3027 // has a vtable entry for the target method.
3028 if (is_super) {
3029 DCHECK(method_type == METHOD_VIRTUAL);
3030 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3031 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3032 if (super == NULL) { // Only Object has no super class
3033 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3034 << " to super " << PrettyMethod(res_method);
3035 } else {
3036 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3037 << " to super " << PrettyDescriptor(super->GetDescriptor())
3038 << "." << res_method->GetName()->ToModifiedUtf8()
3039 << " " << res_method->GetSignature()->ToModifiedUtf8();
3040
3041 }
3042 return NULL;
3043 }
3044 }
3045 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3046 // match the call to the signature. Also, we might might be calling through an abstract method
3047 // definition (which doesn't have register count values).
3048 int expected_args = dec_insn.vA_;
3049 /* caught by static verifier */
3050 DCHECK(is_range || expected_args <= 5);
3051 if (expected_args > code_item_->outs_size_) {
3052 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3053 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3054 return NULL;
3055 }
3056 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3057 if (sig[0] != '(') {
3058 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3059 << " as descriptor doesn't start with '(': " << sig;
3060 return NULL;
3061 }
jeffhaobdb76512011-09-07 11:43:16 -07003062 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 * Check the "this" argument, which must be an instance of the class
3064 * that declared the method. For an interface class, we don't do the
3065 * full interface merge, so we can't do a rigorous check here (which
3066 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003067 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 int actual_args = 0;
3069 if (!res_method->IsStatic()) {
3070 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3071 if (failure_ != VERIFY_ERROR_NONE) {
3072 return NULL;
3073 }
3074 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3075 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3076 return NULL;
3077 }
3078 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3079 Class* actual_this_ref = actual_arg_type.GetClass();
3080 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
3081 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '"
3082 << PrettyDescriptor(actual_this_ref->GetDescriptor()) << "' not instance of '"
3083 << PrettyDescriptor(res_method->GetDeclaringClass()->GetDescriptor()) << "'";
3084 return NULL;
3085 }
3086 }
3087 actual_args++;
3088 }
3089 /*
3090 * Process the target method's signature. This signature may or may not
3091 * have been verified, so we can't assume it's properly formed.
3092 */
3093 size_t sig_offset = 0;
3094 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3095 if (actual_args >= expected_args) {
3096 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3097 << "'. Expected " << expected_args << " args, found more ("
3098 << sig.substr(sig_offset) << ")";
3099 return NULL;
3100 }
3101 std::string descriptor;
3102 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3103 size_t end;
3104 if (sig[sig_offset] == 'L') {
3105 end = sig.find(';', sig_offset);
3106 } else {
3107 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3108 if (sig[end] == 'L') {
3109 end = sig.find(';', end);
3110 }
3111 }
3112 if (end == std::string::npos) {
3113 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3114 << "bad signature component '" << sig << "' (missing ';')";
3115 return NULL;
3116 }
3117 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3118 sig_offset = end;
3119 } else {
3120 descriptor = sig[sig_offset];
3121 }
3122 const RegType& reg_type =
3123 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003124 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3125 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3126 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003127 }
3128 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3129 }
3130 if (sig[sig_offset] != ')') {
3131 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3132 return NULL;
3133 }
3134 if (actual_args != expected_args) {
3135 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3136 << " expected " << expected_args << " args, found " << actual_args;
3137 return NULL;
3138 } else {
3139 return res_method;
3140 }
3141}
3142
3143void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3144 const RegType& insn_type, bool is_primitive) {
3145 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3146 if (!index_type.IsArrayIndexTypes()) {
3147 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3148 } else {
3149 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3150 if (failure_ == VERIFY_ERROR_NONE) {
3151 if (array_class == NULL) {
3152 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3153 // instruction type. TODO: have a proper notion of bottom here.
3154 if (!is_primitive || insn_type.IsCategory1Types()) {
3155 // Reference or category 1
3156 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3157 } else {
3158 // Category 2
3159 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3160 }
3161 } else {
3162 /* verify the class */
3163 Class* component_class = array_class->GetComponentType();
3164 const RegType& component_type = reg_types_.FromClass(component_class);
3165 if (!array_class->IsArrayClass()) {
3166 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3167 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3168 } else if (component_class->IsPrimitive() && !is_primitive) {
3169 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3170 << PrettyDescriptor(array_class->GetDescriptor())
3171 << " source for aget-object";
3172 } else if (!component_class->IsPrimitive() && is_primitive) {
3173 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3174 << PrettyDescriptor(array_class->GetDescriptor())
3175 << " source for category 1 aget";
3176 } else if (is_primitive && !insn_type.Equals(component_type) &&
3177 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3178 (insn_type.IsLong() && component_type.IsDouble()))) {
3179 Fail(VERIFY_ERROR_GENERIC) << "array type "
3180 << PrettyDescriptor(array_class->GetDescriptor())
3181 << " incompatible with aget of type " << insn_type;
3182 } else {
3183 // Use knowledge of the field type which is stronger than the type inferred from the
3184 // instruction, which can't differentiate object types and ints from floats, longs from
3185 // doubles.
3186 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3187 }
3188 }
3189 }
3190 }
3191}
3192
3193void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3194 const RegType& insn_type, bool is_primitive) {
3195 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3196 if (!index_type.IsArrayIndexTypes()) {
3197 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3198 } else {
3199 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3200 if (failure_ == VERIFY_ERROR_NONE) {
3201 if (array_class == NULL) {
3202 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3203 // instruction type.
3204 } else {
3205 /* verify the class */
3206 Class* component_class = array_class->GetComponentType();
3207 const RegType& component_type = reg_types_.FromClass(component_class);
3208 if (!array_class->IsArrayClass()) {
3209 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3210 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3211 } else if (component_class->IsPrimitive() && !is_primitive) {
3212 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3213 << PrettyDescriptor(array_class->GetDescriptor())
3214 << " source for aput-object";
3215 } else if (!component_class->IsPrimitive() && is_primitive) {
3216 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3217 << PrettyDescriptor(array_class->GetDescriptor())
3218 << " source for category 1 aput";
3219 } else if (is_primitive && !insn_type.Equals(component_type) &&
3220 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3221 (insn_type.IsLong() && component_type.IsDouble()))) {
3222 Fail(VERIFY_ERROR_GENERIC) << "array type "
3223 << PrettyDescriptor(array_class->GetDescriptor())
3224 << " incompatible with aput of type " << insn_type;
3225 } else {
3226 // The instruction agrees with the type of array, confirm the value to be stored does too
3227 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3228 }
3229 }
3230 }
3231 }
3232}
3233
3234Field* DexVerifier::GetStaticField(int field_idx) {
3235 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3236 if (field == NULL) {
3237 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3238 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3239 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003240 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 DCHECK(Thread::Current()->IsExceptionPending());
3242 Thread::Current()->ClearException();
3243 return NULL;
3244 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3245 field->GetAccessFlags())) {
3246 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3247 << " from " << PrettyClass(method_->GetDeclaringClass());
3248 return NULL;
3249 } else if (!field->IsStatic()) {
3250 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3251 return NULL;
3252 } else {
3253 return field;
3254 }
3255}
3256
3257void DexVerifier::VerifySGet(const Instruction::DecodedInstruction& dec_insn,
3258 const RegType& insn_type, bool is_primitive) {
3259 Field* field = GetStaticField(dec_insn.vB_);
3260 if (field != NULL) {
3261 DCHECK(field->GetDeclaringClass()->IsResolved());
3262 Class* field_class = field->GetType();
3263 Class* insn_class = insn_type.GetClass();
3264 if (is_primitive) {
3265 if (field_class == insn_class ||
3266 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3267 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3268 // expected that read is of the correct primitive type or that int reads are reading
3269 // floats or long reads are reading doubles
3270 } else {
3271 // This is a global failure rather than a class change failure as the instructions and
3272 // the descriptors for the type should have been consistent within the same file at
3273 // compile time
3274 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3275 << " to be of type " << PrettyClass(insn_class)
3276 << " but found type " << PrettyClass(field_class) << " in sget";
3277 return;
3278 }
3279 } else {
3280 if (!insn_class->IsAssignableFrom(field_class)) {
3281 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3282 << " to be of type " << PrettyClass(insn_class)
3283 << " but found type " << PrettyClass(field_class)
3284 << " in sget-object";
3285 return;
3286 }
3287 }
3288 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3289 }
3290}
3291
3292void DexVerifier::VerifySPut(const Instruction::DecodedInstruction& dec_insn,
3293 const RegType& insn_type, bool is_primitive) {
3294 Field* field = GetStaticField(dec_insn.vB_);
3295 if (field != NULL) {
3296 DCHECK(field->GetDeclaringClass()->IsResolved());
3297 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3298 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final static field " << PrettyField(field)
3299 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3300 return;
3301 }
3302 Class* field_class = field->GetType();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003303 const RegType& field_type = reg_types_.FromClass(field_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 Class* insn_class = insn_type.GetClass();
3305 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003306 // Primitive field assignability rules are weaker than regular assignability rules
3307 bool instruction_compatible;
3308 bool value_compatible;
3309 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3310 if (field_type.IsIntegralTypes()) {
3311 instruction_compatible = insn_type.IsIntegralTypes();
3312 value_compatible = value_type.IsIntegralTypes();
3313 } else if (field_type.IsFloat()) {
3314 instruction_compatible = insn_type.IsInteger(); // no sput-float, so expect sput-int
3315 value_compatible = value_type.IsFloatTypes();
3316 } else if (field_type.IsLong()) {
3317 instruction_compatible = insn_type.IsLong();
3318 value_compatible = value_type.IsLongTypes();
3319 } else if (field_type.IsDouble()) {
3320 instruction_compatible = insn_type.IsLong(); // no sput-double, so expect sput-long
3321 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003323 instruction_compatible = false; // reference field with primitive store
3324 value_compatible = false; // unused
3325 }
3326 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003327 // This is a global failure rather than a class change failure as the instructions and
3328 // the descriptors for the type should have been consistent within the same file at
3329 // compile time
3330 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3331 << " to be of type " << PrettyClass(insn_class)
3332 << " but found type " << PrettyClass(field_class) << " in sput";
3333 return;
3334 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003335 if (!value_compatible) {
3336 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3337 << " of type " << value_type
3338 << " but expected " << field_type
3339 << " for store to " << PrettyField(field) << " in sput";
3340 return;
3341 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003342 } else {
3343 if (!insn_class->IsAssignableFrom(field_class)) {
3344 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3345 << " to be compatible with type " << insn_type
3346 << " but found type " << PrettyClass(field_class)
3347 << " in sput-object";
3348 return;
3349 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003350 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 }
3353}
3354
3355Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3356 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3357 if (field == NULL) {
3358 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3359 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3360 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003361 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 DCHECK(Thread::Current()->IsExceptionPending());
3363 Thread::Current()->ClearException();
3364 return NULL;
3365 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3366 field->GetAccessFlags())) {
3367 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3368 << " from " << PrettyClass(method_->GetDeclaringClass());
3369 return NULL;
3370 } else if (field->IsStatic()) {
3371 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3372 << " to not be static";
3373 return NULL;
3374 } else if (obj_type.IsZero()) {
3375 // Cannot infer and check type, however, access will cause null pointer exception
3376 return field;
3377 } else if(obj_type.IsUninitializedReference() &&
3378 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3379 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3380 // Field accesses through uninitialized references are only allowable for constructors where
3381 // the field is declared in this class
3382 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3383 << " of a not fully initialized object within the context of "
3384 << PrettyMethod(method_);
3385 return NULL;
3386 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3387 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3388 // of C1. For resolution to occur the declared class of the field must be compatible with
3389 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3390 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3391 << " from object of type " << PrettyClass(obj_type.GetClass());
3392 return NULL;
3393 } else {
3394 return field;
3395 }
3396}
3397
3398void DexVerifier::VerifyIGet(const Instruction::DecodedInstruction& dec_insn,
3399 const RegType& insn_type, bool is_primitive) {
3400 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3401 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3402 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 Class* field_class = field->GetType();
3404 Class* insn_class = insn_type.GetClass();
3405 if (is_primitive) {
3406 if (field_class == insn_class ||
3407 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3408 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3409 // expected that read is of the correct primitive type or that int reads are reading
3410 // floats or long reads are reading doubles
3411 } else {
3412 // This is a global failure rather than a class change failure as the instructions and
3413 // the descriptors for the type should have been consistent within the same file at
3414 // compile time
3415 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3416 << " to be of type " << PrettyClass(insn_class)
3417 << " but found type " << PrettyClass(field_class) << " in iget";
3418 return;
3419 }
3420 } else {
3421 if (!insn_class->IsAssignableFrom(field_class)) {
3422 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3423 << " to be compatible with type " << PrettyClass(insn_class)
3424 << " but found type " << PrettyClass(field_class) << " in iget-object";
3425 return;
3426 }
3427 }
3428 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3429 }
3430}
3431
3432void DexVerifier::VerifyIPut(const Instruction::DecodedInstruction& dec_insn,
3433 const RegType& insn_type, bool is_primitive) {
3434 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3435 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3436 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3438 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3439 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3440 return;
3441 }
3442 Class* field_class = field->GetType();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003443 const RegType& field_type = reg_types_.FromClass(field_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 Class* insn_class = insn_type.GetClass();
3445 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003446 // Primitive field assignability rules are weaker than regular assignability rules
3447 bool instruction_compatible;
3448 bool value_compatible;
3449 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3450 if (field_type.IsIntegralTypes()) {
3451 instruction_compatible = insn_type.IsIntegralTypes();
3452 value_compatible = value_type.IsIntegralTypes();
3453 } else if (field_type.IsFloat()) {
3454 instruction_compatible = insn_type.IsInteger(); // no iput-float, so expect iput-int
3455 value_compatible = value_type.IsFloatTypes();
3456 } else if (field_type.IsLong()) {
3457 instruction_compatible = insn_type.IsLong();
3458 value_compatible = value_type.IsLongTypes();
3459 } else if (field_type.IsDouble()) {
3460 instruction_compatible = insn_type.IsLong(); // no iput-double, so expect iput-long
3461 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003463 instruction_compatible = false; // reference field with primitive store
3464 value_compatible = false; // unused
3465 }
3466 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 // This is a global failure rather than a class change failure as the instructions and
3468 // the descriptors for the type should have been consistent within the same file at
3469 // compile time
3470 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3471 << " to be of type " << PrettyClass(insn_class)
3472 << " but found type " << PrettyClass(field_class) << " in iput";
3473 return;
3474 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003475 if (!value_compatible) {
3476 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3477 << " of type " << value_type
3478 << " but expected " << field_type
3479 << " for store to " << PrettyField(field) << " in iput";
3480 return;
3481 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 } else {
3483 if (!insn_class->IsAssignableFrom(field_class)) {
3484 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogers2c8a8572011-10-24 17:11:36 -07003485 << " to be compatible with type " << insn_type
3486 << " but found type " << PrettyClass(field_class)
3487 << " in iput-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 return;
3489 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003490 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 }
3493}
3494
3495bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3496 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3497 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3498 return false;
3499 }
3500 return true;
3501}
3502
3503void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3504 Class* res_class, bool is_range) {
3505 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3506 /*
3507 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3508 * list and fail. It's legal, if silly, for arg_count to be zero.
3509 */
3510 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3511 uint32_t arg_count = dec_insn.vA_;
3512 for (size_t ui = 0; ui < arg_count; ui++) {
3513 uint32_t get_reg;
3514
3515 if (is_range)
3516 get_reg = dec_insn.vC_ + ui;
3517 else
3518 get_reg = dec_insn.arg_[ui];
3519
3520 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3521 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3522 << ") not valid";
3523 return;
3524 }
3525 }
3526}
3527
3528void DexVerifier::ReplaceFailingInstruction() {
3529 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3530 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3531 VerifyErrorRefType ref_type;
3532 switch (inst->Opcode()) {
3533 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003534 case Instruction::CHECK_CAST:
3535 case Instruction::INSTANCE_OF:
3536 case Instruction::NEW_INSTANCE:
3537 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003539 case Instruction::FILLED_NEW_ARRAY_RANGE:
3540 ref_type = VERIFY_ERROR_REF_CLASS;
3541 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003543 case Instruction::IGET_BOOLEAN:
3544 case Instruction::IGET_BYTE:
3545 case Instruction::IGET_CHAR:
3546 case Instruction::IGET_SHORT:
3547 case Instruction::IGET_WIDE:
3548 case Instruction::IGET_OBJECT:
3549 case Instruction::IPUT:
3550 case Instruction::IPUT_BOOLEAN:
3551 case Instruction::IPUT_BYTE:
3552 case Instruction::IPUT_CHAR:
3553 case Instruction::IPUT_SHORT:
3554 case Instruction::IPUT_WIDE:
3555 case Instruction::IPUT_OBJECT:
3556 case Instruction::SGET:
3557 case Instruction::SGET_BOOLEAN:
3558 case Instruction::SGET_BYTE:
3559 case Instruction::SGET_CHAR:
3560 case Instruction::SGET_SHORT:
3561 case Instruction::SGET_WIDE:
3562 case Instruction::SGET_OBJECT:
3563 case Instruction::SPUT:
3564 case Instruction::SPUT_BOOLEAN:
3565 case Instruction::SPUT_BYTE:
3566 case Instruction::SPUT_CHAR:
3567 case Instruction::SPUT_SHORT:
3568 case Instruction::SPUT_WIDE:
3569 case Instruction::SPUT_OBJECT:
3570 ref_type = VERIFY_ERROR_REF_FIELD;
3571 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003573 case Instruction::INVOKE_VIRTUAL_RANGE:
3574 case Instruction::INVOKE_SUPER:
3575 case Instruction::INVOKE_SUPER_RANGE:
3576 case Instruction::INVOKE_DIRECT:
3577 case Instruction::INVOKE_DIRECT_RANGE:
3578 case Instruction::INVOKE_STATIC:
3579 case Instruction::INVOKE_STATIC_RANGE:
3580 case Instruction::INVOKE_INTERFACE:
3581 case Instruction::INVOKE_INTERFACE_RANGE:
3582 ref_type = VERIFY_ERROR_REF_METHOD;
3583 break;
jeffhaobdb76512011-09-07 11:43:16 -07003584 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003585 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003586 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003587 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3589 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3590 // instruction, so assert it.
3591 size_t width = inst->SizeInCodeUnits();
3592 CHECK_GT(width, 1u);
3593 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3594 // NOPs
3595 for (size_t i = 2; i < width; i++) {
3596 insns[work_insn_idx_ + i] = Instruction::NOP;
3597 }
3598 // Encode the opcode, with the failure code in the high byte
3599 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3600 (failure_ << 8) | // AA - component
3601 (ref_type << (8 + kVerifyErrorRefTypeShift));
3602 insns[work_insn_idx_] = new_instruction;
3603 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3604 // rewrote, so nothing to do here.
jeffhaobdb76512011-09-07 11:43:16 -07003605}
jeffhaoba5ebb92011-08-25 17:24:37 -07003606
Ian Rogersd81871c2011-10-03 13:57:23 -07003607bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3608 const bool merge_debug = true;
3609 bool changed = true;
3610 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3611 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003612 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 * We haven't processed this instruction before, and we haven't touched the registers here, so
3614 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3615 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003616 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003618 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3620 copy->CopyFromLine(target_line);
3621 changed = target_line->MergeRegisters(merge_line);
3622 if (failure_ != VERIFY_ERROR_NONE) {
3623 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003624 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003625 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3627 << *copy.get() << " MERGE" << std::endl
3628 << *merge_line << " ==" << std::endl
3629 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003630 }
3631 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003632 if (changed) {
3633 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003634 }
3635 return true;
3636}
3637
Ian Rogersd81871c2011-10-03 13:57:23 -07003638void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3639 size_t* log2_max_gc_pc) {
3640 size_t local_gc_points = 0;
3641 size_t max_insn = 0;
3642 size_t max_ref_reg = -1;
3643 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3644 if (insn_flags_[i].IsGcPoint()) {
3645 local_gc_points++;
3646 max_insn = i;
3647 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003648 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003649 }
3650 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003651 *gc_points = local_gc_points;
3652 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3653 size_t i = 0;
3654 while ((1U << i) < max_insn) {
3655 i++;
3656 }
3657 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003658}
3659
Ian Rogersd81871c2011-10-03 13:57:23 -07003660ByteArray* DexVerifier::GenerateGcMap() {
3661 size_t num_entries, ref_bitmap_bits, pc_bits;
3662 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3663 // There's a single byte to encode the size of each bitmap
3664 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3665 // TODO: either a better GC map format or per method failures
3666 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3667 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003668 return NULL;
3669 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3671 // There are 2 bytes to encode the number of entries
3672 if (num_entries >= 65536) {
3673 // TODO: either a better GC map format or per method failures
3674 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3675 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003676 return NULL;
3677 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003679 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003681 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 pc_bytes = 1;
3683 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003684 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003686 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 // TODO: either a better GC map format or per method failures
3688 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3689 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3690 return NULL;
3691 }
3692 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3693 ByteArray* table = ByteArray::Alloc(table_size);
3694 if (table == NULL) {
3695 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3696 return NULL;
3697 }
3698 // Write table header
3699 table->Set(0, format);
3700 table->Set(1, ref_bitmap_bytes);
3701 table->Set(2, num_entries & 0xFF);
3702 table->Set(3, (num_entries >> 8) & 0xFF);
3703 // Write table data
3704 size_t offset = 4;
3705 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3706 if (insn_flags_[i].IsGcPoint()) {
3707 table->Set(offset, i & 0xFF);
3708 offset++;
3709 if (pc_bytes == 2) {
3710 table->Set(offset, (i >> 8) & 0xFF);
3711 offset++;
3712 }
3713 RegisterLine* line = reg_table_.GetLine(i);
3714 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3715 offset += ref_bitmap_bytes;
3716 }
3717 }
3718 DCHECK(offset == table_size);
3719 return table;
3720}
jeffhaoa0a764a2011-09-16 10:43:38 -07003721
Ian Rogersd81871c2011-10-03 13:57:23 -07003722void DexVerifier::VerifyGcMap() {
3723 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3724 // that the table data is well formed and all references are marked (or not) in the bitmap
3725 PcToReferenceMap map(method_);
3726 size_t map_index = 0;
3727 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3728 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3729 if (insn_flags_[i].IsGcPoint()) {
3730 CHECK_LT(map_index, map.NumEntries());
3731 CHECK_EQ(map.GetPC(map_index), i);
3732 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3733 map_index++;
3734 RegisterLine* line = reg_table_.GetLine(i);
3735 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003736 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 CHECK_LT(j / 8, map.RegWidth());
3738 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3739 } else if ((j / 8) < map.RegWidth()) {
3740 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3741 } else {
3742 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3743 }
3744 }
3745 } else {
3746 CHECK(reg_bitmap == NULL);
3747 }
3748 }
3749}
jeffhaoa0a764a2011-09-16 10:43:38 -07003750
Ian Rogersd81871c2011-10-03 13:57:23 -07003751Class* DexVerifier::JavaLangThrowable() {
3752 if (java_lang_throwable_ == NULL) {
3753 java_lang_throwable_ =
3754 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3755 DCHECK(java_lang_throwable_ != NULL);
3756 }
3757 return java_lang_throwable_;
3758}
3759
3760const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3761 size_t num_entries = NumEntries();
3762 // Do linear or binary search?
3763 static const size_t kSearchThreshold = 8;
3764 if (num_entries < kSearchThreshold) {
3765 for (size_t i = 0; i < num_entries; i++) {
3766 if (GetPC(i) == dex_pc) {
3767 return GetBitMap(i);
3768 }
3769 }
3770 } else {
3771 int lo = 0;
3772 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003773 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 int mid = (hi + lo) / 2;
3775 int mid_pc = GetPC(mid);
3776 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003777 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003778 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003779 hi = mid - 1;
3780 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003782 }
3783 }
3784 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 if (error_if_not_present) {
3786 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3787 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003788 return NULL;
3789}
3790
Ian Rogersd81871c2011-10-03 13:57:23 -07003791} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003792} // namespace art