blob: 0338027030963fac4c8f9f1ce6c4fc81bcfd9b64 [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
Ian Rogersb5e95b92011-10-25 23:28:55 -0700154bool RegType::IsAssignableFrom(const RegType& src) const {
155 if (Equals(src)) {
156 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700157 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700158 switch (GetType()) {
159 case RegType::kRegTypeBoolean: return IsBooleanTypes();
160 case RegType::kRegTypeByte: return IsByteTypes();
161 case RegType::kRegTypeShort: return IsShortTypes();
162 case RegType::kRegTypeChar: return IsCharTypes();
163 case RegType::kRegTypeInteger: return IsIntegralTypes();
164 case RegType::kRegTypeFloat: return IsFloatTypes();
165 case RegType::kRegTypeLongLo: return IsLongTypes();
166 case RegType::kRegTypeDoubleLo: return IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700167 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700168 if (!IsReferenceTypes()) {
169 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700170 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700171 if (src.IsZero()) {
172 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700173 } else if (IsUninitializedReference()) {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700174 return false; // Nonsensical to Join two uninitialized classes
175 } else if (IsReference() && src.IsReference() &&
176 (GetClass()->IsAssignableFrom(src.GetClass()) ||
177 (src.GetClass()->IsObjectClass() && GetClass()->IsInterface()) ||
178 (GetClass()->IsObjectClass() && src.GetClass()->IsInterface()))) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700179 // Either we're assignable or this is trying to assign Object to an Interface, which
Ian Rogers84fa0742011-10-25 18:13:30 -0700180 // is allowed (see comment for ClassJoin)
Ian Rogersb5e95b92011-10-25 23:28:55 -0700181 return true;
182 } else if (src.IsUnresolvedReference() && IsReference() && GetClass()->IsObjectClass()) {
183 // We're an object being assigned an unresolved reference, which is ok
184 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700185 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700186 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700188 }
189 }
190}
191
Ian Rogers84fa0742011-10-25 18:13:30 -0700192static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
193 return a.IsConstant() ? b : a;
194}
jeffhaobdb76512011-09-07 11:43:16 -0700195
Ian Rogersd81871c2011-10-03 13:57:23 -0700196const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
197 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700198 if (IsUnknown() && incoming_type.IsUnknown()) {
199 return *this; // Unknown MERGE Unknown => Unknown
200 } else if (IsConflict()) {
201 return *this; // Conflict MERGE * => Conflict
202 } else if (incoming_type.IsConflict()) {
203 return incoming_type; // * MERGE Conflict => Conflict
204 } else if (IsUnknown() || incoming_type.IsUnknown()) {
205 return reg_types->Conflict(); // Unknown MERGE * => Conflict
206 } else if(IsConstant() && incoming_type.IsConstant()) {
207 int32_t val1 = ConstantValue();
208 int32_t val2 = incoming_type.ConstantValue();
209 if (val1 >= 0 && val2 >= 0) {
210 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
211 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700212 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700213 } else {
214 return incoming_type;
215 }
216 } else if (val1 < 0 && val2 < 0) {
217 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
218 if (val1 <= val2) {
219 return *this;
220 } else {
221 return incoming_type;
222 }
223 } else {
224 // Values are +ve and -ve, choose smallest signed type in which they both fit
225 if (IsConstantByte()) {
226 if (incoming_type.IsConstantByte()) {
227 return reg_types->ByteConstant();
228 } else if (incoming_type.IsConstantShort()) {
229 return reg_types->ShortConstant();
230 } else {
231 return reg_types->IntConstant();
232 }
233 } else if (IsConstantShort()) {
234 if (incoming_type.IsShort()) {
235 return reg_types->ShortConstant();
236 } else {
237 return reg_types->IntConstant();
238 }
239 } else {
240 return reg_types->IntConstant();
241 }
242 }
243 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
244 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
245 return reg_types->Boolean(); // boolean MERGE boolean => boolean
246 }
247 if (IsByteTypes() && incoming_type.IsByteTypes()) {
248 return reg_types->Byte(); // byte MERGE byte => byte
249 }
250 if (IsShortTypes() && incoming_type.IsShortTypes()) {
251 return reg_types->Short(); // short MERGE short => short
252 }
253 if (IsCharTypes() && incoming_type.IsCharTypes()) {
254 return reg_types->Char(); // char MERGE char => char
255 }
256 return reg_types->Integer(); // int MERGE * => int
257 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
258 (IsLongTypes() && incoming_type.IsLongTypes()) ||
259 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
260 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
261 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
262 // check constant case was handled prior to entry
263 DCHECK(!IsConstant() || !incoming_type.IsConstant());
264 // float/long/double MERGE float/long/double_constant => float/long/double
265 return SelectNonConstant(*this, incoming_type);
266 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
267 if (IsZero() | incoming_type.IsZero()) {
268 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
269 } else if (IsUnresolvedReference() || IsUninitializedReference() ||
270 IsUninitializedThisReference()) {
271 // Can only merge an uninitialized or unresolved type with itself or 0, we've already checked
272 // these so => Conflict
273 return reg_types->Conflict();
274 } else { // Two reference types, compute Join
275 Class* c1 = GetClass();
276 Class* c2 = incoming_type.GetClass();
277 DCHECK(c1 != NULL && !c1->IsPrimitive());
278 DCHECK(c2 != NULL && !c2->IsPrimitive());
279 Class* join_class = ClassJoin(c1, c2);
280 if (c1 == join_class) {
281 return *this;
282 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700283 return incoming_type;
284 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700285 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700287 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700288 } else {
289 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700290 }
291}
292
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700293static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700294 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700295 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
296 case Primitive::kPrimByte: return RegType::kRegTypeByte;
297 case Primitive::kPrimShort: return RegType::kRegTypeShort;
298 case Primitive::kPrimChar: return RegType::kRegTypeChar;
299 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
300 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
301 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
302 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
303 case Primitive::kPrimVoid:
304 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700305 }
306}
307
308static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
309 if (descriptor.length() == 1) {
310 switch (descriptor[0]) {
311 case 'Z': return RegType::kRegTypeBoolean;
312 case 'B': return RegType::kRegTypeByte;
313 case 'S': return RegType::kRegTypeShort;
314 case 'C': return RegType::kRegTypeChar;
315 case 'I': return RegType::kRegTypeInteger;
316 case 'J': return RegType::kRegTypeLongLo;
317 case 'F': return RegType::kRegTypeFloat;
318 case 'D': return RegType::kRegTypeDoubleLo;
319 case 'V':
320 default: return RegType::kRegTypeUnknown;
321 }
322 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
323 return RegType::kRegTypeReference;
324 } else {
325 return RegType::kRegTypeUnknown;
326 }
327}
328
329std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700330 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700331 return os;
332}
333
334const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
335 const std::string& descriptor) {
336 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
337}
338
339const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
340 const std::string& descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700341 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 // entries should be sized greater than primitive types
343 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
344 RegType* entry = entries_[type];
345 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700346 Class* klass = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -0700347 if (descriptor.size() != 0) {
348 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
349 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700350 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 entries_[type] = entry;
352 }
353 return *entry;
354 } else {
355 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers84fa0742011-10-25 18:13:30 -0700356 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700357 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700358 // check resolved and unresolved references, ignore uninitialized references
359 if (cur_entry->IsReference() && cur_entry->GetClass()->GetDescriptor()->Equals(descriptor)) {
360 return *cur_entry;
361 } else if (cur_entry->IsUnresolvedReference() &&
362 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700363 return *cur_entry;
364 }
365 }
366 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700367 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700368 // Able to resolve so create resolved register type
369 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700370 entries_.push_back(entry);
371 return *entry;
372 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700373 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700374 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700375 Thread::Current()->ClearException();
376 String* string_descriptor =
377 Runtime::Current()->GetInternTable()->InternStrong(descriptor.c_str());
378 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
379 entries_.size());
380 entries_.push_back(entry);
381 return *entry;
Ian Rogers2c8a8572011-10-24 17:11:36 -0700382 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 }
384}
385
386const RegType& RegTypeCache::FromClass(Class* klass) {
387 if (klass->IsPrimitive()) {
388 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
389 // entries should be sized greater than primitive types
390 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
391 RegType* entry = entries_[type];
392 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700393 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700394 entries_[type] = entry;
395 }
396 return *entry;
397 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700398 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700399 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700400 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 return *cur_entry;
402 }
403 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700404 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 entries_.push_back(entry);
406 return *entry;
407 }
408}
409
410const RegType& RegTypeCache::Uninitialized(Class* klass, uint32_t allocation_pc) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700411 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700413 if (cur_entry->IsUninitializedReference() && cur_entry->GetAllocationPc() == allocation_pc &&
414 cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 return *cur_entry;
416 }
417 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700418 RegType* entry = new RegType(RegType::kRegTypeUninitializedReference, klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700419 entries_.push_back(entry);
420 return *entry;
421}
422
423const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700424 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 RegType* cur_entry = entries_[i];
426 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
427 return *cur_entry;
428 }
429 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700430 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 entries_.size());
432 entries_.push_back(entry);
433 return *entry;
434}
435
436const RegType& RegTypeCache::FromType(RegType::Type type) {
437 CHECK(type < RegType::kRegTypeReference);
438 switch (type) {
439 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
440 case RegType::kRegTypeByte: return From(type, NULL, "B");
441 case RegType::kRegTypeShort: return From(type, NULL, "S");
442 case RegType::kRegTypeChar: return From(type, NULL, "C");
443 case RegType::kRegTypeInteger: return From(type, NULL, "I");
444 case RegType::kRegTypeFloat: return From(type, NULL, "F");
445 case RegType::kRegTypeLongLo:
446 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
447 case RegType::kRegTypeDoubleLo:
448 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
449 default: return From(type, NULL, "");
450 }
451}
452
453const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700454 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
455 RegType* cur_entry = entries_[i];
456 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
457 return *cur_entry;
458 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700460 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
461 entries_.push_back(entry);
462 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700463}
464
465bool RegisterLine::CheckConstructorReturn() const {
466 for (size_t i = 0; i < num_regs_; i++) {
467 if (GetRegisterType(i).IsUninitializedThisReference()) {
468 verifier_->Fail(VERIFY_ERROR_GENERIC)
469 << "Constructor returning without calling superclass constructor";
470 return false;
471 }
472 }
473 return true;
474}
475
476void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
477 DCHECK(vdst < num_regs_);
478 if (new_type.IsLowHalf()) {
479 line_[vdst] = new_type.GetId();
480 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
481 } else if (new_type.IsHighHalf()) {
482 /* should never set these explicitly */
483 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
484 } else if (new_type.IsConflict()) { // should only be set during a merge
485 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
486 } else {
487 line_[vdst] = new_type.GetId();
488 }
489 // Clear the monitor entry bits for this register.
490 ClearAllRegToLockDepths(vdst);
491}
492
493void RegisterLine::SetResultTypeToUnknown() {
494 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
495 result_[0] = unknown_id;
496 result_[1] = unknown_id;
497}
498
499void RegisterLine::SetResultRegisterType(const RegType& new_type) {
500 result_[0] = new_type.GetId();
501 if(new_type.IsLowHalf()) {
502 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
503 result_[1] = new_type.GetId() + 1;
504 } else {
505 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
506 }
507}
508
509const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
510 // The register index was validated during the static pass, so we don't need to check it here.
511 DCHECK_LT(vsrc, num_regs_);
512 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
513}
514
515const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
516 if (dec_insn.vA_ < 1) {
517 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
518 return verifier_->GetRegTypeCache()->Unknown();
519 }
520 /* get the element type of the array held in vsrc */
521 const RegType& this_type = GetRegisterType(dec_insn.vC_);
522 if (!this_type.IsReferenceTypes()) {
523 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
524 << dec_insn.vC_ << " (type=" << this_type << ")";
525 return verifier_->GetRegTypeCache()->Unknown();
526 }
527 return this_type;
528}
529
530Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
531 /* get the element type of the array held in vsrc */
532 const RegType& type = GetRegisterType(vsrc);
533 /* if "always zero", we allow it to fail at runtime */
534 if (type.IsZero()) {
535 return NULL;
536 } else if (!type.IsReferenceTypes()) {
537 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
538 << " (type=" << type << ")";
539 return NULL;
540 } else if (type.IsUninitializedReference()) {
541 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
542 return NULL;
543 } else {
544 return type.GetClass();
545 }
546}
547
548bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
549 // Verify the src register type against the check type refining the type of the register
550 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700551 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700552 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
553 << " but expected " << check_type;
554 return false;
555 }
556 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
557 // precise than the subtype in vsrc so leave it for reference types. For primitive types
558 // if they are a defined type then they are as precise as we can get, however, for constant
559 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
560 return true;
561}
562
563void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
564 Class* klass = uninit_type.GetClass();
565 if (klass == NULL) {
566 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Unable to find type=" << uninit_type;
567 } else {
568 const RegType& init_type = verifier_->GetRegTypeCache()->FromClass(klass);
569 size_t changed = 0;
570 for (size_t i = 0; i < num_regs_; i++) {
571 if (GetRegisterType(i).Equals(uninit_type)) {
572 line_[i] = init_type.GetId();
573 changed++;
574 }
575 }
576 DCHECK_GT(changed, 0u);
577 }
578}
579
580void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
581 for (size_t i = 0; i < num_regs_; i++) {
582 if (GetRegisterType(i).Equals(uninit_type)) {
583 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
584 ClearAllRegToLockDepths(i);
585 }
586 }
587}
588
589void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
590 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
591 const RegType& type = GetRegisterType(vsrc);
592 SetRegisterType(vdst, type);
593 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
594 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
595 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
596 << " cat=" << static_cast<int>(cat);
597 } else if (cat == kTypeCategoryRef) {
598 CopyRegToLockDepth(vdst, vsrc);
599 }
600}
601
602void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
603 const RegType& type_l = GetRegisterType(vsrc);
604 const RegType& type_h = GetRegisterType(vsrc + 1);
605
606 if (!type_l.CheckWidePair(type_h)) {
607 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
608 << " type=" << type_l << "/" << type_h;
609 } else {
610 SetRegisterType(vdst, type_l); // implicitly sets the second half
611 }
612}
613
614void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
615 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
616 if ((!is_reference && !type.IsCategory1Types()) ||
617 (is_reference && !type.IsReferenceTypes())) {
618 verifier_->Fail(VERIFY_ERROR_GENERIC)
619 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
620 } else {
621 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
622 SetRegisterType(vdst, type);
623 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
624 }
625}
626
627/*
628 * Implement "move-result-wide". Copy the category-2 value from the result
629 * register to another register, and reset the result register.
630 */
631void RegisterLine::CopyResultRegister2(uint32_t vdst) {
632 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
633 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
634 if (!type_l.IsCategory2Types()) {
635 verifier_->Fail(VERIFY_ERROR_GENERIC)
636 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
637 } else {
638 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
639 SetRegisterType(vdst, type_l); // also sets the high
640 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
641 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
642 }
643}
644
645void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
646 const RegType& dst_type, const RegType& src_type) {
647 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
648 SetRegisterType(dec_insn.vA_, dst_type);
649 }
650}
651
652void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
653 const RegType& dst_type,
654 const RegType& src_type1, const RegType& src_type2,
655 bool check_boolean_op) {
656 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
657 VerifyRegisterType(dec_insn.vC_, src_type2)) {
658 if (check_boolean_op) {
659 DCHECK(dst_type.IsInteger());
660 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
661 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
662 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
663 return;
664 }
665 }
666 SetRegisterType(dec_insn.vA_, dst_type);
667 }
668}
669
670void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
671 const RegType& dst_type, const RegType& src_type1,
672 const RegType& src_type2, bool check_boolean_op) {
673 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
674 VerifyRegisterType(dec_insn.vB_, src_type2)) {
675 if (check_boolean_op) {
676 DCHECK(dst_type.IsInteger());
677 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
678 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
679 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
680 return;
681 }
682 }
683 SetRegisterType(dec_insn.vA_, dst_type);
684 }
685}
686
687void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
688 const RegType& dst_type, const RegType& src_type,
689 bool check_boolean_op) {
690 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
691 if (check_boolean_op) {
692 DCHECK(dst_type.IsInteger());
693 /* check vB with the call, then check the constant manually */
694 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
695 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
696 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
697 return;
698 }
699 }
700 SetRegisterType(dec_insn.vA_, dst_type);
701 }
702}
703
704void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
705 const RegType& reg_type = GetRegisterType(reg_idx);
706 if (!reg_type.IsReferenceTypes()) {
707 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
708 } else {
709 SetRegToLockDepth(reg_idx, monitors_.size());
710 monitors_.push(insn_idx);
711 }
712}
713
714void RegisterLine::PopMonitor(uint32_t reg_idx) {
715 const RegType& reg_type = GetRegisterType(reg_idx);
716 if (!reg_type.IsReferenceTypes()) {
717 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
718 } else if (monitors_.empty()) {
719 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
720 } else {
721 monitors_.pop();
722 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
723 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
724 // format "036" the constant collector may create unlocks on the same object but referenced
725 // via different registers.
726 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
727 : verifier_->LogVerifyInfo())
728 << "monitor-exit not unlocking the top of the monitor stack";
729 } else {
730 // Record the register was unlocked
731 ClearRegToLockDepth(reg_idx, monitors_.size());
732 }
733 }
734}
735
736bool RegisterLine::VerifyMonitorStackEmpty() {
737 if (MonitorStackDepth() != 0) {
738 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
739 return false;
740 } else {
741 return true;
742 }
743}
744
745bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
746 bool changed = false;
747 for (size_t idx = 0; idx < num_regs_; idx++) {
748 if (line_[idx] != incoming_line->line_[idx]) {
749 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
750 const RegType& cur_type = GetRegisterType(idx);
751 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
752 changed = changed || !cur_type.Equals(new_type);
753 line_[idx] = new_type.GetId();
754 }
755 }
756 if(monitors_ != incoming_line->monitors_) {
757 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
758 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
759 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
760 for (uint32_t idx = 0; idx < num_regs_; idx++) {
761 size_t depths = reg_to_lock_depths_.count(idx);
762 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
763 if (depths != incoming_depths) {
764 if (depths == 0 || incoming_depths == 0) {
765 reg_to_lock_depths_.erase(idx);
766 } else {
767 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
768 << ": " << depths << " != " << incoming_depths;
769 break;
770 }
771 }
772 }
773 }
774 return changed;
775}
776
777void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
778 for (size_t i = 0; i < num_regs_; i += 8) {
779 uint8_t val = 0;
780 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
781 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700782 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 val |= 1 << j;
784 }
785 }
786 if (val != 0) {
787 DCHECK_LT(i / 8, max_bytes);
788 data[i / 8] = val;
789 }
790 }
791}
792
793std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700794 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 return os;
796}
797
798
799void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
800 uint32_t insns_size, uint16_t registers_size,
801 DexVerifier* verifier) {
802 DCHECK_GT(insns_size, 0U);
803
804 for (uint32_t i = 0; i < insns_size; i++) {
805 bool interesting = false;
806 switch (mode) {
807 case kTrackRegsAll:
808 interesting = flags[i].IsOpcode();
809 break;
810 case kTrackRegsGcPoints:
811 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
812 break;
813 case kTrackRegsBranches:
814 interesting = flags[i].IsBranchTarget();
815 break;
816 default:
817 break;
818 }
819 if (interesting) {
820 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
821 }
822 }
823}
824
825bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700826 if (klass->IsVerified()) {
827 return true;
828 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 Class* super = klass->GetSuperClass();
830 if (super == NULL && !klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
831 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
832 return false;
833 }
834 if (super != NULL) {
835 if (!super->IsVerified() && !super->IsErroneous()) {
836 Runtime::Current()->GetClassLinker()->VerifyClass(super);
837 }
838 if (!super->IsVerified()) {
839 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
840 << " that attempts to sub-class corrupt class " << PrettyClass(super);
841 return false;
842 } else if (super->IsFinal()) {
843 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
844 << " that attempts to sub-class final class " << PrettyClass(super);
845 return false;
846 }
847 }
jeffhaobdb76512011-09-07 11:43:16 -0700848 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
849 Method* method = klass->GetDirectMethod(i);
850 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
852 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700853 return false;
854 }
855 }
856 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
857 Method* method = klass->GetVirtualMethod(i);
858 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
860 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700861 return false;
862 }
863 }
864 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700865}
866
jeffhaobdb76512011-09-07 11:43:16 -0700867bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 DexVerifier verifier(method);
869 bool success = verifier.Verify();
870 // We expect either success and no verification error, or failure and a generic failure to
871 // reject the class.
872 if (success) {
873 if (verifier.failure_ != VERIFY_ERROR_NONE) {
874 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
875 << verifier.fail_messages_;
876 }
877 } else {
878 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
879 << verifier.fail_messages_.str() << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700880 if (gDebugVerify) {
881 verifier.Dump(std::cout);
882 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
884 }
885 return success;
886}
887
888DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
889 method_(method), failure_(VERIFY_ERROR_NONE),
890 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700891 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 dex_file_ = &class_linker->FindDexFile(dex_cache);
894 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700895}
896
Ian Rogersd81871c2011-10-03 13:57:23 -0700897bool DexVerifier::Verify() {
898 // If there aren't any instructions, make sure that's expected, then exit successfully.
899 if (code_item_ == NULL) {
900 if (!method_->IsNative() && !method_->IsAbstract()) {
901 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700902 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 } else {
904 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700905 }
jeffhaobdb76512011-09-07 11:43:16 -0700906 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
908 if (code_item_->ins_size_ > code_item_->registers_size_) {
909 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
910 << " regs=" << code_item_->registers_size_;
911 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700912 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 // Allocate and initialize an array to hold instruction data.
914 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
915 // Run through the instructions and see if the width checks out.
916 bool result = ComputeWidthsAndCountOps();
917 // Flag instructions guarded by a "try" block and check exception handlers.
918 result = result && ScanTryCatchBlocks();
919 // Perform static instruction verification.
920 result = result && VerifyInstructions();
921 // Perform code flow analysis.
922 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700923 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924}
925
Ian Rogersd81871c2011-10-03 13:57:23 -0700926bool DexVerifier::ComputeWidthsAndCountOps() {
927 const uint16_t* insns = code_item_->insns_;
928 size_t insns_size = code_item_->insns_size_in_code_units_;
929 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700930 size_t new_instance_count = 0;
931 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700933
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700935 Instruction::Code opcode = inst->Opcode();
936 if (opcode == Instruction::NEW_INSTANCE) {
937 new_instance_count++;
938 } else if (opcode == Instruction::MONITOR_ENTER) {
939 monitor_enter_count++;
940 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 size_t inst_size = inst->SizeInCodeUnits();
942 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
943 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700944 inst = inst->Next();
945 }
946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 if (dex_pc != insns_size) {
948 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
949 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700950 return false;
951 }
952
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 new_instance_count_ = new_instance_count;
954 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700955 return true;
956}
957
Ian Rogersd81871c2011-10-03 13:57:23 -0700958bool DexVerifier::ScanTryCatchBlocks() {
959 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700960 if (tries_size == 0) {
961 return true;
962 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 uint32_t insns_size = code_item_->insns_size_in_code_units_;
964 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700965
966 for (uint32_t idx = 0; idx < tries_size; idx++) {
967 const DexFile::TryItem* try_item = &tries[idx];
968 uint32_t start = try_item->start_addr_;
969 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700970 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
972 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700973 return false;
974 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 if (!insn_flags_[start].IsOpcode()) {
976 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700977 return false;
978 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 for (uint32_t dex_pc = start; dex_pc < end;
980 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
981 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700982 }
983 }
jeffhaobdb76512011-09-07 11:43:16 -0700984 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700986 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
987 for (uint32_t idx = 0; idx < handlers_size; idx++) {
988 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700989 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 uint32_t dex_pc= iterator.Get().address_;
991 if (!insn_flags_[dex_pc].IsOpcode()) {
992 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700993 return false;
994 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -0700996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997 handlers_ptr = iterator.GetData();
998 }
jeffhaobdb76512011-09-07 11:43:16 -0700999 return true;
1000}
1001
Ian Rogersd81871c2011-10-03 13:57:23 -07001002bool DexVerifier::VerifyInstructions() {
1003 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001004
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 /* Flag the start of the method as a branch target. */
1006 insn_flags_[0].SetBranchTarget();
1007
1008 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1009 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1010 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001011 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1012 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
1015 /* Flag instructions that are garbage collection points */
1016 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1017 insn_flags_[dex_pc].SetGcPoint();
1018 }
1019 dex_pc += inst->SizeInCodeUnits();
1020 inst = inst->Next();
1021 }
1022 return true;
1023}
1024
1025bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1026 Instruction::DecodedInstruction dec_insn(inst);
1027 bool result = true;
1028 switch (inst->GetVerifyTypeArgumentA()) {
1029 case Instruction::kVerifyRegA:
1030 result = result && CheckRegisterIndex(dec_insn.vA_);
1031 break;
1032 case Instruction::kVerifyRegAWide:
1033 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1034 break;
1035 }
1036 switch (inst->GetVerifyTypeArgumentB()) {
1037 case Instruction::kVerifyRegB:
1038 result = result && CheckRegisterIndex(dec_insn.vB_);
1039 break;
1040 case Instruction::kVerifyRegBField:
1041 result = result && CheckFieldIndex(dec_insn.vB_);
1042 break;
1043 case Instruction::kVerifyRegBMethod:
1044 result = result && CheckMethodIndex(dec_insn.vB_);
1045 break;
1046 case Instruction::kVerifyRegBNewInstance:
1047 result = result && CheckNewInstance(dec_insn.vB_);
1048 break;
1049 case Instruction::kVerifyRegBString:
1050 result = result && CheckStringIndex(dec_insn.vB_);
1051 break;
1052 case Instruction::kVerifyRegBType:
1053 result = result && CheckTypeIndex(dec_insn.vB_);
1054 break;
1055 case Instruction::kVerifyRegBWide:
1056 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1057 break;
1058 }
1059 switch (inst->GetVerifyTypeArgumentC()) {
1060 case Instruction::kVerifyRegC:
1061 result = result && CheckRegisterIndex(dec_insn.vC_);
1062 break;
1063 case Instruction::kVerifyRegCField:
1064 result = result && CheckFieldIndex(dec_insn.vC_);
1065 break;
1066 case Instruction::kVerifyRegCNewArray:
1067 result = result && CheckNewArray(dec_insn.vC_);
1068 break;
1069 case Instruction::kVerifyRegCType:
1070 result = result && CheckTypeIndex(dec_insn.vC_);
1071 break;
1072 case Instruction::kVerifyRegCWide:
1073 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1074 break;
1075 }
1076 switch (inst->GetVerifyExtraFlags()) {
1077 case Instruction::kVerifyArrayData:
1078 result = result && CheckArrayData(code_offset);
1079 break;
1080 case Instruction::kVerifyBranchTarget:
1081 result = result && CheckBranchTarget(code_offset);
1082 break;
1083 case Instruction::kVerifySwitchTargets:
1084 result = result && CheckSwitchTargets(code_offset);
1085 break;
1086 case Instruction::kVerifyVarArg:
1087 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1088 break;
1089 case Instruction::kVerifyVarArgRange:
1090 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1091 break;
1092 case Instruction::kVerifyError:
1093 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1094 result = false;
1095 break;
1096 }
1097 return result;
1098}
1099
1100bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1101 if (idx >= code_item_->registers_size_) {
1102 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1103 << code_item_->registers_size_ << ")";
1104 return false;
1105 }
1106 return true;
1107}
1108
1109bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1110 if (idx + 1 >= code_item_->registers_size_) {
1111 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1112 << "+1 >= " << code_item_->registers_size_ << ")";
1113 return false;
1114 }
1115 return true;
1116}
1117
1118bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1119 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1120 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1121 << dex_file_->GetHeader().field_ids_size_ << ")";
1122 return false;
1123 }
1124 return true;
1125}
1126
1127bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1128 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1129 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1130 << dex_file_->GetHeader().method_ids_size_ << ")";
1131 return false;
1132 }
1133 return true;
1134}
1135
1136bool DexVerifier::CheckNewInstance(uint32_t idx) {
1137 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1138 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1139 << dex_file_->GetHeader().type_ids_size_ << ")";
1140 return false;
1141 }
1142 // We don't need the actual class, just a pointer to the class name.
1143 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1144 if (descriptor[0] != 'L') {
1145 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1146 return false;
1147 }
1148 return true;
1149}
1150
1151bool DexVerifier::CheckStringIndex(uint32_t idx) {
1152 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1153 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1154 << dex_file_->GetHeader().string_ids_size_ << ")";
1155 return false;
1156 }
1157 return true;
1158}
1159
1160bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1161 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1162 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1163 << dex_file_->GetHeader().type_ids_size_ << ")";
1164 return false;
1165 }
1166 return true;
1167}
1168
1169bool DexVerifier::CheckNewArray(uint32_t idx) {
1170 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1171 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1172 << dex_file_->GetHeader().type_ids_size_ << ")";
1173 return false;
1174 }
1175 int bracket_count = 0;
1176 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1177 const char* cp = descriptor;
1178 while (*cp++ == '[') {
1179 bracket_count++;
1180 }
1181 if (bracket_count == 0) {
1182 /* The given class must be an array type. */
1183 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1184 return false;
1185 } else if (bracket_count > 255) {
1186 /* It is illegal to create an array of more than 255 dimensions. */
1187 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1188 return false;
1189 }
1190 return true;
1191}
1192
1193bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1194 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1195 const uint16_t* insns = code_item_->insns_ + cur_offset;
1196 const uint16_t* array_data;
1197 int32_t array_data_offset;
1198
1199 DCHECK_LT(cur_offset, insn_count);
1200 /* make sure the start of the array data table is in range */
1201 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1202 if ((int32_t) cur_offset + array_data_offset < 0 ||
1203 cur_offset + array_data_offset + 2 >= insn_count) {
1204 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1205 << ", data offset " << array_data_offset << ", count " << insn_count;
1206 return false;
1207 }
1208 /* offset to array data table is a relative branch-style offset */
1209 array_data = insns + array_data_offset;
1210 /* make sure the table is 32-bit aligned */
1211 if ((((uint32_t) array_data) & 0x03) != 0) {
1212 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1213 << ", data offset " << array_data_offset;
1214 return false;
1215 }
1216 uint32_t value_width = array_data[1];
1217 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1218 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1219 /* make sure the end of the switch is in range */
1220 if (cur_offset + array_data_offset + table_size > insn_count) {
1221 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1222 << ", data offset " << array_data_offset << ", end "
1223 << cur_offset + array_data_offset + table_size
1224 << ", count " << insn_count;
1225 return false;
1226 }
1227 return true;
1228}
1229
1230bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1231 int32_t offset;
1232 bool isConditional, selfOkay;
1233 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1234 return false;
1235 }
1236 if (!selfOkay && offset == 0) {
1237 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1238 return false;
1239 }
1240 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1241 // identical "wrap-around" behavior, but it's unwise to depend on that.
1242 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1243 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1244 return false;
1245 }
1246 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1247 int32_t abs_offset = cur_offset + offset;
1248 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1249 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1250 << (void*) abs_offset << ") at " << (void*) cur_offset;
1251 return false;
1252 }
1253 insn_flags_[abs_offset].SetBranchTarget();
1254 return true;
1255}
1256
1257bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1258 bool* selfOkay) {
1259 const uint16_t* insns = code_item_->insns_ + cur_offset;
1260 *pConditional = false;
1261 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 switch (*insns & 0xff) {
1263 case Instruction::GOTO:
1264 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 break;
1266 case Instruction::GOTO_32:
1267 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 *selfOkay = true;
1269 break;
1270 case Instruction::GOTO_16:
1271 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001272 break;
1273 case Instruction::IF_EQ:
1274 case Instruction::IF_NE:
1275 case Instruction::IF_LT:
1276 case Instruction::IF_GE:
1277 case Instruction::IF_GT:
1278 case Instruction::IF_LE:
1279 case Instruction::IF_EQZ:
1280 case Instruction::IF_NEZ:
1281 case Instruction::IF_LTZ:
1282 case Instruction::IF_GEZ:
1283 case Instruction::IF_GTZ:
1284 case Instruction::IF_LEZ:
1285 *pOffset = (int16_t) insns[1];
1286 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001287 break;
1288 default:
1289 return false;
1290 break;
1291 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 return true;
1293}
1294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1296 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001297 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001299 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1301 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1302 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1303 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001304 return false;
1305 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001306 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 /* make sure the table is 32-bit aligned */
1309 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1311 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 return false;
1313 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 uint32_t switch_count = switch_insns[1];
1315 int32_t keys_offset, targets_offset;
1316 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1318 /* 0=sig, 1=count, 2/3=firstKey */
1319 targets_offset = 4;
1320 keys_offset = -1;
1321 expected_signature = Instruction::kPackedSwitchSignature;
1322 } else {
1323 /* 0=sig, 1=count, 2..count*2 = keys */
1324 keys_offset = 2;
1325 targets_offset = 2 + 2 * switch_count;
1326 expected_signature = Instruction::kSparseSwitchSignature;
1327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1331 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001332 return false;
1333 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 /* make sure the end of the switch is in range */
1335 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1337 << switch_offset << ", end "
1338 << (cur_offset + switch_offset + table_size)
1339 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 return false;
1341 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 /* for a sparse switch, verify the keys are in ascending order */
1343 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1345 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1347 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1348 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1350 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001351 return false;
1352 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 last_key = key;
1354 }
1355 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001356 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 for (uint32_t targ = 0; targ < switch_count; targ++) {
1358 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1359 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1360 int32_t abs_offset = cur_offset + offset;
1361 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1362 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1363 << (void*) abs_offset << ") at "
1364 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 return false;
1366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 insn_flags_[abs_offset].SetBranchTarget();
1368 }
1369 return true;
1370}
1371
1372bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1373 if (vA > 5) {
1374 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1375 return false;
1376 }
1377 uint16_t registers_size = code_item_->registers_size_;
1378 for (uint32_t idx = 0; idx < vA; idx++) {
1379 if (arg[idx] > registers_size) {
1380 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1381 << ") in non-range invoke (> " << registers_size << ")";
1382 return false;
1383 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001384 }
1385
1386 return true;
1387}
1388
Ian Rogersd81871c2011-10-03 13:57:23 -07001389bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1390 uint16_t registers_size = code_item_->registers_size_;
1391 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1392 // integer overflow when adding them here.
1393 if (vA + vC > registers_size) {
1394 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1395 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001396 return false;
1397 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001398 return true;
1399}
1400
Ian Rogersd81871c2011-10-03 13:57:23 -07001401bool DexVerifier::VerifyCodeFlow() {
1402 uint16_t registers_size = code_item_->registers_size_;
1403 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001404
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 if (registers_size * insns_size > 4*1024*1024) {
1406 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1407 << " insns_size=" << insns_size << ")";
1408 }
1409 /* Create and initialize table holding register status */
1410 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1411 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001412
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 work_line_.reset(new RegisterLine(registers_size, this));
1414 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001415
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 /* Initialize register types of method arguments. */
1417 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001418 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1419 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 return false;
1421 }
1422 /* Perform code flow verification. */
1423 if (!CodeFlowVerifyMethod()) {
1424 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001425 }
1426
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 /* Generate a register map and add it to the method. */
1428 ByteArray* map = GenerateGcMap();
1429 if (map == NULL) {
1430 return false; // Not a real failure, but a failure to encode
1431 }
1432 method_->SetGcMap(map);
1433#ifndef NDEBUG
1434 VerifyGcMap();
1435#endif
jeffhaobdb76512011-09-07 11:43:16 -07001436 return true;
1437}
1438
Ian Rogersd81871c2011-10-03 13:57:23 -07001439void DexVerifier::Dump(std::ostream& os) {
1440 if (method_->IsNative()) {
1441 os << "Native method" << std::endl;
1442 return;
jeffhaobdb76512011-09-07 11:43:16 -07001443 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 DCHECK(code_item_ != NULL);
1445 const Instruction* inst = Instruction::At(code_item_->insns_);
1446 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1447 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001448 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1449 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1451 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001452 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001453 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001454 inst = inst->Next();
1455 }
jeffhaobdb76512011-09-07 11:43:16 -07001456}
1457
Ian Rogersd81871c2011-10-03 13:57:23 -07001458static bool IsPrimitiveDescriptor(char descriptor) {
1459 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001460 case 'I':
1461 case 'C':
1462 case 'S':
1463 case 'B':
1464 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001465 case 'F':
1466 case 'D':
1467 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001469 default:
1470 return false;
1471 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001472}
1473
Ian Rogersd81871c2011-10-03 13:57:23 -07001474bool DexVerifier::SetTypesFromSignature() {
1475 RegisterLine* reg_line = reg_table_.GetLine(0);
1476 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1477 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001478
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1480 //Include the "this" pointer.
1481 size_t cur_arg = 0;
1482 if (!method_->IsStatic()) {
1483 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1484 // argument as uninitialized. This restricts field access until the superclass constructor is
1485 // called.
1486 Class* declaring_class = method_->GetDeclaringClass();
1487 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1488 reg_line->SetRegisterType(arg_start + cur_arg,
1489 reg_types_.UninitializedThisArgument(declaring_class));
1490 } else {
1491 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001492 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001494 }
1495
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1497 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1498
1499 for (; iterator.HasNext(); iterator.Next()) {
1500 const char* descriptor = iterator.GetDescriptor();
1501 if (descriptor == NULL) {
1502 LOG(FATAL) << "Null descriptor";
1503 }
1504 if (cur_arg >= expected_args) {
1505 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1506 << " args, found more (" << descriptor << ")";
1507 return false;
1508 }
1509 switch (descriptor[0]) {
1510 case 'L':
1511 case '[':
1512 // We assume that reference arguments are initialized. The only way it could be otherwise
1513 // (assuming the caller was verified) is if the current method is <init>, but in that case
1514 // it's effectively considered initialized the instant we reach here (in the sense that we
1515 // can return without doing anything or call virtual methods).
1516 {
1517 const RegType& reg_type =
1518 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001519 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 }
1521 break;
1522 case 'Z':
1523 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1524 break;
1525 case 'C':
1526 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1527 break;
1528 case 'B':
1529 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1530 break;
1531 case 'I':
1532 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1533 break;
1534 case 'S':
1535 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1536 break;
1537 case 'F':
1538 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1539 break;
1540 case 'J':
1541 case 'D': {
1542 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1543 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1544 cur_arg++;
1545 break;
1546 }
1547 default:
1548 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1549 return false;
1550 }
1551 cur_arg++;
1552 }
1553 if (cur_arg != expected_args) {
1554 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1555 return false;
1556 }
1557 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1558 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1559 // format. Only major difference from the method argument format is that 'V' is supported.
1560 bool result;
1561 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1562 result = descriptor[1] == '\0';
1563 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1564 size_t i = 0;
1565 do {
1566 i++;
1567 } while (descriptor[i] == '['); // process leading [
1568 if (descriptor[i] == 'L') { // object array
1569 do {
1570 i++; // find closing ;
1571 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1572 result = descriptor[i] == ';';
1573 } else { // primitive array
1574 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1575 }
1576 } else if (descriptor[0] == 'L') {
1577 // could be more thorough here, but shouldn't be required
1578 size_t i = 0;
1579 do {
1580 i++;
1581 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1582 result = descriptor[i] == ';';
1583 } else {
1584 result = false;
1585 }
1586 if (!result) {
1587 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1588 << descriptor << "'";
1589 }
1590 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001591}
1592
Ian Rogersd81871c2011-10-03 13:57:23 -07001593bool DexVerifier::CodeFlowVerifyMethod() {
1594 const uint16_t* insns = code_item_->insns_;
1595 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001596
jeffhaobdb76512011-09-07 11:43:16 -07001597 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 insn_flags_[0].SetChanged();
1599 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001600
jeffhaobdb76512011-09-07 11:43:16 -07001601 /* Continue until no instructions are marked "changed". */
1602 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1604 uint32_t insn_idx = start_guess;
1605 for (; insn_idx < insns_size; insn_idx++) {
1606 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
1608 }
jeffhaobdb76512011-09-07 11:43:16 -07001609 if (insn_idx == insns_size) {
1610 if (start_guess != 0) {
1611 /* try again, starting from the top */
1612 start_guess = 0;
1613 continue;
1614 } else {
1615 /* all flags are clear */
1616 break;
1617 }
1618 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 // We carry the working set of registers from instruction to instruction. If this address can
1620 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1621 // "changed" flags, we need to load the set of registers from the table.
1622 // Because we always prefer to continue on to the next instruction, we should never have a
1623 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1624 // target.
1625 work_insn_idx_ = insn_idx;
1626 if (insn_flags_[insn_idx].IsBranchTarget()) {
1627 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001628 } else {
1629#ifndef NDEBUG
1630 /*
1631 * Sanity check: retrieve the stored register line (assuming
1632 * a full table) and make sure it actually matches.
1633 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1635 if (register_line != NULL) {
1636 if (work_line_->CompareLine(register_line) != 0) {
1637 Dump(std::cout);
1638 std::cout << info_messages_.str();
1639 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1640 << "@" << (void*)work_insn_idx_ << std::endl
1641 << " work_line=" << *work_line_ << std::endl
1642 << " expected=" << *register_line;
1643 }
jeffhaobdb76512011-09-07 11:43:16 -07001644 }
1645#endif
1646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 if (!CodeFlowVerifyInstruction(&start_guess)) {
1648 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001649 return false;
1650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 insn_flags_[insn_idx].SetVisited();
1653 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001654 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001655
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001657 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001658 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001659 * (besides the wasted space), but it indicates a flaw somewhere
1660 * down the line, possibly in the verifier.
1661 *
1662 * If we've substituted "always throw" instructions into the stream,
1663 * we are almost certainly going to have some dead code.
1664 */
1665 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 uint32_t insn_idx = 0;
1667 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001668 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001670 * may or may not be preceded by a padding NOP (for alignment).
1671 */
1672 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1673 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1674 insns[insn_idx] == Instruction::kArrayDataSignature ||
1675 (insns[insn_idx] == Instruction::NOP &&
1676 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1677 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1678 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
1681
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001683 if (dead_start < 0)
1684 dead_start = insn_idx;
1685 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001687 dead_start = -1;
1688 }
1689 }
1690 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001692 }
1693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 return true;
1695}
1696
Ian Rogersd81871c2011-10-03 13:57:23 -07001697bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001698#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001700 gDvm.verifierStats.instrsReexamined++;
1701 } else {
1702 gDvm.verifierStats.instrsExamined++;
1703 }
1704#endif
1705
1706 /*
1707 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001708 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001709 * control to another statement:
1710 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001711 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001712 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001713 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001714 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001715 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001716 * throw an exception that is handled by an encompassing "try"
1717 * block.
1718 *
1719 * We can also return, in which case there is no successor instruction
1720 * from this point.
1721 *
1722 * The behavior can be determined from the OpcodeFlags.
1723 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1725 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001726 Instruction::DecodedInstruction dec_insn(inst);
1727 int opcode_flag = inst->Flag();
1728
jeffhaobdb76512011-09-07 11:43:16 -07001729 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001730 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001731 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 // Generate processing back trace to debug verifier
Ian Rogers2c8a8572011-10-24 17:11:36 -07001733 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl << *work_line_.get();
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 }
jeffhaobdb76512011-09-07 11:43:16 -07001735
1736 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001737 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001738 * can throw an exception, we will copy/merge this into the "catch"
1739 * address rather than work_line, because we don't want the result
1740 * from the "successful" code path (e.g. a check-cast that "improves"
1741 * a type) to be visible to the exception handler.
1742 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1744 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001745 } else {
1746#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001748#endif
1749 }
1750
1751 switch (dec_insn.opcode_) {
1752 case Instruction::NOP:
1753 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001754 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001755 * a signature that looks like a NOP; if we see one of these in
1756 * the course of executing code then we have a problem.
1757 */
1758 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001760 }
1761 break;
1762
1763 case Instruction::MOVE:
1764 case Instruction::MOVE_FROM16:
1765 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
1768 case Instruction::MOVE_WIDE:
1769 case Instruction::MOVE_WIDE_FROM16:
1770 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773 case Instruction::MOVE_OBJECT:
1774 case Instruction::MOVE_OBJECT_FROM16:
1775 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778
1779 /*
1780 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001781 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001782 * might want to hold the result in an actual CPU register, so the
1783 * Dalvik spec requires that these only appear immediately after an
1784 * invoke or filled-new-array.
1785 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001786 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001787 * redundant with the reset done below, but it can make the debug info
1788 * easier to read in some cases.)
1789 */
1790 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
1793 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
1799
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001801 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 * This statement can only appear as the first instruction in an exception handler (though not
1803 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001804 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001805 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001807 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001809 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001811 }
1812 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 }
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1816 if (!GetMethodReturnType().IsUnknown()) {
1817 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1818 }
jeffhaobdb76512011-09-07 11:43:16 -07001819 }
1820 break;
1821 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001823 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 const RegType& return_type = GetMethodReturnType();
1825 if (!return_type.IsCategory1Types()) {
1826 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1827 } else {
1828 // Compilers may generate synthetic functions that write byte values into boolean fields.
1829 // Also, it may use integer values for boolean, byte, short, and character return types.
1830 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1831 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1832 ((return_type.IsBoolean() || return_type.IsByte() ||
1833 return_type.IsShort() || return_type.IsChar()) &&
1834 src_type.IsInteger()));
1835 /* check the register contents */
1836 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1837 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001838 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 }
jeffhaobdb76512011-09-07 11:43:16 -07001840 }
1841 }
1842 break;
1843 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001845 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 const RegType& return_type = GetMethodReturnType();
1847 if (!return_type.IsCategory2Types()) {
1848 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1849 } else {
1850 /* check the register contents */
1851 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1852 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001853 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 }
jeffhaobdb76512011-09-07 11:43:16 -07001855 }
1856 }
1857 break;
1858 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1860 const RegType& return_type = GetMethodReturnType();
1861 if (!return_type.IsReferenceTypes()) {
1862 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1863 } else {
1864 /* return_type is the *expected* return type, not register value */
1865 DCHECK(!return_type.IsZero());
1866 DCHECK(!return_type.IsUninitializedReference());
1867 // Verify that the reference in vAA is an instance of the type in "return_type". The Zero
1868 // type is allowed here. If the method is declared to return an interface, then any
1869 // initialized reference is acceptable.
1870 // Note GetClassFromRegister fails if the register holds an uninitialized reference, so
1871 // we do not allow them to be returned.
1872 Class* decl_class = return_type.GetClass();
1873 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
1874 if (res_class != NULL && failure_ == VERIFY_ERROR_NONE) {
1875 if (!decl_class->IsInterface() && !decl_class->IsAssignableFrom(res_class)) {
1876 Fail(VERIFY_ERROR_GENERIC) << "returning " << PrettyClassAndClassLoader(res_class)
1877 << ", declared " << PrettyClassAndClassLoader(res_class);
1878 }
jeffhaobdb76512011-09-07 11:43:16 -07001879 }
1880 }
1881 }
1882 break;
1883
1884 case Instruction::CONST_4:
1885 case Instruction::CONST_16:
1886 case Instruction::CONST:
1887 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 case Instruction::CONST_HIGH16:
1891 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 work_line_->SetRegisterType(dec_insn.vA_,
1893 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
1895 case Instruction::CONST_WIDE_16:
1896 case Instruction::CONST_WIDE_32:
1897 case Instruction::CONST_WIDE:
1898 case Instruction::CONST_WIDE_HIGH16:
1899 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902 case Instruction::CONST_STRING:
1903 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001905 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001907 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001909 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001911 fail_messages_ << "unable to resolve const-class " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 << " (" << bad_class_desc << ") in "
1913 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1914 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001915 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001917 }
1918 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 }
jeffhaobdb76512011-09-07 11:43:16 -07001920 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
1923 case Instruction::MONITOR_EXIT:
1924 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001925 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001926 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001927 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001928 * to the need to handle asynchronous exceptions, a now-deprecated
1929 * feature that Dalvik doesn't support.)
1930 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001932 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001933 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001934 * structured locking checks are working, the former would have
1935 * failed on the -enter instruction, and the latter is impossible.
1936 *
1937 * This is fortunate, because issue 3221411 prevents us from
1938 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001939 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001940 * some catch blocks (which will show up as "dead" code when
1941 * we skip them here); if we can't, then the code path could be
1942 * "live" so we still need to check it.
1943 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 opcode_flag &= ~Instruction::kThrow;
1945 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
1947
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001949 /*
1950 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001951 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001952 * we don't try to address it.)
1953 *
1954 * If it fails, an exception is thrown, which we deal with later
1955 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1956 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001958 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001960 fail_messages_ << "unable to resolve check-cast " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 << " (" << bad_class_desc << ") in "
1962 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1963 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001964 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1966 if (!orig_type.IsReferenceTypes()) {
1967 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1968 } else {
1969 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001970 }
jeffhaobdb76512011-09-07 11:43:16 -07001971 }
1972 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 }
1974 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001975 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1977 if (!tmp_type.IsReferenceTypes()) {
1978 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001979 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 /* make sure we can resolve the class; access check is important */
1981 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1982 if (res_class == NULL) {
1983 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001984 fail_messages_ << "unable to resolve instance of " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 << " (" << bad_class_desc << ") in "
1986 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1987 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1988 } else {
1989 /* result is boolean */
1990 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001993 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 }
1995 case Instruction::ARRAY_LENGTH: {
1996 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
1997 if (failure_ == VERIFY_ERROR_NONE) {
1998 if (res_class != NULL && !res_class->IsArrayClass()) {
1999 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
2000 } else {
2001 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2002 }
2003 }
2004 break;
2005 }
2006 case Instruction::NEW_INSTANCE: {
2007 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002008 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002010 fail_messages_ << "unable to resolve new-instance " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 << " (" << bad_class_desc << ") in "
2012 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2013 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2014 } else {
2015 /* can't create an instance of an interface or abstract class */
2016 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2017 Fail(VERIFY_ERROR_INSTANTIATION)
2018 << "new-instance on primitive, interface or abstract class"
2019 << PrettyDescriptor(res_class->GetDescriptor());
2020 } else {
2021 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2022 // Any registers holding previous allocations from this address that have not yet been
2023 // initialized must be marked invalid.
2024 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2025
2026 /* add the new uninitialized reference to the register state */
2027 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2028 }
2029 }
2030 break;
2031 }
2032 case Instruction::NEW_ARRAY: {
2033 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2034 if (res_class == NULL) {
2035 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002036 fail_messages_ << "unable to resolve new-array " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 << " (" << bad_class_desc << ") in "
2038 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2039 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002040 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002042 } else {
2043 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002045 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002047 }
2048 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 }
jeffhaobdb76512011-09-07 11:43:16 -07002050 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2052 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002053 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002055 fail_messages_ << "unable to resolve filled-array " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 << " (" << bad_class_desc << ") in "
2057 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2058 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002059 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002061 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002062 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002063 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002065 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002067 just_set_result = true;
2068 }
2069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
jeffhaobdb76512011-09-07 11:43:16 -07002071 case Instruction::CMPL_FLOAT:
2072 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2074 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2075 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::CMPL_DOUBLE:
2078 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2080 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2081 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2085 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2086 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 case Instruction::THROW: {
2089 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2090 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2091 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2092 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2093 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002094 }
2095 }
2096 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 }
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::GOTO:
2099 case Instruction::GOTO_16:
2100 case Instruction::GOTO_32:
2101 /* no effect on or use of registers */
2102 break;
2103
2104 case Instruction::PACKED_SWITCH:
2105 case Instruction::SPARSE_SWITCH:
2106 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 case Instruction::FILL_ARRAY_DATA: {
2111 /* Similar to the verification done for APUT */
2112 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2113 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002114 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 if (res_class != NULL) {
2116 Class* component_type = res_class->GetComponentType();
2117 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2118 component_type->IsPrimitiveVoid()) {
2119 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2120 << PrettyDescriptor(res_class->GetDescriptor());
2121 } else {
2122 const RegType& value_type = reg_types_.FromClass(component_type);
2123 DCHECK(!value_type.IsUnknown());
2124 // Now verify if the element width in the table matches the element width declared in
2125 // the array
2126 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2127 if (array_data[0] != Instruction::kArrayDataSignature) {
2128 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2129 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002130 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 // Since we don't compress the data in Dex, expect to see equal width of data stored
2132 // in the table and expected from the array class.
2133 if (array_data[1] != elem_width) {
2134 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2135 << " vs " << elem_width << ")";
2136 }
2137 }
2138 }
jeffhaobdb76512011-09-07 11:43:16 -07002139 }
2140 }
2141 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 }
jeffhaobdb76512011-09-07 11:43:16 -07002143 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 case Instruction::IF_NE: {
2145 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2146 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2147 bool mismatch = false;
2148 if (reg_type1.IsZero()) { // zero then integral or reference expected
2149 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2150 } else if (reg_type1.IsReferenceTypes()) { // both references?
2151 mismatch = !reg_type2.IsReferenceTypes();
2152 } else { // both integral?
2153 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2154 }
2155 if (mismatch) {
2156 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2157 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002158 }
2159 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 }
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::IF_LT:
2162 case Instruction::IF_GE:
2163 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::IF_LE: {
2165 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2166 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2167 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2168 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2169 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002170 }
2171 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 }
jeffhaobdb76512011-09-07 11:43:16 -07002173 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 case Instruction::IF_NEZ: {
2175 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2176 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2177 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 }
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::IF_LTZ:
2182 case Instruction::IF_GEZ:
2183 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 case Instruction::IF_LEZ: {
2185 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2186 if (!reg_type.IsIntegralTypes()) {
2187 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2188 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2189 }
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 }
jeffhaobdb76512011-09-07 11:43:16 -07002192 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2194 break;
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2197 break;
jeffhaobdb76512011-09-07 11:43:16 -07002198 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 VerifyAGet(dec_insn, reg_types_.Char(), true);
2200 break;
jeffhaobdb76512011-09-07 11:43:16 -07002201 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 case Instruction::AGET:
2205 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2206 break;
jeffhaobdb76512011-09-07 11:43:16 -07002207 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 VerifyAGet(dec_insn, reg_types_.Long(), true);
2209 break;
2210 case Instruction::AGET_OBJECT:
2211 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 case Instruction::APUT_BOOLEAN:
2215 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2216 break;
2217 case Instruction::APUT_BYTE:
2218 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2219 break;
2220 case Instruction::APUT_CHAR:
2221 VerifyAPut(dec_insn, reg_types_.Char(), true);
2222 break;
2223 case Instruction::APUT_SHORT:
2224 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002228 break;
2229 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
2232 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
2235
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::IGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 VerifyIGet(dec_insn, reg_types_.Boolean(), true);
2238 break;
jeffhaobdb76512011-09-07 11:43:16 -07002239 case Instruction::IGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 VerifyIGet(dec_insn, reg_types_.Byte(), true);
2241 break;
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::IGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 VerifyIGet(dec_insn, reg_types_.Char(), true);
2244 break;
jeffhaobdb76512011-09-07 11:43:16 -07002245 case Instruction::IGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 VerifyIGet(dec_insn, reg_types_.Short(), true);
2247 break;
2248 case Instruction::IGET:
2249 VerifyIGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002250 break;
2251 case Instruction::IGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 VerifyIGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002253 break;
2254 case Instruction::IGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 VerifyIGet(dec_insn, reg_types_.JavaLangObject(), false);
2256 break;
jeffhaobdb76512011-09-07 11:43:16 -07002257
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 case Instruction::IPUT_BOOLEAN:
2259 VerifyIPut(dec_insn, reg_types_.Boolean(), true);
2260 break;
2261 case Instruction::IPUT_BYTE:
2262 VerifyIPut(dec_insn, reg_types_.Byte(), true);
2263 break;
2264 case Instruction::IPUT_CHAR:
2265 VerifyIPut(dec_insn, reg_types_.Char(), true);
2266 break;
2267 case Instruction::IPUT_SHORT:
2268 VerifyIPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::IPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 VerifyIPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::IPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 VerifyIPut(dec_insn, reg_types_.Long(), true);
2275 break;
jeffhaobdb76512011-09-07 11:43:16 -07002276 case Instruction::IPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 VerifyIPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
2279
jeffhaobdb76512011-09-07 11:43:16 -07002280 case Instruction::SGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 VerifySGet(dec_insn, reg_types_.Boolean(), true);
2282 break;
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::SGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 VerifySGet(dec_insn, reg_types_.Byte(), true);
2285 break;
jeffhaobdb76512011-09-07 11:43:16 -07002286 case Instruction::SGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 VerifySGet(dec_insn, reg_types_.Char(), true);
2288 break;
jeffhaobdb76512011-09-07 11:43:16 -07002289 case Instruction::SGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 VerifySGet(dec_insn, reg_types_.Short(), true);
2291 break;
2292 case Instruction::SGET:
2293 VerifySGet(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::SGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 VerifySGet(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::SGET_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 VerifySGet(dec_insn, reg_types_.JavaLangObject(), false);
2300 break;
2301
2302 case Instruction::SPUT_BOOLEAN:
2303 VerifySPut(dec_insn, reg_types_.Boolean(), true);
2304 break;
2305 case Instruction::SPUT_BYTE:
2306 VerifySPut(dec_insn, reg_types_.Byte(), true);
2307 break;
2308 case Instruction::SPUT_CHAR:
2309 VerifySPut(dec_insn, reg_types_.Char(), true);
2310 break;
2311 case Instruction::SPUT_SHORT:
2312 VerifySPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::SPUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 VerifySPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::SPUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 VerifySPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::SPUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 VerifySPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323
2324 case Instruction::INVOKE_VIRTUAL:
2325 case Instruction::INVOKE_VIRTUAL_RANGE:
2326 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 case Instruction::INVOKE_SUPER_RANGE: {
2328 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2329 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2330 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2331 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2332 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2333 if (failure_ == VERIFY_ERROR_NONE) {
2334 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2335 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002336 just_set_result = true;
2337 }
2338 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 }
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 case Instruction::INVOKE_DIRECT_RANGE: {
2342 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2343 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2344 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002345 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 * Some additional checks when calling a constructor. We know from the invocation arg check
2347 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2348 * that to require that called_method->klass is the same as this->klass or this->super,
2349 * allowing the latter only if the "this" argument is the same as the "this" argument to
2350 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002351 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 if (called_method->IsConstructor()) {
2353 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2354 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356
2357 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 if (this_type.IsZero()) {
2359 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002363 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002364
2365 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2367 if (this_class != method_->GetDeclaringClass()) {
2368 Fail(VERIFY_ERROR_GENERIC)
2369 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 }
2372 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 }
2376
2377 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002378 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2380 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 }
2383
2384 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002385 * Replace the uninitialized reference with an initialized one. We need to do this for all
2386 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002387 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 work_line_->MarkRefsAsInitialized(this_type);
2389 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002391 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2393 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002394 just_set_result = true;
2395 }
2396 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 }
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 case Instruction::INVOKE_STATIC_RANGE: {
2400 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2401 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2402 if (failure_ == VERIFY_ERROR_NONE) {
2403 const RegType& return_type = reg_types_.FromClass(called_method->GetReturnType());
2404 work_line_->SetResultRegisterType(return_type);
2405 just_set_result = true;
2406 }
jeffhaobdb76512011-09-07 11:43:16 -07002407 }
2408 break;
2409 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 case Instruction::INVOKE_INTERFACE_RANGE: {
2411 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2412 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2413 if (failure_ == VERIFY_ERROR_NONE) {
2414 Class* called_interface = abs_method->GetDeclaringClass();
2415 if (!called_interface->IsInterface()) {
2416 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2417 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
jeffhaobdb76512011-09-07 11:43:16 -07002419 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 /* Get the type of the "this" arg, which should either be a sub-interface of called
2421 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002422 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2424 if (failure_ == VERIFY_ERROR_NONE) {
2425 if (this_type.IsZero()) {
2426 /* null pointer always passes (and always fails at runtime) */
2427 } else {
2428 Class* this_class = this_type.GetClass();
2429 if (this_type.IsUninitializedReference() || this_class == NULL) {
2430 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized";
2431 break;
2432 }
2433 if (!this_class->IsObjectClass() && !called_interface->IsAssignableFrom(this_class)) {
2434 Fail(VERIFY_ERROR_GENERIC) << "unable to match abstract method '"
2435 << PrettyMethod(abs_method) << "' with "
2436 << PrettyDescriptor(this_class->GetDescriptor())
2437 << " interfaces";
2438 break;
2439 }
2440 }
jeffhaobdb76512011-09-07 11:43:16 -07002441 }
2442 }
jeffhaobdb76512011-09-07 11:43:16 -07002443 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 * We don't have an object instance, so we can't find the concrete method. However, all of
2445 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002446 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 const RegType& return_type = reg_types_.FromClass(abs_method->GetReturnType());
2448 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002449 just_set_result = true;
2450 }
2451 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 }
jeffhaobdb76512011-09-07 11:43:16 -07002453 case Instruction::NEG_INT:
2454 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::NEG_LONG:
2458 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002463 break;
2464 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512
2513 case Instruction::ADD_INT:
2514 case Instruction::SUB_INT:
2515 case Instruction::MUL_INT:
2516 case Instruction::REM_INT:
2517 case Instruction::DIV_INT:
2518 case Instruction::SHL_INT:
2519 case Instruction::SHR_INT:
2520 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::AND_INT:
2524 case Instruction::OR_INT:
2525 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::ADD_LONG:
2529 case Instruction::SUB_LONG:
2530 case Instruction::MUL_LONG:
2531 case Instruction::DIV_LONG:
2532 case Instruction::REM_LONG:
2533 case Instruction::AND_LONG:
2534 case Instruction::OR_LONG:
2535 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::SHL_LONG:
2539 case Instruction::SHR_LONG:
2540 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 /* shift distance is Int, making these different from other binary operations */
2542 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::ADD_FLOAT:
2545 case Instruction::SUB_FLOAT:
2546 case Instruction::MUL_FLOAT:
2547 case Instruction::DIV_FLOAT:
2548 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 case Instruction::ADD_DOUBLE:
2552 case Instruction::SUB_DOUBLE:
2553 case Instruction::MUL_DOUBLE:
2554 case Instruction::DIV_DOUBLE:
2555 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::ADD_INT_2ADDR:
2559 case Instruction::SUB_INT_2ADDR:
2560 case Instruction::MUL_INT_2ADDR:
2561 case Instruction::REM_INT_2ADDR:
2562 case Instruction::SHL_INT_2ADDR:
2563 case Instruction::SHR_INT_2ADDR:
2564 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::AND_INT_2ADDR:
2568 case Instruction::OR_INT_2ADDR:
2569 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::ADD_LONG_2ADDR:
2576 case Instruction::SUB_LONG_2ADDR:
2577 case Instruction::MUL_LONG_2ADDR:
2578 case Instruction::DIV_LONG_2ADDR:
2579 case Instruction::REM_LONG_2ADDR:
2580 case Instruction::AND_LONG_2ADDR:
2581 case Instruction::OR_LONG_2ADDR:
2582 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::SHL_LONG_2ADDR:
2586 case Instruction::SHR_LONG_2ADDR:
2587 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::ADD_FLOAT_2ADDR:
2591 case Instruction::SUB_FLOAT_2ADDR:
2592 case Instruction::MUL_FLOAT_2ADDR:
2593 case Instruction::DIV_FLOAT_2ADDR:
2594 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::ADD_DOUBLE_2ADDR:
2598 case Instruction::SUB_DOUBLE_2ADDR:
2599 case Instruction::MUL_DOUBLE_2ADDR:
2600 case Instruction::DIV_DOUBLE_2ADDR:
2601 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::ADD_INT_LIT16:
2605 case Instruction::RSUB_INT:
2606 case Instruction::MUL_INT_LIT16:
2607 case Instruction::DIV_INT_LIT16:
2608 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::AND_INT_LIT16:
2612 case Instruction::OR_INT_LIT16:
2613 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::ADD_INT_LIT8:
2617 case Instruction::RSUB_INT_LIT8:
2618 case Instruction::MUL_INT_LIT8:
2619 case Instruction::DIV_INT_LIT8:
2620 case Instruction::REM_INT_LIT8:
2621 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::AND_INT_LIT8:
2627 case Instruction::OR_INT_LIT8:
2628 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631
2632 /*
2633 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002634 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002635 * inserted in the course of verification, we can expect to see it here.
2636 */
jeffhaob4df5142011-09-19 20:25:32 -07002637 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::UNUSED_EE:
2642 case Instruction::UNUSED_EF:
2643 case Instruction::UNUSED_F2:
2644 case Instruction::UNUSED_F3:
2645 case Instruction::UNUSED_F4:
2646 case Instruction::UNUSED_F5:
2647 case Instruction::UNUSED_F6:
2648 case Instruction::UNUSED_F7:
2649 case Instruction::UNUSED_F8:
2650 case Instruction::UNUSED_F9:
2651 case Instruction::UNUSED_FA:
2652 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002653 case Instruction::UNUSED_F0:
2654 case Instruction::UNUSED_F1:
2655 case Instruction::UNUSED_E3:
2656 case Instruction::UNUSED_E8:
2657 case Instruction::UNUSED_E7:
2658 case Instruction::UNUSED_E4:
2659 case Instruction::UNUSED_E9:
2660 case Instruction::UNUSED_FC:
2661 case Instruction::UNUSED_E5:
2662 case Instruction::UNUSED_EA:
2663 case Instruction::UNUSED_FD:
2664 case Instruction::UNUSED_E6:
2665 case Instruction::UNUSED_EB:
2666 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002667 case Instruction::UNUSED_3E:
2668 case Instruction::UNUSED_3F:
2669 case Instruction::UNUSED_40:
2670 case Instruction::UNUSED_41:
2671 case Instruction::UNUSED_42:
2672 case Instruction::UNUSED_43:
2673 case Instruction::UNUSED_73:
2674 case Instruction::UNUSED_79:
2675 case Instruction::UNUSED_7A:
2676 case Instruction::UNUSED_EC:
2677 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002678 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680
2681 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002682 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002683 * complain if an instruction is missing (which is desirable).
2684 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002686
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 if (failure_ != VERIFY_ERROR_NONE) {
2688 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002689 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002690 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002691 return false;
2692 } else {
2693 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002694 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002696 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002698 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002700 opcode_flag = Instruction::kThrow;
2701 }
2702 }
jeffhaobdb76512011-09-07 11:43:16 -07002703 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 * If we didn't just set the result register, clear it out. This ensures that you can only use
2705 * "move-result" immediately after the result is set. (We could check this statically, but it's
2706 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002707 */
2708 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002710 }
2711
jeffhaoa0a764a2011-09-16 10:43:38 -07002712 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002713 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2715 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2716 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002717 return false;
2718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2720 // next instruction isn't one.
2721 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002722 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 }
2724 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2725 if (next_line != NULL) {
2726 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2727 // needed.
2728 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002729 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 }
jeffhaobdb76512011-09-07 11:43:16 -07002731 } else {
2732 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 * We're not recording register data for the next instruction, so we don't know what the prior
2734 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002735 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002737 }
2738 }
2739
2740 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002741 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002742 *
2743 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002744 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002745 * somebody could get a reference field, check it for zero, and if the
2746 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002747 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002748 * that, and will reject the code.
2749 *
2750 * TODO: avoid re-fetching the branch target
2751 */
2752 if ((opcode_flag & Instruction::kBranch) != 0) {
2753 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002755 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002757 return false;
2758 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002759 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002761 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 }
jeffhaobdb76512011-09-07 11:43:16 -07002763 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002765 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 }
jeffhaobdb76512011-09-07 11:43:16 -07002767 }
2768
2769 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002770 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002771 *
2772 * We've already verified that the table is structurally sound, so we
2773 * just need to walk through and tag the targets.
2774 */
2775 if ((opcode_flag & Instruction::kSwitch) != 0) {
2776 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2777 const uint16_t* switch_insns = insns + offset_to_switch;
2778 int switch_count = switch_insns[1];
2779 int offset_to_targets, targ;
2780
2781 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2782 /* 0 = sig, 1 = count, 2/3 = first key */
2783 offset_to_targets = 4;
2784 } else {
2785 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002786 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002787 offset_to_targets = 2 + 2 * switch_count;
2788 }
2789
2790 /* verify each switch target */
2791 for (targ = 0; targ < switch_count; targ++) {
2792 int offset;
2793 uint32_t abs_offset;
2794
2795 /* offsets are 32-bit, and only partly endian-swapped */
2796 offset = switch_insns[offset_to_targets + targ * 2] |
2797 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 abs_offset = work_insn_idx_ + offset;
2799 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2800 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002801 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 }
2803 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002804 return false;
2805 }
2806 }
2807
2808 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2810 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002811 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2813 bool within_catch_all = false;
2814 DexFile::CatchHandlerIterator iterator =
2815 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002816
2817 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2819 within_catch_all = true;
2820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2823 * "work_regs", because at runtime the exception will be thrown before the instruction
2824 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002825 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002827 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 }
jeffhaobdb76512011-09-07 11:43:16 -07002829 }
2830
2831 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2833 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002834 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 * The state in work_line reflects the post-execution state. If the current instruction is a
2838 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002839 * it will do so before grabbing the lock).
2840 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2842 Fail(VERIFY_ERROR_GENERIC)
2843 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002844 return false;
2845 }
2846 }
2847 }
2848
jeffhaod1f0fde2011-09-08 17:25:33 -07002849 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 if ((opcode_flag & Instruction::kReturn) != 0) {
2851 if(!work_line_->VerifyMonitorStackEmpty()) {
2852 return false;
2853 }
jeffhaobdb76512011-09-07 11:43:16 -07002854 }
2855
2856 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002857 * Update start_guess. Advance to the next instruction of that's
2858 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002859 * neither of those exists we're in a return or throw; leave start_guess
2860 * alone and let the caller sort it out.
2861 */
2862 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002864 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2865 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002867 }
2868
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2870 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002871
2872 return true;
2873}
2874
Ian Rogersd81871c2011-10-03 13:57:23 -07002875Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2876 const Class* referrer = method_->GetDeclaringClass();
2877 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2878 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002879
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 if (res_class == NULL) {
2881 Thread::Current()->ClearException();
2882 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2883 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2884 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2885 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2886 << res_class->GetDescriptor()->ToModifiedUtf8();
2887 }
2888 return res_class;
2889}
2890
2891Class* DexVerifier::GetCaughtExceptionType() {
2892 Class* common_super = NULL;
2893 if (code_item_->tries_size_ != 0) {
2894 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2895 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2896 for (uint32_t i = 0; i < handlers_size; i++) {
2897 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2898 for (; !iterator.HasNext(); iterator.Next()) {
2899 DexFile::CatchHandlerItem handler = iterator.Get();
2900 if (handler.address_ == (uint32_t) work_insn_idx_) {
2901 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2902 common_super = JavaLangThrowable();
2903 } else {
2904 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2905 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2906 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2907 * test, so is essentially harmless.
2908 */
2909 if (klass == NULL) {
2910 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2911 << handler.type_idx_ << " ("
2912 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2913 return NULL;
2914 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2915 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2916 return NULL;
2917 } else if (common_super == NULL) {
2918 common_super = klass;
2919 } else {
2920 common_super = RegType::ClassJoin(common_super, klass);
2921 }
2922 }
2923 }
2924 }
2925 handlers_ptr = iterator.GetData();
2926 }
2927 }
2928 if (common_super == NULL) {
2929 /* no catch blocks, or no catches with classes we can find */
2930 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2931 }
2932 return common_super;
2933}
2934
2935Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2936 Class* referrer = method_->GetDeclaringClass();
2937 DexCache* dex_cache = referrer->GetDexCache();
2938 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2939 if (res_method == NULL) {
2940 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2941 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2942 if (klass == NULL) {
2943 DCHECK(failure_ != VERIFY_ERROR_NONE);
2944 return NULL;
2945 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002946 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2948 if (is_direct) {
2949 res_method = klass->FindDirectMethod(name, signature);
2950 } else if (klass->IsInterface()) {
2951 res_method = klass->FindInterfaceMethod(name, signature);
2952 } else {
2953 res_method = klass->FindVirtualMethod(name, signature);
2954 }
2955 if (res_method != NULL) {
2956 dex_cache->SetResolvedMethod(method_idx, res_method);
2957 } else {
2958 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2959 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2960 << " " << signature;
2961 return NULL;
2962 }
2963 }
2964 /* Check if access is allowed. */
2965 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2966 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2967 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2968 return NULL;
2969 }
2970 return res_method;
2971}
2972
2973Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2974 MethodType method_type, bool is_range, bool is_super) {
2975 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2976 // we're making.
2977 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2978 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2979 if (res_method == NULL) {
2980 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2981 const char* method_name = dex_file_->GetMethodName(method_id);
2982 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002983 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002984 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
2985 fail_messages_ << "unable to resolve method " << dec_insn.vB_ << ": "
2986 << class_descriptor << "." << method_name << " " << method_signature;
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 return NULL;
2988 }
2989 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2990 // enforce them here.
2991 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
2992 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
2993 << PrettyMethod(res_method);
2994 return NULL;
2995 }
2996 // See if the method type implied by the invoke instruction matches the access flags for the
2997 // target method.
2998 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2999 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3000 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3001 ) {
3002 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3003 << PrettyMethod(res_method);
3004 return NULL;
3005 }
3006 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3007 // has a vtable entry for the target method.
3008 if (is_super) {
3009 DCHECK(method_type == METHOD_VIRTUAL);
3010 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3011 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3012 if (super == NULL) { // Only Object has no super class
3013 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3014 << " to super " << PrettyMethod(res_method);
3015 } else {
3016 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3017 << " to super " << PrettyDescriptor(super->GetDescriptor())
3018 << "." << res_method->GetName()->ToModifiedUtf8()
3019 << " " << res_method->GetSignature()->ToModifiedUtf8();
3020
3021 }
3022 return NULL;
3023 }
3024 }
3025 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3026 // match the call to the signature. Also, we might might be calling through an abstract method
3027 // definition (which doesn't have register count values).
3028 int expected_args = dec_insn.vA_;
3029 /* caught by static verifier */
3030 DCHECK(is_range || expected_args <= 5);
3031 if (expected_args > code_item_->outs_size_) {
3032 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3033 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3034 return NULL;
3035 }
3036 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3037 if (sig[0] != '(') {
3038 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3039 << " as descriptor doesn't start with '(': " << sig;
3040 return NULL;
3041 }
jeffhaobdb76512011-09-07 11:43:16 -07003042 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 * Check the "this" argument, which must be an instance of the class
3044 * that declared the method. For an interface class, we don't do the
3045 * full interface merge, so we can't do a rigorous check here (which
3046 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003047 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 int actual_args = 0;
3049 if (!res_method->IsStatic()) {
3050 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3051 if (failure_ != VERIFY_ERROR_NONE) {
3052 return NULL;
3053 }
3054 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3055 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3056 return NULL;
3057 }
3058 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3059 Class* actual_this_ref = actual_arg_type.GetClass();
3060 if (!res_method->GetDeclaringClass()->IsAssignableFrom(actual_this_ref)) {
3061 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '"
3062 << PrettyDescriptor(actual_this_ref->GetDescriptor()) << "' not instance of '"
3063 << PrettyDescriptor(res_method->GetDeclaringClass()->GetDescriptor()) << "'";
3064 return NULL;
3065 }
3066 }
3067 actual_args++;
3068 }
3069 /*
3070 * Process the target method's signature. This signature may or may not
3071 * have been verified, so we can't assume it's properly formed.
3072 */
3073 size_t sig_offset = 0;
3074 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3075 if (actual_args >= expected_args) {
3076 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3077 << "'. Expected " << expected_args << " args, found more ("
3078 << sig.substr(sig_offset) << ")";
3079 return NULL;
3080 }
3081 std::string descriptor;
3082 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3083 size_t end;
3084 if (sig[sig_offset] == 'L') {
3085 end = sig.find(';', sig_offset);
3086 } else {
3087 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3088 if (sig[end] == 'L') {
3089 end = sig.find(';', end);
3090 }
3091 }
3092 if (end == std::string::npos) {
3093 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3094 << "bad signature component '" << sig << "' (missing ';')";
3095 return NULL;
3096 }
3097 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3098 sig_offset = end;
3099 } else {
3100 descriptor = sig[sig_offset];
3101 }
3102 const RegType& reg_type =
3103 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003104 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3105 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3106 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 }
3108 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3109 }
3110 if (sig[sig_offset] != ')') {
3111 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3112 return NULL;
3113 }
3114 if (actual_args != expected_args) {
3115 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3116 << " expected " << expected_args << " args, found " << actual_args;
3117 return NULL;
3118 } else {
3119 return res_method;
3120 }
3121}
3122
3123void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3124 const RegType& insn_type, bool is_primitive) {
3125 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3126 if (!index_type.IsArrayIndexTypes()) {
3127 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3128 } else {
3129 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3130 if (failure_ == VERIFY_ERROR_NONE) {
3131 if (array_class == NULL) {
3132 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3133 // instruction type. TODO: have a proper notion of bottom here.
3134 if (!is_primitive || insn_type.IsCategory1Types()) {
3135 // Reference or category 1
3136 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3137 } else {
3138 // Category 2
3139 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3140 }
3141 } else {
3142 /* verify the class */
3143 Class* component_class = array_class->GetComponentType();
3144 const RegType& component_type = reg_types_.FromClass(component_class);
3145 if (!array_class->IsArrayClass()) {
3146 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3147 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3148 } else if (component_class->IsPrimitive() && !is_primitive) {
3149 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3150 << PrettyDescriptor(array_class->GetDescriptor())
3151 << " source for aget-object";
3152 } else if (!component_class->IsPrimitive() && is_primitive) {
3153 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3154 << PrettyDescriptor(array_class->GetDescriptor())
3155 << " source for category 1 aget";
3156 } else if (is_primitive && !insn_type.Equals(component_type) &&
3157 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3158 (insn_type.IsLong() && component_type.IsDouble()))) {
3159 Fail(VERIFY_ERROR_GENERIC) << "array type "
3160 << PrettyDescriptor(array_class->GetDescriptor())
3161 << " incompatible with aget of type " << insn_type;
3162 } else {
3163 // Use knowledge of the field type which is stronger than the type inferred from the
3164 // instruction, which can't differentiate object types and ints from floats, longs from
3165 // doubles.
3166 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3167 }
3168 }
3169 }
3170 }
3171}
3172
3173void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3174 const RegType& insn_type, bool is_primitive) {
3175 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3176 if (!index_type.IsArrayIndexTypes()) {
3177 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3178 } else {
3179 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3180 if (failure_ == VERIFY_ERROR_NONE) {
3181 if (array_class == NULL) {
3182 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3183 // instruction type.
3184 } else {
3185 /* verify the class */
3186 Class* component_class = array_class->GetComponentType();
3187 const RegType& component_type = reg_types_.FromClass(component_class);
3188 if (!array_class->IsArrayClass()) {
3189 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3190 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3191 } else if (component_class->IsPrimitive() && !is_primitive) {
3192 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3193 << PrettyDescriptor(array_class->GetDescriptor())
3194 << " source for aput-object";
3195 } else if (!component_class->IsPrimitive() && is_primitive) {
3196 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3197 << PrettyDescriptor(array_class->GetDescriptor())
3198 << " source for category 1 aput";
3199 } else if (is_primitive && !insn_type.Equals(component_type) &&
3200 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3201 (insn_type.IsLong() && component_type.IsDouble()))) {
3202 Fail(VERIFY_ERROR_GENERIC) << "array type "
3203 << PrettyDescriptor(array_class->GetDescriptor())
3204 << " incompatible with aput of type " << insn_type;
3205 } else {
3206 // The instruction agrees with the type of array, confirm the value to be stored does too
3207 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3208 }
3209 }
3210 }
3211 }
3212}
3213
3214Field* DexVerifier::GetStaticField(int field_idx) {
3215 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3216 if (field == NULL) {
3217 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3218 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3219 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003220 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 DCHECK(Thread::Current()->IsExceptionPending());
3222 Thread::Current()->ClearException();
3223 return NULL;
3224 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3225 field->GetAccessFlags())) {
3226 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3227 << " from " << PrettyClass(method_->GetDeclaringClass());
3228 return NULL;
3229 } else if (!field->IsStatic()) {
3230 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3231 return NULL;
3232 } else {
3233 return field;
3234 }
3235}
3236
3237void DexVerifier::VerifySGet(const Instruction::DecodedInstruction& dec_insn,
3238 const RegType& insn_type, bool is_primitive) {
3239 Field* field = GetStaticField(dec_insn.vB_);
3240 if (field != NULL) {
3241 DCHECK(field->GetDeclaringClass()->IsResolved());
3242 Class* field_class = field->GetType();
3243 Class* insn_class = insn_type.GetClass();
3244 if (is_primitive) {
3245 if (field_class == insn_class ||
3246 (field_class->IsPrimitiveFloat() && insn_class->IsPrimitiveInt()) ||
3247 (field_class->IsPrimitiveDouble() && insn_class->IsPrimitiveLong())) {
3248 // expected that read is of the correct primitive type or that int reads are reading
3249 // floats or long reads are reading doubles
3250 } else {
3251 // This is a global failure rather than a class change failure as the instructions and
3252 // the descriptors for the type should have been consistent within the same file at
3253 // compile time
3254 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3255 << " to be of type " << PrettyClass(insn_class)
3256 << " but found type " << PrettyClass(field_class) << " in sget";
3257 return;
3258 }
3259 } else {
3260 if (!insn_class->IsAssignableFrom(field_class)) {
3261 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3262 << " to be of type " << PrettyClass(insn_class)
3263 << " but found type " << PrettyClass(field_class)
3264 << " in sget-object";
3265 return;
3266 }
3267 }
3268 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(field_class));
3269 }
3270}
3271
3272void DexVerifier::VerifySPut(const Instruction::DecodedInstruction& dec_insn,
3273 const RegType& insn_type, bool is_primitive) {
3274 Field* field = GetStaticField(dec_insn.vB_);
3275 if (field != NULL) {
3276 DCHECK(field->GetDeclaringClass()->IsResolved());
3277 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3278 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final static field " << PrettyField(field)
3279 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3280 return;
3281 }
3282 Class* field_class = field->GetType();
Ian Rogers2c8a8572011-10-24 17:11:36 -07003283 const RegType& field_type = reg_types_.FromClass(field_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 Class* insn_class = insn_type.GetClass();
3285 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003286 // Primitive field assignability rules are weaker than regular assignability rules
3287 bool instruction_compatible;
3288 bool value_compatible;
3289 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3290 if (field_type.IsIntegralTypes()) {
3291 instruction_compatible = insn_type.IsIntegralTypes();
3292 value_compatible = value_type.IsIntegralTypes();
3293 } else if (field_type.IsFloat()) {
3294 instruction_compatible = insn_type.IsInteger(); // no sput-float, so expect sput-int
3295 value_compatible = value_type.IsFloatTypes();
3296 } else if (field_type.IsLong()) {
3297 instruction_compatible = insn_type.IsLong();
3298 value_compatible = value_type.IsLongTypes();
3299 } else if (field_type.IsDouble()) {
3300 instruction_compatible = insn_type.IsLong(); // no sput-double, so expect sput-long
3301 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003303 instruction_compatible = false; // reference field with primitive store
3304 value_compatible = false; // unused
3305 }
3306 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 // This is a global failure rather than a class change failure as the instructions and
3308 // the descriptors for the type should have been consistent within the same file at
3309 // compile time
3310 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3311 << " to be of type " << PrettyClass(insn_class)
3312 << " but found type " << PrettyClass(field_class) << " in sput";
3313 return;
3314 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003315 if (!value_compatible) {
3316 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3317 << " of type " << value_type
3318 << " but expected " << field_type
3319 << " for store to " << PrettyField(field) << " in sput";
3320 return;
3321 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 } else {
3323 if (!insn_class->IsAssignableFrom(field_class)) {
3324 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
3325 << " to be compatible with type " << insn_type
3326 << " but found type " << PrettyClass(field_class)
3327 << " in sput-object";
3328 return;
3329 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003330 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 }
3333}
3334
3335Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3336 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3337 if (field == NULL) {
3338 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3339 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3340 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003341 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003342 DCHECK(Thread::Current()->IsExceptionPending());
3343 Thread::Current()->ClearException();
3344 return NULL;
3345 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3346 field->GetAccessFlags())) {
3347 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3348 << " from " << PrettyClass(method_->GetDeclaringClass());
3349 return NULL;
3350 } else if (field->IsStatic()) {
3351 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3352 << " to not be static";
3353 return NULL;
3354 } else if (obj_type.IsZero()) {
3355 // Cannot infer and check type, however, access will cause null pointer exception
3356 return field;
3357 } else if(obj_type.IsUninitializedReference() &&
3358 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3359 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3360 // Field accesses through uninitialized references are only allowable for constructors where
3361 // the field is declared in this class
3362 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3363 << " of a not fully initialized object within the context of "
3364 << PrettyMethod(method_);
3365 return NULL;
3366 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3367 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3368 // of C1. For resolution to occur the declared class of the field must be compatible with
3369 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3370 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3371 << " from object of type " << PrettyClass(obj_type.GetClass());
3372 return NULL;
3373 } else {
3374 return field;
3375 }
3376}
3377
3378void DexVerifier::VerifyIGet(const Instruction::DecodedInstruction& dec_insn,
3379 const RegType& insn_type, bool is_primitive) {
3380 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3381 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3382 if (field != NULL) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003383 const RegType& field_type =
3384 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3385 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003387 if (field_type.Equals(insn_type) ||
3388 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3389 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 // expected that read is of the correct primitive type or that int reads are reading
3391 // floats or long reads are reading doubles
3392 } else {
3393 // This is a global failure rather than a class change failure as the instructions and
3394 // the descriptors for the type should have been consistent within the same file at
3395 // compile time
3396 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003397 << " to be of type '" << insn_type
3398 << "' but found type '" << field_type << "' in iget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 return;
3400 }
3401 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003402 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003404 << " to be compatible with type '" << insn_type
3405 << "' but found type '" << field_type
3406 << "' in iget-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 return;
3408 }
3409 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003410 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 }
3412}
3413
3414void DexVerifier::VerifyIPut(const Instruction::DecodedInstruction& dec_insn,
3415 const RegType& insn_type, bool is_primitive) {
3416 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3417 Field* field = GetInstanceField(object_type, dec_insn.vC_);
3418 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3420 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3421 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3422 return;
3423 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003424 const RegType& field_type =
3425 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3426 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003428 // Primitive field assignability rules are weaker than regular assignability rules
3429 bool instruction_compatible;
3430 bool value_compatible;
3431 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3432 if (field_type.IsIntegralTypes()) {
3433 instruction_compatible = insn_type.IsIntegralTypes();
3434 value_compatible = value_type.IsIntegralTypes();
3435 } else if (field_type.IsFloat()) {
3436 instruction_compatible = insn_type.IsInteger(); // no iput-float, so expect iput-int
3437 value_compatible = value_type.IsFloatTypes();
3438 } else if (field_type.IsLong()) {
3439 instruction_compatible = insn_type.IsLong();
3440 value_compatible = value_type.IsLongTypes();
3441 } else if (field_type.IsDouble()) {
3442 instruction_compatible = insn_type.IsLong(); // no iput-double, so expect iput-long
3443 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003445 instruction_compatible = false; // reference field with primitive store
3446 value_compatible = false; // unused
3447 }
3448 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003449 // This is a global failure rather than a class change failure as the instructions and
3450 // the descriptors for the type should have been consistent within the same file at
3451 // compile time
3452 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003453 << " to be of type '" << insn_type
3454 << "' but found type '" << field_type
3455 << "' in iput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 return;
3457 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003458 if (!value_compatible) {
3459 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3460 << " of type " << value_type
3461 << " but expected " << field_type
3462 << " for store to " << PrettyField(field) << " in iput";
3463 return;
3464 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003466 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003468 << " to be compatible with type '" << insn_type
3469 << "' but found type '" << field_type
3470 << "' in iput-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003471 return;
3472 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003473 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 }
3476}
3477
3478bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3479 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3480 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3481 return false;
3482 }
3483 return true;
3484}
3485
3486void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3487 Class* res_class, bool is_range) {
3488 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3489 /*
3490 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3491 * list and fail. It's legal, if silly, for arg_count to be zero.
3492 */
3493 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3494 uint32_t arg_count = dec_insn.vA_;
3495 for (size_t ui = 0; ui < arg_count; ui++) {
3496 uint32_t get_reg;
3497
3498 if (is_range)
3499 get_reg = dec_insn.vC_ + ui;
3500 else
3501 get_reg = dec_insn.arg_[ui];
3502
3503 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3504 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3505 << ") not valid";
3506 return;
3507 }
3508 }
3509}
3510
3511void DexVerifier::ReplaceFailingInstruction() {
3512 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3513 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3514 VerifyErrorRefType ref_type;
3515 switch (inst->Opcode()) {
3516 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003517 case Instruction::CHECK_CAST:
3518 case Instruction::INSTANCE_OF:
3519 case Instruction::NEW_INSTANCE:
3520 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003522 case Instruction::FILLED_NEW_ARRAY_RANGE:
3523 ref_type = VERIFY_ERROR_REF_CLASS;
3524 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003526 case Instruction::IGET_BOOLEAN:
3527 case Instruction::IGET_BYTE:
3528 case Instruction::IGET_CHAR:
3529 case Instruction::IGET_SHORT:
3530 case Instruction::IGET_WIDE:
3531 case Instruction::IGET_OBJECT:
3532 case Instruction::IPUT:
3533 case Instruction::IPUT_BOOLEAN:
3534 case Instruction::IPUT_BYTE:
3535 case Instruction::IPUT_CHAR:
3536 case Instruction::IPUT_SHORT:
3537 case Instruction::IPUT_WIDE:
3538 case Instruction::IPUT_OBJECT:
3539 case Instruction::SGET:
3540 case Instruction::SGET_BOOLEAN:
3541 case Instruction::SGET_BYTE:
3542 case Instruction::SGET_CHAR:
3543 case Instruction::SGET_SHORT:
3544 case Instruction::SGET_WIDE:
3545 case Instruction::SGET_OBJECT:
3546 case Instruction::SPUT:
3547 case Instruction::SPUT_BOOLEAN:
3548 case Instruction::SPUT_BYTE:
3549 case Instruction::SPUT_CHAR:
3550 case Instruction::SPUT_SHORT:
3551 case Instruction::SPUT_WIDE:
3552 case Instruction::SPUT_OBJECT:
3553 ref_type = VERIFY_ERROR_REF_FIELD;
3554 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003556 case Instruction::INVOKE_VIRTUAL_RANGE:
3557 case Instruction::INVOKE_SUPER:
3558 case Instruction::INVOKE_SUPER_RANGE:
3559 case Instruction::INVOKE_DIRECT:
3560 case Instruction::INVOKE_DIRECT_RANGE:
3561 case Instruction::INVOKE_STATIC:
3562 case Instruction::INVOKE_STATIC_RANGE:
3563 case Instruction::INVOKE_INTERFACE:
3564 case Instruction::INVOKE_INTERFACE_RANGE:
3565 ref_type = VERIFY_ERROR_REF_METHOD;
3566 break;
jeffhaobdb76512011-09-07 11:43:16 -07003567 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003568 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003569 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3572 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3573 // instruction, so assert it.
3574 size_t width = inst->SizeInCodeUnits();
3575 CHECK_GT(width, 1u);
3576 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3577 // NOPs
3578 for (size_t i = 2; i < width; i++) {
3579 insns[work_insn_idx_ + i] = Instruction::NOP;
3580 }
3581 // Encode the opcode, with the failure code in the high byte
3582 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3583 (failure_ << 8) | // AA - component
3584 (ref_type << (8 + kVerifyErrorRefTypeShift));
3585 insns[work_insn_idx_] = new_instruction;
3586 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3587 // rewrote, so nothing to do here.
jeffhaobdb76512011-09-07 11:43:16 -07003588}
jeffhaoba5ebb92011-08-25 17:24:37 -07003589
Ian Rogersd81871c2011-10-03 13:57:23 -07003590bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3591 const bool merge_debug = true;
3592 bool changed = true;
3593 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3594 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003595 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 * We haven't processed this instruction before, and we haven't touched the registers here, so
3597 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3598 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003599 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003601 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003602 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3603 copy->CopyFromLine(target_line);
3604 changed = target_line->MergeRegisters(merge_line);
3605 if (failure_ != VERIFY_ERROR_NONE) {
3606 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003607 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003608 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3610 << *copy.get() << " MERGE" << std::endl
3611 << *merge_line << " ==" << std::endl
3612 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003613 }
3614 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 if (changed) {
3616 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003617 }
3618 return true;
3619}
3620
Ian Rogersd81871c2011-10-03 13:57:23 -07003621void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3622 size_t* log2_max_gc_pc) {
3623 size_t local_gc_points = 0;
3624 size_t max_insn = 0;
3625 size_t max_ref_reg = -1;
3626 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3627 if (insn_flags_[i].IsGcPoint()) {
3628 local_gc_points++;
3629 max_insn = i;
3630 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003631 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003632 }
3633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 *gc_points = local_gc_points;
3635 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3636 size_t i = 0;
3637 while ((1U << i) < max_insn) {
3638 i++;
3639 }
3640 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003641}
3642
Ian Rogersd81871c2011-10-03 13:57:23 -07003643ByteArray* DexVerifier::GenerateGcMap() {
3644 size_t num_entries, ref_bitmap_bits, pc_bits;
3645 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3646 // There's a single byte to encode the size of each bitmap
3647 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3648 // TODO: either a better GC map format or per method failures
3649 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3650 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003651 return NULL;
3652 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3654 // There are 2 bytes to encode the number of entries
3655 if (num_entries >= 65536) {
3656 // TODO: either a better GC map format or per method failures
3657 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3658 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003659 return NULL;
3660 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003662 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003664 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 pc_bytes = 1;
3666 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003667 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003669 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 // TODO: either a better GC map format or per method failures
3671 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3672 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3673 return NULL;
3674 }
3675 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3676 ByteArray* table = ByteArray::Alloc(table_size);
3677 if (table == NULL) {
3678 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3679 return NULL;
3680 }
3681 // Write table header
3682 table->Set(0, format);
3683 table->Set(1, ref_bitmap_bytes);
3684 table->Set(2, num_entries & 0xFF);
3685 table->Set(3, (num_entries >> 8) & 0xFF);
3686 // Write table data
3687 size_t offset = 4;
3688 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3689 if (insn_flags_[i].IsGcPoint()) {
3690 table->Set(offset, i & 0xFF);
3691 offset++;
3692 if (pc_bytes == 2) {
3693 table->Set(offset, (i >> 8) & 0xFF);
3694 offset++;
3695 }
3696 RegisterLine* line = reg_table_.GetLine(i);
3697 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3698 offset += ref_bitmap_bytes;
3699 }
3700 }
3701 DCHECK(offset == table_size);
3702 return table;
3703}
jeffhaoa0a764a2011-09-16 10:43:38 -07003704
Ian Rogersd81871c2011-10-03 13:57:23 -07003705void DexVerifier::VerifyGcMap() {
3706 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3707 // that the table data is well formed and all references are marked (or not) in the bitmap
3708 PcToReferenceMap map(method_);
3709 size_t map_index = 0;
3710 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3711 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3712 if (insn_flags_[i].IsGcPoint()) {
3713 CHECK_LT(map_index, map.NumEntries());
3714 CHECK_EQ(map.GetPC(map_index), i);
3715 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3716 map_index++;
3717 RegisterLine* line = reg_table_.GetLine(i);
3718 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003719 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003720 CHECK_LT(j / 8, map.RegWidth());
3721 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3722 } else if ((j / 8) < map.RegWidth()) {
3723 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3724 } else {
3725 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3726 }
3727 }
3728 } else {
3729 CHECK(reg_bitmap == NULL);
3730 }
3731 }
3732}
jeffhaoa0a764a2011-09-16 10:43:38 -07003733
Ian Rogersd81871c2011-10-03 13:57:23 -07003734Class* DexVerifier::JavaLangThrowable() {
3735 if (java_lang_throwable_ == NULL) {
3736 java_lang_throwable_ =
3737 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3738 DCHECK(java_lang_throwable_ != NULL);
3739 }
3740 return java_lang_throwable_;
3741}
3742
3743const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3744 size_t num_entries = NumEntries();
3745 // Do linear or binary search?
3746 static const size_t kSearchThreshold = 8;
3747 if (num_entries < kSearchThreshold) {
3748 for (size_t i = 0; i < num_entries; i++) {
3749 if (GetPC(i) == dex_pc) {
3750 return GetBitMap(i);
3751 }
3752 }
3753 } else {
3754 int lo = 0;
3755 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003756 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003757 int mid = (hi + lo) / 2;
3758 int mid_pc = GetPC(mid);
3759 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003760 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003762 hi = mid - 1;
3763 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003765 }
3766 }
3767 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 if (error_if_not_present) {
3769 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3770 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003771 return NULL;
3772}
3773
Ian Rogersd81871c2011-10-03 13:57:23 -07003774} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003775} // namespace art