blob: 6c5a2b90c320e24b2fa0802a12bab6cca4ceac1e [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
Elliott Hughes1f359b02011-07-17 14:27:17 -07005#include <iostream>
6
Brian Carlstrom1f870082011-08-23 16:02:11 -07007#include "class_linker.h"
jeffhaob4df5142011-09-19 20:25:32 -07008#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
10#include "dex_instruction.h"
11#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070012#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070013#include "intern_table.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070014#include "logging.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070016#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017
18namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070019namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
Ian Rogers2c8a8572011-10-24 17:11:36 -070021static const bool gDebugVerify = false;
22
Ian Rogersd81871c2011-10-03 13:57:23 -070023std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
24 return os << (int)rhs;
25}
jeffhaobdb76512011-09-07 11:43:16 -070026
Ian Rogers84fa0742011-10-25 18:13:30 -070027static const char* type_strings[] = {
28 "Unknown",
29 "Conflict",
30 "Boolean",
31 "Byte",
32 "Short",
33 "Char",
34 "Integer",
35 "Float",
36 "Long (Low Half)",
37 "Long (High Half)",
38 "Double (Low Half)",
39 "Double (High Half)",
40 "64-bit Constant (Low Half)",
41 "64-bit Constant (High Half)",
42 "32-bit Constant",
43 "Unresolved Reference",
44 "Uninitialized Reference",
45 "Uninitialized This Reference",
46 "Unresolved And Uninitialized This Reference",
47 "Reference",
48};
Ian Rogersd81871c2011-10-03 13:57:23 -070049
Ian Rogers2c8a8572011-10-24 17:11:36 -070050std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070051 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
52 std::string result;
53 if (IsConstant()) {
54 uint32_t val = ConstantValue();
55 if (val == 0) {
56 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070057 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070058 if(IsConstantShort()) {
59 result = StringPrintf("32-bit Constant: %d", val);
60 } else {
61 result = StringPrintf("32-bit Constant: 0x%x", val);
62 }
63 }
64 } else {
65 result = type_strings[type_];
66 if (IsReferenceTypes()) {
67 result += ": ";
68 if (IsUnresolvedReference()) {
69 result += PrettyDescriptor(GetDescriptor());
70 } else {
71 result += PrettyDescriptor(GetClass()->GetDescriptor());
72 }
Ian Rogersd81871c2011-10-03 13:57:23 -070073 }
74 }
Ian Rogers84fa0742011-10-25 18:13:30 -070075 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070076}
77
78const RegType& RegType::HighHalf(RegTypeCache* cache) const {
79 CHECK(IsLowHalf());
80 if (type_ == kRegTypeLongLo) {
81 return cache->FromType(kRegTypeLongHi);
82 } else if (type_ == kRegTypeDoubleLo) {
83 return cache->FromType(kRegTypeDoubleHi);
84 } else {
85 return cache->FromType(kRegTypeConstHi);
86 }
87}
88
89/*
90 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
91 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
92 * S and T such that there isn't a parent of both S and T that isn't also the parent of J (ie J
93 * is the deepest (lowest upper bound) parent of S and T).
94 *
95 * This operation applies for regular classes and arrays, however, for interface types there needn't
96 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
97 * introducing sets of types, however, the only operation permissible on an interface is
98 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
99 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700100 * the perversion of any Object being assignable to an interface type (note, however, that we don't
101 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
102 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700103 */
104Class* RegType::ClassJoin(Class* s, Class* t) {
105 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
106 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
107 if (s == t) {
108 return s;
109 } else if (s->IsAssignableFrom(t)) {
110 return s;
111 } else if (t->IsAssignableFrom(s)) {
112 return t;
113 } else if (s->IsArrayClass() && t->IsArrayClass()) {
114 Class* s_ct = s->GetComponentType();
115 Class* t_ct = t->GetComponentType();
116 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
117 // Given the types aren't the same, if either array is of primitive types then the only
118 // common parent is java.lang.Object
119 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
120 DCHECK(result->IsObjectClass());
121 return result;
122 }
123 Class* common_elem = ClassJoin(s_ct, t_ct);
124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
125 const ClassLoader* class_loader = s->GetClassLoader();
126 std::string descriptor = "[" + common_elem->GetDescriptor()->ToModifiedUtf8();
127 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
128 DCHECK(array_class != NULL);
129 return array_class;
130 } else {
131 size_t s_depth = s->Depth();
132 size_t t_depth = t->Depth();
133 // Get s and t to the same depth in the hierarchy
134 if (s_depth > t_depth) {
135 while (s_depth > t_depth) {
136 s = s->GetSuperClass();
137 s_depth--;
138 }
139 } else {
140 while (t_depth > s_depth) {
141 t = t->GetSuperClass();
142 t_depth--;
143 }
144 }
145 // Go up the hierarchy until we get to the common parent
146 while (s != t) {
147 s = s->GetSuperClass();
148 t = t->GetSuperClass();
149 }
150 return s;
151 }
152}
153
Ian Rogersb5e95b92011-10-25 23:28:55 -0700154bool RegType::IsAssignableFrom(const RegType& src) const {
155 if (Equals(src)) {
156 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700157 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700158 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700159 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
160 case RegType::kRegTypeByte: return src.IsByteTypes();
161 case RegType::kRegTypeShort: return src.IsShortTypes();
162 case RegType::kRegTypeChar: return src.IsCharTypes();
163 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
164 case RegType::kRegTypeFloat: return src.IsFloatTypes();
165 case RegType::kRegTypeLongLo: return src.IsLongTypes();
166 case RegType::kRegTypeDoubleLo: return src.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()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700172 return true; // all reference types can be assigned null
173 } else if (!src.IsReferenceTypes()) {
174 return false; // expect src to be a reference type
175 } else if (IsJavaLangObject()) {
176 return true; // all reference types can be assigned to Object
177 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700178 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700179 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700180 GetClass()->IsAssignableFrom(src.GetClass())) {
181 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700182 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700183 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700184 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700185 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 }
187 }
188}
189
Ian Rogers84fa0742011-10-25 18:13:30 -0700190static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
191 return a.IsConstant() ? b : a;
192}
jeffhaobdb76512011-09-07 11:43:16 -0700193
Ian Rogersd81871c2011-10-03 13:57:23 -0700194const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
195 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700196 if (IsUnknown() && incoming_type.IsUnknown()) {
197 return *this; // Unknown MERGE Unknown => Unknown
198 } else if (IsConflict()) {
199 return *this; // Conflict MERGE * => Conflict
200 } else if (incoming_type.IsConflict()) {
201 return incoming_type; // * MERGE Conflict => Conflict
202 } else if (IsUnknown() || incoming_type.IsUnknown()) {
203 return reg_types->Conflict(); // Unknown MERGE * => Conflict
204 } else if(IsConstant() && incoming_type.IsConstant()) {
205 int32_t val1 = ConstantValue();
206 int32_t val2 = incoming_type.ConstantValue();
207 if (val1 >= 0 && val2 >= 0) {
208 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
209 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700210 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700211 } else {
212 return incoming_type;
213 }
214 } else if (val1 < 0 && val2 < 0) {
215 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
216 if (val1 <= val2) {
217 return *this;
218 } else {
219 return incoming_type;
220 }
221 } else {
222 // Values are +ve and -ve, choose smallest signed type in which they both fit
223 if (IsConstantByte()) {
224 if (incoming_type.IsConstantByte()) {
225 return reg_types->ByteConstant();
226 } else if (incoming_type.IsConstantShort()) {
227 return reg_types->ShortConstant();
228 } else {
229 return reg_types->IntConstant();
230 }
231 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700232 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700233 return reg_types->ShortConstant();
234 } else {
235 return reg_types->IntConstant();
236 }
237 } else {
238 return reg_types->IntConstant();
239 }
240 }
241 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
242 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
243 return reg_types->Boolean(); // boolean MERGE boolean => boolean
244 }
245 if (IsByteTypes() && incoming_type.IsByteTypes()) {
246 return reg_types->Byte(); // byte MERGE byte => byte
247 }
248 if (IsShortTypes() && incoming_type.IsShortTypes()) {
249 return reg_types->Short(); // short MERGE short => short
250 }
251 if (IsCharTypes() && incoming_type.IsCharTypes()) {
252 return reg_types->Char(); // char MERGE char => char
253 }
254 return reg_types->Integer(); // int MERGE * => int
255 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
256 (IsLongTypes() && incoming_type.IsLongTypes()) ||
257 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
258 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
259 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
260 // check constant case was handled prior to entry
261 DCHECK(!IsConstant() || !incoming_type.IsConstant());
262 // float/long/double MERGE float/long/double_constant => float/long/double
263 return SelectNonConstant(*this, incoming_type);
264 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700265 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700266 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700267 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
268 return reg_types->JavaLangObject(); // Object MERGE ref => Object
269 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
270 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
271 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
272 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700273 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) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700879 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700880 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700881 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700882 verifier.Dump(std::cout);
883 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
885 }
886 return success;
887}
888
889DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
890 method_(method), failure_(VERIFY_ERROR_NONE),
891 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700892 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
893 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 dex_file_ = &class_linker->FindDexFile(dex_cache);
895 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700896}
897
Ian Rogersd81871c2011-10-03 13:57:23 -0700898bool DexVerifier::Verify() {
899 // If there aren't any instructions, make sure that's expected, then exit successfully.
900 if (code_item_ == NULL) {
901 if (!method_->IsNative() && !method_->IsAbstract()) {
902 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700903 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 } else {
905 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700906 }
jeffhaobdb76512011-09-07 11:43:16 -0700907 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
909 if (code_item_->ins_size_ > code_item_->registers_size_) {
910 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
911 << " regs=" << code_item_->registers_size_;
912 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700913 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 // Allocate and initialize an array to hold instruction data.
915 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
916 // Run through the instructions and see if the width checks out.
917 bool result = ComputeWidthsAndCountOps();
918 // Flag instructions guarded by a "try" block and check exception handlers.
919 result = result && ScanTryCatchBlocks();
920 // Perform static instruction verification.
921 result = result && VerifyInstructions();
922 // Perform code flow analysis.
923 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700924 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700925}
926
Ian Rogersd81871c2011-10-03 13:57:23 -0700927bool DexVerifier::ComputeWidthsAndCountOps() {
928 const uint16_t* insns = code_item_->insns_;
929 size_t insns_size = code_item_->insns_size_in_code_units_;
930 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700931 size_t new_instance_count = 0;
932 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700934
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700936 Instruction::Code opcode = inst->Opcode();
937 if (opcode == Instruction::NEW_INSTANCE) {
938 new_instance_count++;
939 } else if (opcode == Instruction::MONITOR_ENTER) {
940 monitor_enter_count++;
941 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 size_t inst_size = inst->SizeInCodeUnits();
943 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
944 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700945 inst = inst->Next();
946 }
947
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 if (dex_pc != insns_size) {
949 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
950 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700951 return false;
952 }
953
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 new_instance_count_ = new_instance_count;
955 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700956 return true;
957}
958
Ian Rogersd81871c2011-10-03 13:57:23 -0700959bool DexVerifier::ScanTryCatchBlocks() {
960 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700961 if (tries_size == 0) {
962 return true;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 uint32_t insns_size = code_item_->insns_size_in_code_units_;
965 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700966
967 for (uint32_t idx = 0; idx < tries_size; idx++) {
968 const DexFile::TryItem* try_item = &tries[idx];
969 uint32_t start = try_item->start_addr_;
970 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700971 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
973 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700974 return false;
975 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 if (!insn_flags_[start].IsOpcode()) {
977 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700978 return false;
979 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 for (uint32_t dex_pc = start; dex_pc < end;
981 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
982 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700983 }
984 }
jeffhaobdb76512011-09-07 11:43:16 -0700985 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700987 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
988 for (uint32_t idx = 0; idx < handlers_size; idx++) {
989 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -0700990 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 uint32_t dex_pc= iterator.Get().address_;
992 if (!insn_flags_[dex_pc].IsOpcode()) {
993 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700994 return false;
995 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -0700997 }
jeffhaobdb76512011-09-07 11:43:16 -0700998 handlers_ptr = iterator.GetData();
999 }
jeffhaobdb76512011-09-07 11:43:16 -07001000 return true;
1001}
1002
Ian Rogersd81871c2011-10-03 13:57:23 -07001003bool DexVerifier::VerifyInstructions() {
1004 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001005
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 /* Flag the start of the method as a branch target. */
1007 insn_flags_[0].SetBranchTarget();
1008
1009 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1010 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1011 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001012 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1013 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 return false;
1015 }
1016 /* Flag instructions that are garbage collection points */
1017 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1018 insn_flags_[dex_pc].SetGcPoint();
1019 }
1020 dex_pc += inst->SizeInCodeUnits();
1021 inst = inst->Next();
1022 }
1023 return true;
1024}
1025
1026bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1027 Instruction::DecodedInstruction dec_insn(inst);
1028 bool result = true;
1029 switch (inst->GetVerifyTypeArgumentA()) {
1030 case Instruction::kVerifyRegA:
1031 result = result && CheckRegisterIndex(dec_insn.vA_);
1032 break;
1033 case Instruction::kVerifyRegAWide:
1034 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1035 break;
1036 }
1037 switch (inst->GetVerifyTypeArgumentB()) {
1038 case Instruction::kVerifyRegB:
1039 result = result && CheckRegisterIndex(dec_insn.vB_);
1040 break;
1041 case Instruction::kVerifyRegBField:
1042 result = result && CheckFieldIndex(dec_insn.vB_);
1043 break;
1044 case Instruction::kVerifyRegBMethod:
1045 result = result && CheckMethodIndex(dec_insn.vB_);
1046 break;
1047 case Instruction::kVerifyRegBNewInstance:
1048 result = result && CheckNewInstance(dec_insn.vB_);
1049 break;
1050 case Instruction::kVerifyRegBString:
1051 result = result && CheckStringIndex(dec_insn.vB_);
1052 break;
1053 case Instruction::kVerifyRegBType:
1054 result = result && CheckTypeIndex(dec_insn.vB_);
1055 break;
1056 case Instruction::kVerifyRegBWide:
1057 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1058 break;
1059 }
1060 switch (inst->GetVerifyTypeArgumentC()) {
1061 case Instruction::kVerifyRegC:
1062 result = result && CheckRegisterIndex(dec_insn.vC_);
1063 break;
1064 case Instruction::kVerifyRegCField:
1065 result = result && CheckFieldIndex(dec_insn.vC_);
1066 break;
1067 case Instruction::kVerifyRegCNewArray:
1068 result = result && CheckNewArray(dec_insn.vC_);
1069 break;
1070 case Instruction::kVerifyRegCType:
1071 result = result && CheckTypeIndex(dec_insn.vC_);
1072 break;
1073 case Instruction::kVerifyRegCWide:
1074 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1075 break;
1076 }
1077 switch (inst->GetVerifyExtraFlags()) {
1078 case Instruction::kVerifyArrayData:
1079 result = result && CheckArrayData(code_offset);
1080 break;
1081 case Instruction::kVerifyBranchTarget:
1082 result = result && CheckBranchTarget(code_offset);
1083 break;
1084 case Instruction::kVerifySwitchTargets:
1085 result = result && CheckSwitchTargets(code_offset);
1086 break;
1087 case Instruction::kVerifyVarArg:
1088 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1089 break;
1090 case Instruction::kVerifyVarArgRange:
1091 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1092 break;
1093 case Instruction::kVerifyError:
1094 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1095 result = false;
1096 break;
1097 }
1098 return result;
1099}
1100
1101bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1102 if (idx >= code_item_->registers_size_) {
1103 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1104 << code_item_->registers_size_ << ")";
1105 return false;
1106 }
1107 return true;
1108}
1109
1110bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1111 if (idx + 1 >= code_item_->registers_size_) {
1112 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1113 << "+1 >= " << code_item_->registers_size_ << ")";
1114 return false;
1115 }
1116 return true;
1117}
1118
1119bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1120 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1121 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1122 << dex_file_->GetHeader().field_ids_size_ << ")";
1123 return false;
1124 }
1125 return true;
1126}
1127
1128bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1129 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1130 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1131 << dex_file_->GetHeader().method_ids_size_ << ")";
1132 return false;
1133 }
1134 return true;
1135}
1136
1137bool DexVerifier::CheckNewInstance(uint32_t idx) {
1138 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1139 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1140 << dex_file_->GetHeader().type_ids_size_ << ")";
1141 return false;
1142 }
1143 // We don't need the actual class, just a pointer to the class name.
1144 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1145 if (descriptor[0] != 'L') {
1146 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1147 return false;
1148 }
1149 return true;
1150}
1151
1152bool DexVerifier::CheckStringIndex(uint32_t idx) {
1153 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1154 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1155 << dex_file_->GetHeader().string_ids_size_ << ")";
1156 return false;
1157 }
1158 return true;
1159}
1160
1161bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1162 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1163 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1164 << dex_file_->GetHeader().type_ids_size_ << ")";
1165 return false;
1166 }
1167 return true;
1168}
1169
1170bool DexVerifier::CheckNewArray(uint32_t idx) {
1171 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1172 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1173 << dex_file_->GetHeader().type_ids_size_ << ")";
1174 return false;
1175 }
1176 int bracket_count = 0;
1177 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1178 const char* cp = descriptor;
1179 while (*cp++ == '[') {
1180 bracket_count++;
1181 }
1182 if (bracket_count == 0) {
1183 /* The given class must be an array type. */
1184 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1185 return false;
1186 } else if (bracket_count > 255) {
1187 /* It is illegal to create an array of more than 255 dimensions. */
1188 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1189 return false;
1190 }
1191 return true;
1192}
1193
1194bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1195 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1196 const uint16_t* insns = code_item_->insns_ + cur_offset;
1197 const uint16_t* array_data;
1198 int32_t array_data_offset;
1199
1200 DCHECK_LT(cur_offset, insn_count);
1201 /* make sure the start of the array data table is in range */
1202 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1203 if ((int32_t) cur_offset + array_data_offset < 0 ||
1204 cur_offset + array_data_offset + 2 >= insn_count) {
1205 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1206 << ", data offset " << array_data_offset << ", count " << insn_count;
1207 return false;
1208 }
1209 /* offset to array data table is a relative branch-style offset */
1210 array_data = insns + array_data_offset;
1211 /* make sure the table is 32-bit aligned */
1212 if ((((uint32_t) array_data) & 0x03) != 0) {
1213 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1214 << ", data offset " << array_data_offset;
1215 return false;
1216 }
1217 uint32_t value_width = array_data[1];
1218 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1219 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1220 /* make sure the end of the switch is in range */
1221 if (cur_offset + array_data_offset + table_size > insn_count) {
1222 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1223 << ", data offset " << array_data_offset << ", end "
1224 << cur_offset + array_data_offset + table_size
1225 << ", count " << insn_count;
1226 return false;
1227 }
1228 return true;
1229}
1230
1231bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1232 int32_t offset;
1233 bool isConditional, selfOkay;
1234 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1235 return false;
1236 }
1237 if (!selfOkay && offset == 0) {
1238 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1239 return false;
1240 }
1241 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1242 // identical "wrap-around" behavior, but it's unwise to depend on that.
1243 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1244 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1245 return false;
1246 }
1247 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1248 int32_t abs_offset = cur_offset + offset;
1249 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1250 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1251 << (void*) abs_offset << ") at " << (void*) cur_offset;
1252 return false;
1253 }
1254 insn_flags_[abs_offset].SetBranchTarget();
1255 return true;
1256}
1257
1258bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1259 bool* selfOkay) {
1260 const uint16_t* insns = code_item_->insns_ + cur_offset;
1261 *pConditional = false;
1262 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001263 switch (*insns & 0xff) {
1264 case Instruction::GOTO:
1265 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001266 break;
1267 case Instruction::GOTO_32:
1268 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 *selfOkay = true;
1270 break;
1271 case Instruction::GOTO_16:
1272 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 break;
1274 case Instruction::IF_EQ:
1275 case Instruction::IF_NE:
1276 case Instruction::IF_LT:
1277 case Instruction::IF_GE:
1278 case Instruction::IF_GT:
1279 case Instruction::IF_LE:
1280 case Instruction::IF_EQZ:
1281 case Instruction::IF_NEZ:
1282 case Instruction::IF_LTZ:
1283 case Instruction::IF_GEZ:
1284 case Instruction::IF_GTZ:
1285 case Instruction::IF_LEZ:
1286 *pOffset = (int16_t) insns[1];
1287 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001288 break;
1289 default:
1290 return false;
1291 break;
1292 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001293 return true;
1294}
1295
Ian Rogersd81871c2011-10-03 13:57:23 -07001296bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1297 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001298 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001300 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1302 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1303 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1304 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 return false;
1306 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001307 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001309 /* make sure the table is 32-bit aligned */
1310 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1312 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001313 return false;
1314 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 uint32_t switch_count = switch_insns[1];
1316 int32_t keys_offset, targets_offset;
1317 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1319 /* 0=sig, 1=count, 2/3=firstKey */
1320 targets_offset = 4;
1321 keys_offset = -1;
1322 expected_signature = Instruction::kPackedSwitchSignature;
1323 } else {
1324 /* 0=sig, 1=count, 2..count*2 = keys */
1325 keys_offset = 2;
1326 targets_offset = 2 + 2 * switch_count;
1327 expected_signature = Instruction::kSparseSwitchSignature;
1328 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1332 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 return false;
1334 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001335 /* make sure the end of the switch is in range */
1336 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1338 << switch_offset << ", end "
1339 << (cur_offset + switch_offset + table_size)
1340 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001341 return false;
1342 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 /* for a sparse switch, verify the keys are in ascending order */
1344 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1346 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001347 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1348 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1349 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1351 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 return false;
1353 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 last_key = key;
1355 }
1356 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 for (uint32_t targ = 0; targ < switch_count; targ++) {
1359 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1360 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1361 int32_t abs_offset = cur_offset + offset;
1362 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1363 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1364 << (void*) abs_offset << ") at "
1365 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 return false;
1367 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 insn_flags_[abs_offset].SetBranchTarget();
1369 }
1370 return true;
1371}
1372
1373bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1374 if (vA > 5) {
1375 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1376 return false;
1377 }
1378 uint16_t registers_size = code_item_->registers_size_;
1379 for (uint32_t idx = 0; idx < vA; idx++) {
1380 if (arg[idx] > registers_size) {
1381 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1382 << ") in non-range invoke (> " << registers_size << ")";
1383 return false;
1384 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 }
1386
1387 return true;
1388}
1389
Ian Rogersd81871c2011-10-03 13:57:23 -07001390bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1391 uint16_t registers_size = code_item_->registers_size_;
1392 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1393 // integer overflow when adding them here.
1394 if (vA + vC > registers_size) {
1395 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1396 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 return false;
1398 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 return true;
1400}
1401
Ian Rogersd81871c2011-10-03 13:57:23 -07001402bool DexVerifier::VerifyCodeFlow() {
1403 uint16_t registers_size = code_item_->registers_size_;
1404 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001405
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 if (registers_size * insns_size > 4*1024*1024) {
1407 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1408 << " insns_size=" << insns_size << ")";
1409 }
1410 /* Create and initialize table holding register status */
1411 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1412 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001413
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 work_line_.reset(new RegisterLine(registers_size, this));
1415 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001416
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 /* Initialize register types of method arguments. */
1418 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001419 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1420 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 return false;
1422 }
1423 /* Perform code flow verification. */
1424 if (!CodeFlowVerifyMethod()) {
1425 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001426 }
1427
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 /* Generate a register map and add it to the method. */
1429 ByteArray* map = GenerateGcMap();
1430 if (map == NULL) {
1431 return false; // Not a real failure, but a failure to encode
1432 }
1433 method_->SetGcMap(map);
1434#ifndef NDEBUG
1435 VerifyGcMap();
1436#endif
jeffhaobdb76512011-09-07 11:43:16 -07001437 return true;
1438}
1439
Ian Rogersd81871c2011-10-03 13:57:23 -07001440void DexVerifier::Dump(std::ostream& os) {
1441 if (method_->IsNative()) {
1442 os << "Native method" << std::endl;
1443 return;
jeffhaobdb76512011-09-07 11:43:16 -07001444 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 DCHECK(code_item_ != NULL);
1446 const Instruction* inst = Instruction::At(code_item_->insns_);
1447 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1448 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001449 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1450 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1452 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001453 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 inst = inst->Next();
1456 }
jeffhaobdb76512011-09-07 11:43:16 -07001457}
1458
Ian Rogersd81871c2011-10-03 13:57:23 -07001459static bool IsPrimitiveDescriptor(char descriptor) {
1460 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001461 case 'I':
1462 case 'C':
1463 case 'S':
1464 case 'B':
1465 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001466 case 'F':
1467 case 'D':
1468 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001470 default:
1471 return false;
1472 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001473}
1474
Ian Rogersd81871c2011-10-03 13:57:23 -07001475bool DexVerifier::SetTypesFromSignature() {
1476 RegisterLine* reg_line = reg_table_.GetLine(0);
1477 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1478 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001479
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1481 //Include the "this" pointer.
1482 size_t cur_arg = 0;
1483 if (!method_->IsStatic()) {
1484 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1485 // argument as uninitialized. This restricts field access until the superclass constructor is
1486 // called.
1487 Class* declaring_class = method_->GetDeclaringClass();
1488 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1489 reg_line->SetRegisterType(arg_start + cur_arg,
1490 reg_types_.UninitializedThisArgument(declaring_class));
1491 } else {
1492 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001495 }
1496
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1498 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1499
1500 for (; iterator.HasNext(); iterator.Next()) {
1501 const char* descriptor = iterator.GetDescriptor();
1502 if (descriptor == NULL) {
1503 LOG(FATAL) << "Null descriptor";
1504 }
1505 if (cur_arg >= expected_args) {
1506 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1507 << " args, found more (" << descriptor << ")";
1508 return false;
1509 }
1510 switch (descriptor[0]) {
1511 case 'L':
1512 case '[':
1513 // We assume that reference arguments are initialized. The only way it could be otherwise
1514 // (assuming the caller was verified) is if the current method is <init>, but in that case
1515 // it's effectively considered initialized the instant we reach here (in the sense that we
1516 // can return without doing anything or call virtual methods).
1517 {
1518 const RegType& reg_type =
1519 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001520 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 }
1522 break;
1523 case 'Z':
1524 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1525 break;
1526 case 'C':
1527 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1528 break;
1529 case 'B':
1530 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1531 break;
1532 case 'I':
1533 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1534 break;
1535 case 'S':
1536 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1537 break;
1538 case 'F':
1539 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1540 break;
1541 case 'J':
1542 case 'D': {
1543 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1544 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1545 cur_arg++;
1546 break;
1547 }
1548 default:
1549 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1550 return false;
1551 }
1552 cur_arg++;
1553 }
1554 if (cur_arg != expected_args) {
1555 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1556 return false;
1557 }
1558 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1559 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1560 // format. Only major difference from the method argument format is that 'V' is supported.
1561 bool result;
1562 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1563 result = descriptor[1] == '\0';
1564 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1565 size_t i = 0;
1566 do {
1567 i++;
1568 } while (descriptor[i] == '['); // process leading [
1569 if (descriptor[i] == 'L') { // object array
1570 do {
1571 i++; // find closing ;
1572 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1573 result = descriptor[i] == ';';
1574 } else { // primitive array
1575 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1576 }
1577 } else if (descriptor[0] == 'L') {
1578 // could be more thorough here, but shouldn't be required
1579 size_t i = 0;
1580 do {
1581 i++;
1582 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1583 result = descriptor[i] == ';';
1584 } else {
1585 result = false;
1586 }
1587 if (!result) {
1588 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1589 << descriptor << "'";
1590 }
1591 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001592}
1593
Ian Rogersd81871c2011-10-03 13:57:23 -07001594bool DexVerifier::CodeFlowVerifyMethod() {
1595 const uint16_t* insns = code_item_->insns_;
1596 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001597
jeffhaobdb76512011-09-07 11:43:16 -07001598 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 insn_flags_[0].SetChanged();
1600 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001601
jeffhaobdb76512011-09-07 11:43:16 -07001602 /* Continue until no instructions are marked "changed". */
1603 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1605 uint32_t insn_idx = start_guess;
1606 for (; insn_idx < insns_size; insn_idx++) {
1607 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
1609 }
jeffhaobdb76512011-09-07 11:43:16 -07001610 if (insn_idx == insns_size) {
1611 if (start_guess != 0) {
1612 /* try again, starting from the top */
1613 start_guess = 0;
1614 continue;
1615 } else {
1616 /* all flags are clear */
1617 break;
1618 }
1619 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 // We carry the working set of registers from instruction to instruction. If this address can
1621 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1622 // "changed" flags, we need to load the set of registers from the table.
1623 // Because we always prefer to continue on to the next instruction, we should never have a
1624 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1625 // target.
1626 work_insn_idx_ = insn_idx;
1627 if (insn_flags_[insn_idx].IsBranchTarget()) {
1628 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001629 } else {
1630#ifndef NDEBUG
1631 /*
1632 * Sanity check: retrieve the stored register line (assuming
1633 * a full table) and make sure it actually matches.
1634 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1636 if (register_line != NULL) {
1637 if (work_line_->CompareLine(register_line) != 0) {
1638 Dump(std::cout);
1639 std::cout << info_messages_.str();
1640 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1641 << "@" << (void*)work_insn_idx_ << std::endl
1642 << " work_line=" << *work_line_ << std::endl
1643 << " expected=" << *register_line;
1644 }
jeffhaobdb76512011-09-07 11:43:16 -07001645 }
1646#endif
1647 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 if (!CodeFlowVerifyInstruction(&start_guess)) {
1649 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001650 return false;
1651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 insn_flags_[insn_idx].SetVisited();
1654 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001655 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001656
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001658 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001660 * (besides the wasted space), but it indicates a flaw somewhere
1661 * down the line, possibly in the verifier.
1662 *
1663 * If we've substituted "always throw" instructions into the stream,
1664 * we are almost certainly going to have some dead code.
1665 */
1666 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 uint32_t insn_idx = 0;
1668 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001669 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001670 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001671 * may or may not be preceded by a padding NOP (for alignment).
1672 */
1673 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1674 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1675 insns[insn_idx] == Instruction::kArrayDataSignature ||
1676 (insns[insn_idx] == Instruction::NOP &&
1677 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1678 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1679 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001681 }
1682
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001684 if (dead_start < 0)
1685 dead_start = insn_idx;
1686 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001688 dead_start = -1;
1689 }
1690 }
1691 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001693 }
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 return true;
1696}
1697
Ian Rogersd81871c2011-10-03 13:57:23 -07001698bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001699#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001701 gDvm.verifierStats.instrsReexamined++;
1702 } else {
1703 gDvm.verifierStats.instrsExamined++;
1704 }
1705#endif
1706
1707 /*
1708 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001709 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001710 * control to another statement:
1711 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001712 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001713 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001714 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001715 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001716 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001717 * throw an exception that is handled by an encompassing "try"
1718 * block.
1719 *
1720 * We can also return, in which case there is no successor instruction
1721 * from this point.
1722 *
1723 * The behavior can be determined from the OpcodeFlags.
1724 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1726 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001727 Instruction::DecodedInstruction dec_insn(inst);
1728 int opcode_flag = inst->Flag();
1729
jeffhaobdb76512011-09-07 11:43:16 -07001730 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001731 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001732 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001734 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1735 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737
1738 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001739 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001740 * can throw an exception, we will copy/merge this into the "catch"
1741 * address rather than work_line, because we don't want the result
1742 * from the "successful" code path (e.g. a check-cast that "improves"
1743 * a type) to be visible to the exception handler.
1744 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1746 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001747 } else {
1748#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001750#endif
1751 }
1752
1753 switch (dec_insn.opcode_) {
1754 case Instruction::NOP:
1755 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001756 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001757 * a signature that looks like a NOP; if we see one of these in
1758 * the course of executing code then we have a problem.
1759 */
1760 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001762 }
1763 break;
1764
1765 case Instruction::MOVE:
1766 case Instruction::MOVE_FROM16:
1767 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::MOVE_WIDE:
1771 case Instruction::MOVE_WIDE_FROM16:
1772 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775 case Instruction::MOVE_OBJECT:
1776 case Instruction::MOVE_OBJECT_FROM16:
1777 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
1780
1781 /*
1782 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001783 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001784 * might want to hold the result in an actual CPU register, so the
1785 * Dalvik spec requires that these only appear immediately after an
1786 * invoke or filled-new-array.
1787 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001788 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001789 * redundant with the reset done below, but it can make the debug info
1790 * easier to read in some cases.)
1791 */
1792 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
1801
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001803 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 * This statement can only appear as the first instruction in an exception handler (though not
1805 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001806 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001807 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001809 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001811 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
1814 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1818 if (!GetMethodReturnType().IsUnknown()) {
1819 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1820 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 }
1822 break;
1823 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001825 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 const RegType& return_type = GetMethodReturnType();
1827 if (!return_type.IsCategory1Types()) {
1828 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1829 } else {
1830 // Compilers may generate synthetic functions that write byte values into boolean fields.
1831 // Also, it may use integer values for boolean, byte, short, and character return types.
1832 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1833 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1834 ((return_type.IsBoolean() || return_type.IsByte() ||
1835 return_type.IsShort() || return_type.IsChar()) &&
1836 src_type.IsInteger()));
1837 /* check the register contents */
1838 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1839 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001840 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 }
jeffhaobdb76512011-09-07 11:43:16 -07001842 }
1843 }
1844 break;
1845 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001847 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 const RegType& return_type = GetMethodReturnType();
1849 if (!return_type.IsCategory2Types()) {
1850 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1851 } else {
1852 /* check the register contents */
1853 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1854 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001855 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
1858 }
1859 break;
1860 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1862 const RegType& return_type = GetMethodReturnType();
1863 if (!return_type.IsReferenceTypes()) {
1864 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1865 } else {
1866 /* return_type is the *expected* return type, not register value */
1867 DCHECK(!return_type.IsZero());
1868 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001869 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1870 // Disallow returning uninitialized values and verify that the reference in vAA is an
1871 // instance of the "return_type"
1872 if (reg_type.IsUninitializedTypes()) {
1873 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1874 } else if (!return_type.IsAssignableFrom(reg_type)) {
1875 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1876 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001877 }
1878 }
1879 }
1880 break;
1881
1882 case Instruction::CONST_4:
1883 case Instruction::CONST_16:
1884 case Instruction::CONST:
1885 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
1888 case Instruction::CONST_HIGH16:
1889 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 work_line_->SetRegisterType(dec_insn.vA_,
1891 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::CONST_WIDE_16:
1894 case Instruction::CONST_WIDE_32:
1895 case Instruction::CONST_WIDE:
1896 case Instruction::CONST_WIDE_HIGH16:
1897 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900 case Instruction::CONST_STRING:
1901 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001903 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001905 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001907 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001909 fail_messages_ << "unable to resolve const-class " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 << " (" << bad_class_desc << ") in "
1911 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1912 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001913 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001915 }
1916 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
1921 case Instruction::MONITOR_EXIT:
1922 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001923 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001924 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001925 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001926 * to the need to handle asynchronous exceptions, a now-deprecated
1927 * feature that Dalvik doesn't support.)
1928 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001929 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001930 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001932 * structured locking checks are working, the former would have
1933 * failed on the -enter instruction, and the latter is impossible.
1934 *
1935 * This is fortunate, because issue 3221411 prevents us from
1936 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001937 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001938 * some catch blocks (which will show up as "dead" code when
1939 * we skip them here); if we can't, then the code path could be
1940 * "live" so we still need to check it.
1941 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 opcode_flag &= ~Instruction::kThrow;
1943 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001947 /*
1948 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001949 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001950 * we don't try to address it.)
1951 *
1952 * If it fails, an exception is thrown, which we deal with later
1953 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1954 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001956 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001958 fail_messages_ << "unable to resolve check-cast " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 << " (" << bad_class_desc << ") in "
1960 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1961 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001962 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1964 if (!orig_type.IsReferenceTypes()) {
1965 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1966 } else {
1967 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 }
1970 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 }
1972 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001973 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1975 if (!tmp_type.IsReferenceTypes()) {
1976 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001977 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 /* make sure we can resolve the class; access check is important */
1979 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1980 if (res_class == NULL) {
1981 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001982 fail_messages_ << "unable to resolve instance of " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 << " (" << bad_class_desc << ") in "
1984 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1985 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1986 } else {
1987 /* result is boolean */
1988 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
1993 case Instruction::ARRAY_LENGTH: {
1994 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
1995 if (failure_ == VERIFY_ERROR_NONE) {
1996 if (res_class != NULL && !res_class->IsArrayClass()) {
1997 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
1998 } else {
1999 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2000 }
2001 }
2002 break;
2003 }
2004 case Instruction::NEW_INSTANCE: {
2005 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002006 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002008 fail_messages_ << "unable to resolve new-instance " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 << " (" << bad_class_desc << ") in "
2010 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2011 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2012 } else {
2013 /* can't create an instance of an interface or abstract class */
2014 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2015 Fail(VERIFY_ERROR_INSTANTIATION)
2016 << "new-instance on primitive, interface or abstract class"
2017 << PrettyDescriptor(res_class->GetDescriptor());
2018 } else {
2019 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2020 // Any registers holding previous allocations from this address that have not yet been
2021 // initialized must be marked invalid.
2022 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2023
2024 /* add the new uninitialized reference to the register state */
2025 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2026 }
2027 }
2028 break;
2029 }
2030 case Instruction::NEW_ARRAY: {
2031 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2032 if (res_class == NULL) {
2033 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002034 fail_messages_ << "unable to resolve new-array " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 << " (" << bad_class_desc << ") in "
2036 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2037 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002038 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002040 } else {
2041 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002043 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002045 }
2046 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 }
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2050 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002051 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002053 fail_messages_ << "unable to resolve filled-array " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 << " (" << bad_class_desc << ") in "
2055 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2056 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002057 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002059 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002060 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002061 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002063 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002065 just_set_result = true;
2066 }
2067 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 }
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::CMPL_FLOAT:
2070 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2072 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2073 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075 case Instruction::CMPL_DOUBLE:
2076 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2078 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2079 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2083 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2084 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 case Instruction::THROW: {
2087 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2088 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2089 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2090 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2091 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002092 }
2093 }
2094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::GOTO:
2097 case Instruction::GOTO_16:
2098 case Instruction::GOTO_32:
2099 /* no effect on or use of registers */
2100 break;
2101
2102 case Instruction::PACKED_SWITCH:
2103 case Instruction::SPARSE_SWITCH:
2104 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 case Instruction::FILL_ARRAY_DATA: {
2109 /* Similar to the verification done for APUT */
2110 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2111 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002112 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 if (res_class != NULL) {
2114 Class* component_type = res_class->GetComponentType();
2115 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2116 component_type->IsPrimitiveVoid()) {
2117 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2118 << PrettyDescriptor(res_class->GetDescriptor());
2119 } else {
2120 const RegType& value_type = reg_types_.FromClass(component_type);
2121 DCHECK(!value_type.IsUnknown());
2122 // Now verify if the element width in the table matches the element width declared in
2123 // the array
2124 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2125 if (array_data[0] != Instruction::kArrayDataSignature) {
2126 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2127 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002128 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 // Since we don't compress the data in Dex, expect to see equal width of data stored
2130 // in the table and expected from the array class.
2131 if (array_data[1] != elem_width) {
2132 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2133 << " vs " << elem_width << ")";
2134 }
2135 }
2136 }
jeffhaobdb76512011-09-07 11:43:16 -07002137 }
2138 }
2139 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 }
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 case Instruction::IF_NE: {
2143 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2144 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2145 bool mismatch = false;
2146 if (reg_type1.IsZero()) { // zero then integral or reference expected
2147 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2148 } else if (reg_type1.IsReferenceTypes()) { // both references?
2149 mismatch = !reg_type2.IsReferenceTypes();
2150 } else { // both integral?
2151 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2152 }
2153 if (mismatch) {
2154 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2155 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002156 }
2157 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 }
jeffhaobdb76512011-09-07 11:43:16 -07002159 case Instruction::IF_LT:
2160 case Instruction::IF_GE:
2161 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 case Instruction::IF_LE: {
2163 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2164 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2165 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2166 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2167 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002168 }
2169 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 }
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 case Instruction::IF_NEZ: {
2173 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2174 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2175 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 case Instruction::IF_LTZ:
2180 case Instruction::IF_GEZ:
2181 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 case Instruction::IF_LEZ: {
2183 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2184 if (!reg_type.IsIntegralTypes()) {
2185 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2186 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 }
jeffhaobdb76512011-09-07 11:43:16 -07002190 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2192 break;
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2195 break;
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 VerifyAGet(dec_insn, reg_types_.Char(), true);
2198 break;
jeffhaobdb76512011-09-07 11:43:16 -07002199 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 case Instruction::AGET:
2203 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2204 break;
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 VerifyAGet(dec_insn, reg_types_.Long(), true);
2207 break;
2208 case Instruction::AGET_OBJECT:
2209 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 case Instruction::APUT_BOOLEAN:
2213 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2214 break;
2215 case Instruction::APUT_BYTE:
2216 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2217 break;
2218 case Instruction::APUT_CHAR:
2219 VerifyAPut(dec_insn, reg_types_.Char(), true);
2220 break;
2221 case Instruction::APUT_SHORT:
2222 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233
jeffhaobdb76512011-09-07 11:43:16 -07002234 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002235 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 break;
jeffhaobdb76512011-09-07 11:43:16 -07002237 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002238 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 break;
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002241 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 break;
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002244 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 break;
2246 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002247 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002250 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002253 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002257 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 break;
2259 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002260 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 break;
2262 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002263 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 break;
2265 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002266 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002269 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002272 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002275 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277
jeffhaobdb76512011-09-07 11:43:16 -07002278 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002279 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 break;
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002282 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002285 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002288 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
2290 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
2299
2300 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002301 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 break;
2303 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002304 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
2306 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002307 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
2309 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002310 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321
2322 case Instruction::INVOKE_VIRTUAL:
2323 case Instruction::INVOKE_VIRTUAL_RANGE:
2324 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 case Instruction::INVOKE_SUPER_RANGE: {
2326 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2327 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2328 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2329 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2330 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2331 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers9074b992011-10-26 17:41:55 -07002332 const RegType& return_type =
2333 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2334 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 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 Rogers9074b992011-10-26 17:41:55 -07002392 const RegType& return_type =
2393 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2394 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002396 just_set_result = true;
2397 }
2398 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 }
jeffhaobdb76512011-09-07 11:43:16 -07002400 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 case Instruction::INVOKE_STATIC_RANGE: {
2402 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2403 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2404 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers9074b992011-10-26 17:41:55 -07002405 const RegType& return_type =
2406 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2407 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 work_line_->SetResultRegisterType(return_type);
2409 just_set_result = true;
2410 }
jeffhaobdb76512011-09-07 11:43:16 -07002411 }
2412 break;
2413 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 case Instruction::INVOKE_INTERFACE_RANGE: {
2415 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2416 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2417 if (failure_ == VERIFY_ERROR_NONE) {
2418 Class* called_interface = abs_method->GetDeclaringClass();
2419 if (!called_interface->IsInterface()) {
2420 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2421 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
jeffhaobdb76512011-09-07 11:43:16 -07002423 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 /* Get the type of the "this" arg, which should either be a sub-interface of called
2425 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002426 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2428 if (failure_ == VERIFY_ERROR_NONE) {
2429 if (this_type.IsZero()) {
2430 /* null pointer always passes (and always fails at runtime) */
2431 } else {
Ian Rogersb94a27b2011-10-26 00:33:41 -07002432 if (this_type.IsUninitializedTypes()) {
2433 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2434 << this_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 break;
2436 }
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002437 // In the past we have tried to assert that "called_interface" is assignable
2438 // from "this_type.GetClass()", however, as we do an imprecise Join
2439 // (RegType::JoinClass) we don't have full information on what interfaces are
2440 // implemented by "this_type". For example, two classes may implement the same
2441 // interfaces and have a common parent that doesn't implement the interface. The
2442 // join will set "this_type" to the parent class and a test that this implements
2443 // the interface will incorrectly fail.
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 }
jeffhaobdb76512011-09-07 11:43:16 -07002445 }
2446 }
jeffhaobdb76512011-09-07 11:43:16 -07002447 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 * We don't have an object instance, so we can't find the concrete method. However, all of
2449 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002450 */
Ian Rogers9074b992011-10-26 17:41:55 -07002451 const RegType& return_type =
2452 reg_types_.FromDescriptor(abs_method->GetDeclaringClass()->GetClassLoader(),
2453 abs_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002455 just_set_result = true;
2456 }
2457 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 }
jeffhaobdb76512011-09-07 11:43:16 -07002459 case Instruction::NEG_INT:
2460 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::NEG_LONG:
2464 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518
2519 case Instruction::ADD_INT:
2520 case Instruction::SUB_INT:
2521 case Instruction::MUL_INT:
2522 case Instruction::REM_INT:
2523 case Instruction::DIV_INT:
2524 case Instruction::SHL_INT:
2525 case Instruction::SHR_INT:
2526 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::AND_INT:
2530 case Instruction::OR_INT:
2531 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::ADD_LONG:
2535 case Instruction::SUB_LONG:
2536 case Instruction::MUL_LONG:
2537 case Instruction::DIV_LONG:
2538 case Instruction::REM_LONG:
2539 case Instruction::AND_LONG:
2540 case Instruction::OR_LONG:
2541 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::SHL_LONG:
2545 case Instruction::SHR_LONG:
2546 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 /* shift distance is Int, making these different from other binary operations */
2548 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550 case Instruction::ADD_FLOAT:
2551 case Instruction::SUB_FLOAT:
2552 case Instruction::MUL_FLOAT:
2553 case Instruction::DIV_FLOAT:
2554 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::ADD_DOUBLE:
2558 case Instruction::SUB_DOUBLE:
2559 case Instruction::MUL_DOUBLE:
2560 case Instruction::DIV_DOUBLE:
2561 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::ADD_INT_2ADDR:
2565 case Instruction::SUB_INT_2ADDR:
2566 case Instruction::MUL_INT_2ADDR:
2567 case Instruction::REM_INT_2ADDR:
2568 case Instruction::SHL_INT_2ADDR:
2569 case Instruction::SHR_INT_2ADDR:
2570 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::AND_INT_2ADDR:
2574 case Instruction::OR_INT_2ADDR:
2575 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::ADD_LONG_2ADDR:
2582 case Instruction::SUB_LONG_2ADDR:
2583 case Instruction::MUL_LONG_2ADDR:
2584 case Instruction::DIV_LONG_2ADDR:
2585 case Instruction::REM_LONG_2ADDR:
2586 case Instruction::AND_LONG_2ADDR:
2587 case Instruction::OR_LONG_2ADDR:
2588 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::SHL_LONG_2ADDR:
2592 case Instruction::SHR_LONG_2ADDR:
2593 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 case Instruction::ADD_FLOAT_2ADDR:
2597 case Instruction::SUB_FLOAT_2ADDR:
2598 case Instruction::MUL_FLOAT_2ADDR:
2599 case Instruction::DIV_FLOAT_2ADDR:
2600 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::ADD_DOUBLE_2ADDR:
2604 case Instruction::SUB_DOUBLE_2ADDR:
2605 case Instruction::MUL_DOUBLE_2ADDR:
2606 case Instruction::DIV_DOUBLE_2ADDR:
2607 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::ADD_INT_LIT16:
2611 case Instruction::RSUB_INT:
2612 case Instruction::MUL_INT_LIT16:
2613 case Instruction::DIV_INT_LIT16:
2614 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::AND_INT_LIT16:
2618 case Instruction::OR_INT_LIT16:
2619 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::ADD_INT_LIT8:
2623 case Instruction::RSUB_INT_LIT8:
2624 case Instruction::MUL_INT_LIT8:
2625 case Instruction::DIV_INT_LIT8:
2626 case Instruction::REM_INT_LIT8:
2627 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002628 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002629 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::AND_INT_LIT8:
2633 case Instruction::OR_INT_LIT8:
2634 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637
2638 /*
2639 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002640 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002641 * inserted in the course of verification, we can expect to see it here.
2642 */
jeffhaob4df5142011-09-19 20:25:32 -07002643 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002647 case Instruction::UNUSED_EE:
2648 case Instruction::UNUSED_EF:
2649 case Instruction::UNUSED_F2:
2650 case Instruction::UNUSED_F3:
2651 case Instruction::UNUSED_F4:
2652 case Instruction::UNUSED_F5:
2653 case Instruction::UNUSED_F6:
2654 case Instruction::UNUSED_F7:
2655 case Instruction::UNUSED_F8:
2656 case Instruction::UNUSED_F9:
2657 case Instruction::UNUSED_FA:
2658 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002659 case Instruction::UNUSED_F0:
2660 case Instruction::UNUSED_F1:
2661 case Instruction::UNUSED_E3:
2662 case Instruction::UNUSED_E8:
2663 case Instruction::UNUSED_E7:
2664 case Instruction::UNUSED_E4:
2665 case Instruction::UNUSED_E9:
2666 case Instruction::UNUSED_FC:
2667 case Instruction::UNUSED_E5:
2668 case Instruction::UNUSED_EA:
2669 case Instruction::UNUSED_FD:
2670 case Instruction::UNUSED_E6:
2671 case Instruction::UNUSED_EB:
2672 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002673 case Instruction::UNUSED_3E:
2674 case Instruction::UNUSED_3F:
2675 case Instruction::UNUSED_40:
2676 case Instruction::UNUSED_41:
2677 case Instruction::UNUSED_42:
2678 case Instruction::UNUSED_43:
2679 case Instruction::UNUSED_73:
2680 case Instruction::UNUSED_79:
2681 case Instruction::UNUSED_7A:
2682 case Instruction::UNUSED_EC:
2683 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002684 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686
2687 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002688 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002689 * complain if an instruction is missing (which is desirable).
2690 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002692
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 if (failure_ != VERIFY_ERROR_NONE) {
2694 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002695 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002696 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002697 return false;
2698 } else {
2699 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002700 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002702 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002704 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002706 opcode_flag = Instruction::kThrow;
2707 }
2708 }
jeffhaobdb76512011-09-07 11:43:16 -07002709 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 * If we didn't just set the result register, clear it out. This ensures that you can only use
2711 * "move-result" immediately after the result is set. (We could check this statically, but it's
2712 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002713 */
2714 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002716 }
2717
jeffhaoa0a764a2011-09-16 10:43:38 -07002718 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002719 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2721 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2722 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002723 return false;
2724 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2726 // next instruction isn't one.
2727 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002728 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 }
2730 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2731 if (next_line != NULL) {
2732 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2733 // needed.
2734 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002735 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 }
jeffhaobdb76512011-09-07 11:43:16 -07002737 } else {
2738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * We're not recording register data for the next instruction, so we don't know what the prior
2740 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002741 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002743 }
2744 }
2745
2746 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002747 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002748 *
2749 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002750 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002751 * somebody could get a reference field, check it for zero, and if the
2752 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002753 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002754 * that, and will reject the code.
2755 *
2756 * TODO: avoid re-fetching the branch target
2757 */
2758 if ((opcode_flag & Instruction::kBranch) != 0) {
2759 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002761 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002763 return false;
2764 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002765 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002767 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 }
jeffhaobdb76512011-09-07 11:43:16 -07002769 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002771 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 }
jeffhaobdb76512011-09-07 11:43:16 -07002773 }
2774
2775 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002776 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002777 *
2778 * We've already verified that the table is structurally sound, so we
2779 * just need to walk through and tag the targets.
2780 */
2781 if ((opcode_flag & Instruction::kSwitch) != 0) {
2782 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2783 const uint16_t* switch_insns = insns + offset_to_switch;
2784 int switch_count = switch_insns[1];
2785 int offset_to_targets, targ;
2786
2787 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2788 /* 0 = sig, 1 = count, 2/3 = first key */
2789 offset_to_targets = 4;
2790 } else {
2791 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002792 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002793 offset_to_targets = 2 + 2 * switch_count;
2794 }
2795
2796 /* verify each switch target */
2797 for (targ = 0; targ < switch_count; targ++) {
2798 int offset;
2799 uint32_t abs_offset;
2800
2801 /* offsets are 32-bit, and only partly endian-swapped */
2802 offset = switch_insns[offset_to_targets + targ * 2] |
2803 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 abs_offset = work_insn_idx_ + offset;
2805 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2806 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002807 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 }
2809 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002810 return false;
2811 }
2812 }
2813
2814 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2816 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002817 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2819 bool within_catch_all = false;
2820 DexFile::CatchHandlerIterator iterator =
2821 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002822
2823 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2825 within_catch_all = true;
2826 }
jeffhaobdb76512011-09-07 11:43:16 -07002827 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2829 * "work_regs", because at runtime the exception will be thrown before the instruction
2830 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002831 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002833 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 }
jeffhaobdb76512011-09-07 11:43:16 -07002835 }
2836
2837 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2839 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002840 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002842 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 * The state in work_line reflects the post-execution state. If the current instruction is a
2844 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002845 * it will do so before grabbing the lock).
2846 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2848 Fail(VERIFY_ERROR_GENERIC)
2849 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002850 return false;
2851 }
2852 }
2853 }
2854
jeffhaod1f0fde2011-09-08 17:25:33 -07002855 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if ((opcode_flag & Instruction::kReturn) != 0) {
2857 if(!work_line_->VerifyMonitorStackEmpty()) {
2858 return false;
2859 }
jeffhaobdb76512011-09-07 11:43:16 -07002860 }
2861
2862 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002863 * Update start_guess. Advance to the next instruction of that's
2864 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002865 * neither of those exists we're in a return or throw; leave start_guess
2866 * alone and let the caller sort it out.
2867 */
2868 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002870 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2871 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002873 }
2874
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2876 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002877
2878 return true;
2879}
2880
Ian Rogersd81871c2011-10-03 13:57:23 -07002881Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2882 const Class* referrer = method_->GetDeclaringClass();
2883 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2884 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002885
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 if (res_class == NULL) {
2887 Thread::Current()->ClearException();
2888 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2889 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2890 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2891 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2892 << res_class->GetDescriptor()->ToModifiedUtf8();
2893 }
2894 return res_class;
2895}
2896
2897Class* DexVerifier::GetCaughtExceptionType() {
2898 Class* common_super = NULL;
2899 if (code_item_->tries_size_ != 0) {
2900 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2901 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2902 for (uint32_t i = 0; i < handlers_size; i++) {
2903 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2904 for (; !iterator.HasNext(); iterator.Next()) {
2905 DexFile::CatchHandlerItem handler = iterator.Get();
2906 if (handler.address_ == (uint32_t) work_insn_idx_) {
2907 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2908 common_super = JavaLangThrowable();
2909 } else {
2910 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2911 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2912 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2913 * test, so is essentially harmless.
2914 */
2915 if (klass == NULL) {
2916 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2917 << handler.type_idx_ << " ("
2918 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2919 return NULL;
2920 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2921 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2922 return NULL;
2923 } else if (common_super == NULL) {
2924 common_super = klass;
2925 } else {
2926 common_super = RegType::ClassJoin(common_super, klass);
2927 }
2928 }
2929 }
2930 }
2931 handlers_ptr = iterator.GetData();
2932 }
2933 }
2934 if (common_super == NULL) {
2935 /* no catch blocks, or no catches with classes we can find */
2936 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2937 }
2938 return common_super;
2939}
2940
2941Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2942 Class* referrer = method_->GetDeclaringClass();
2943 DexCache* dex_cache = referrer->GetDexCache();
2944 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2945 if (res_method == NULL) {
2946 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2947 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2948 if (klass == NULL) {
2949 DCHECK(failure_ != VERIFY_ERROR_NONE);
2950 return NULL;
2951 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002952 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002953 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2954 if (is_direct) {
2955 res_method = klass->FindDirectMethod(name, signature);
2956 } else if (klass->IsInterface()) {
2957 res_method = klass->FindInterfaceMethod(name, signature);
2958 } else {
2959 res_method = klass->FindVirtualMethod(name, signature);
2960 }
2961 if (res_method != NULL) {
2962 dex_cache->SetResolvedMethod(method_idx, res_method);
2963 } else {
2964 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2965 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2966 << " " << signature;
2967 return NULL;
2968 }
2969 }
2970 /* Check if access is allowed. */
2971 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2972 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2973 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2974 return NULL;
2975 }
2976 return res_method;
2977}
2978
2979Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2980 MethodType method_type, bool is_range, bool is_super) {
2981 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2982 // we're making.
2983 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2984 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2985 if (res_method == NULL) {
2986 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2987 const char* method_name = dex_file_->GetMethodName(method_id);
2988 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002989 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002990 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
2991 fail_messages_ << "unable to resolve method " << dec_insn.vB_ << ": "
2992 << class_descriptor << "." << method_name << " " << method_signature;
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 return NULL;
2994 }
2995 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2996 // enforce them here.
2997 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
2998 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
2999 << PrettyMethod(res_method);
3000 return NULL;
3001 }
3002 // See if the method type implied by the invoke instruction matches the access flags for the
3003 // target method.
3004 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3005 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3006 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3007 ) {
3008 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3009 << PrettyMethod(res_method);
3010 return NULL;
3011 }
3012 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3013 // has a vtable entry for the target method.
3014 if (is_super) {
3015 DCHECK(method_type == METHOD_VIRTUAL);
3016 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3017 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3018 if (super == NULL) { // Only Object has no super class
3019 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3020 << " to super " << PrettyMethod(res_method);
3021 } else {
3022 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3023 << " to super " << PrettyDescriptor(super->GetDescriptor())
3024 << "." << res_method->GetName()->ToModifiedUtf8()
3025 << " " << res_method->GetSignature()->ToModifiedUtf8();
3026
3027 }
3028 return NULL;
3029 }
3030 }
3031 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3032 // match the call to the signature. Also, we might might be calling through an abstract method
3033 // definition (which doesn't have register count values).
3034 int expected_args = dec_insn.vA_;
3035 /* caught by static verifier */
3036 DCHECK(is_range || expected_args <= 5);
3037 if (expected_args > code_item_->outs_size_) {
3038 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3039 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3040 return NULL;
3041 }
3042 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3043 if (sig[0] != '(') {
3044 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3045 << " as descriptor doesn't start with '(': " << sig;
3046 return NULL;
3047 }
jeffhaobdb76512011-09-07 11:43:16 -07003048 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 * Check the "this" argument, which must be an instance of the class
3050 * that declared the method. For an interface class, we don't do the
3051 * full interface merge, so we can't do a rigorous check here (which
3052 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003053 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 int actual_args = 0;
3055 if (!res_method->IsStatic()) {
3056 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3057 if (failure_ != VERIFY_ERROR_NONE) {
3058 return NULL;
3059 }
3060 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3061 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3062 return NULL;
3063 }
3064 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003065 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3066 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3067 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3068 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 return NULL;
3070 }
3071 }
3072 actual_args++;
3073 }
3074 /*
3075 * Process the target method's signature. This signature may or may not
3076 * have been verified, so we can't assume it's properly formed.
3077 */
3078 size_t sig_offset = 0;
3079 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3080 if (actual_args >= expected_args) {
3081 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3082 << "'. Expected " << expected_args << " args, found more ("
3083 << sig.substr(sig_offset) << ")";
3084 return NULL;
3085 }
3086 std::string descriptor;
3087 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3088 size_t end;
3089 if (sig[sig_offset] == 'L') {
3090 end = sig.find(';', sig_offset);
3091 } else {
3092 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3093 if (sig[end] == 'L') {
3094 end = sig.find(';', end);
3095 }
3096 }
3097 if (end == std::string::npos) {
3098 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3099 << "bad signature component '" << sig << "' (missing ';')";
3100 return NULL;
3101 }
3102 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3103 sig_offset = end;
3104 } else {
3105 descriptor = sig[sig_offset];
3106 }
3107 const RegType& reg_type =
3108 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003109 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3110 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3111 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 }
3113 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3114 }
3115 if (sig[sig_offset] != ')') {
3116 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3117 return NULL;
3118 }
3119 if (actual_args != expected_args) {
3120 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3121 << " expected " << expected_args << " args, found " << actual_args;
3122 return NULL;
3123 } else {
3124 return res_method;
3125 }
3126}
3127
3128void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3129 const RegType& insn_type, bool is_primitive) {
3130 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3131 if (!index_type.IsArrayIndexTypes()) {
3132 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3133 } else {
3134 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3135 if (failure_ == VERIFY_ERROR_NONE) {
3136 if (array_class == NULL) {
3137 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3138 // instruction type. TODO: have a proper notion of bottom here.
3139 if (!is_primitive || insn_type.IsCategory1Types()) {
3140 // Reference or category 1
3141 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3142 } else {
3143 // Category 2
3144 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3145 }
3146 } else {
3147 /* verify the class */
3148 Class* component_class = array_class->GetComponentType();
3149 const RegType& component_type = reg_types_.FromClass(component_class);
3150 if (!array_class->IsArrayClass()) {
3151 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3152 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3153 } else if (component_class->IsPrimitive() && !is_primitive) {
3154 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3155 << PrettyDescriptor(array_class->GetDescriptor())
3156 << " source for aget-object";
3157 } else if (!component_class->IsPrimitive() && is_primitive) {
3158 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3159 << PrettyDescriptor(array_class->GetDescriptor())
3160 << " source for category 1 aget";
3161 } else if (is_primitive && !insn_type.Equals(component_type) &&
3162 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3163 (insn_type.IsLong() && component_type.IsDouble()))) {
3164 Fail(VERIFY_ERROR_GENERIC) << "array type "
3165 << PrettyDescriptor(array_class->GetDescriptor())
3166 << " incompatible with aget of type " << insn_type;
3167 } else {
3168 // Use knowledge of the field type which is stronger than the type inferred from the
3169 // instruction, which can't differentiate object types and ints from floats, longs from
3170 // doubles.
3171 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3172 }
3173 }
3174 }
3175 }
3176}
3177
3178void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3179 const RegType& insn_type, bool is_primitive) {
3180 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3181 if (!index_type.IsArrayIndexTypes()) {
3182 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3183 } else {
3184 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3185 if (failure_ == VERIFY_ERROR_NONE) {
3186 if (array_class == NULL) {
3187 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3188 // instruction type.
3189 } else {
3190 /* verify the class */
3191 Class* component_class = array_class->GetComponentType();
3192 const RegType& component_type = reg_types_.FromClass(component_class);
3193 if (!array_class->IsArrayClass()) {
3194 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3195 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3196 } else if (component_class->IsPrimitive() && !is_primitive) {
3197 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3198 << PrettyDescriptor(array_class->GetDescriptor())
3199 << " source for aput-object";
3200 } else if (!component_class->IsPrimitive() && is_primitive) {
3201 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3202 << PrettyDescriptor(array_class->GetDescriptor())
3203 << " source for category 1 aput";
3204 } else if (is_primitive && !insn_type.Equals(component_type) &&
3205 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3206 (insn_type.IsLong() && component_type.IsDouble()))) {
3207 Fail(VERIFY_ERROR_GENERIC) << "array type "
3208 << PrettyDescriptor(array_class->GetDescriptor())
3209 << " incompatible with aput of type " << insn_type;
3210 } else {
3211 // The instruction agrees with the type of array, confirm the value to be stored does too
3212 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3213 }
3214 }
3215 }
3216 }
3217}
3218
3219Field* DexVerifier::GetStaticField(int field_idx) {
3220 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3221 if (field == NULL) {
3222 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3223 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3224 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003225 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 DCHECK(Thread::Current()->IsExceptionPending());
3227 Thread::Current()->ClearException();
3228 return NULL;
3229 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3230 field->GetAccessFlags())) {
3231 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3232 << " from " << PrettyClass(method_->GetDeclaringClass());
3233 return NULL;
3234 } else if (!field->IsStatic()) {
3235 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3236 return NULL;
3237 } else {
3238 return field;
3239 }
3240}
3241
Ian Rogersd81871c2011-10-03 13:57:23 -07003242Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3243 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3244 if (field == NULL) {
3245 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3246 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3247 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003248 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 DCHECK(Thread::Current()->IsExceptionPending());
3250 Thread::Current()->ClearException();
3251 return NULL;
3252 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3253 field->GetAccessFlags())) {
3254 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3255 << " from " << PrettyClass(method_->GetDeclaringClass());
3256 return NULL;
3257 } else if (field->IsStatic()) {
3258 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3259 << " to not be static";
3260 return NULL;
3261 } else if (obj_type.IsZero()) {
3262 // Cannot infer and check type, however, access will cause null pointer exception
3263 return field;
3264 } else if(obj_type.IsUninitializedReference() &&
3265 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3266 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3267 // Field accesses through uninitialized references are only allowable for constructors where
3268 // the field is declared in this class
3269 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3270 << " of a not fully initialized object within the context of "
3271 << PrettyMethod(method_);
3272 return NULL;
3273 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3274 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3275 // of C1. For resolution to occur the declared class of the field must be compatible with
3276 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3277 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3278 << " from object of type " << PrettyClass(obj_type.GetClass());
3279 return NULL;
3280 } else {
3281 return field;
3282 }
3283}
3284
Ian Rogersb94a27b2011-10-26 00:33:41 -07003285void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3286 const RegType& insn_type, bool is_primitive, bool is_static) {
3287 Field* field;
3288 if (is_static) {
3289 field = GetStaticField(dec_insn.vB_);
3290 } else {
3291 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3292 field = GetInstanceField(object_type, dec_insn.vC_);
3293 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 if (field != NULL) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003295 const RegType& field_type =
3296 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3297 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003299 if (field_type.Equals(insn_type) ||
3300 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3301 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 // expected that read is of the correct primitive type or that int reads are reading
3303 // floats or long reads are reading doubles
3304 } else {
3305 // This is a global failure rather than a class change failure as the instructions and
3306 // the descriptors for the type should have been consistent within the same file at
3307 // compile time
3308 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003309 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003310 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 return;
3312 }
3313 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003314 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003316 << " to be compatible with type '" << insn_type
3317 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003318 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 return;
3320 }
3321 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003322 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 }
3324}
3325
Ian Rogersb94a27b2011-10-26 00:33:41 -07003326void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3327 const RegType& insn_type, bool is_primitive, bool is_static) {
3328 Field* field;
3329 if (is_static) {
3330 field = GetStaticField(dec_insn.vB_);
3331 } else {
3332 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3333 field = GetInstanceField(object_type, dec_insn.vC_);
3334 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3337 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3338 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3339 return;
3340 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003341 const RegType& field_type =
3342 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3343 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003345 // Primitive field assignability rules are weaker than regular assignability rules
3346 bool instruction_compatible;
3347 bool value_compatible;
3348 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3349 if (field_type.IsIntegralTypes()) {
3350 instruction_compatible = insn_type.IsIntegralTypes();
3351 value_compatible = value_type.IsIntegralTypes();
3352 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003353 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003354 value_compatible = value_type.IsFloatTypes();
3355 } else if (field_type.IsLong()) {
3356 instruction_compatible = insn_type.IsLong();
3357 value_compatible = value_type.IsLongTypes();
3358 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003359 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003360 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003362 instruction_compatible = false; // reference field with primitive store
3363 value_compatible = false; // unused
3364 }
3365 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 // This is a global failure rather than a class change failure as the instructions and
3367 // the descriptors for the type should have been consistent within the same file at
3368 // compile time
3369 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003370 << " to be of type '" << insn_type
3371 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003372 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 return;
3374 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003375 if (!value_compatible) {
3376 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3377 << " of type " << value_type
3378 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003379 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003380 return;
3381 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003383 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003385 << " to be compatible with type '" << insn_type
3386 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003387 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 return;
3389 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003390 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 }
3393}
3394
3395bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3396 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3397 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3398 return false;
3399 }
3400 return true;
3401}
3402
3403void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3404 Class* res_class, bool is_range) {
3405 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3406 /*
3407 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3408 * list and fail. It's legal, if silly, for arg_count to be zero.
3409 */
3410 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3411 uint32_t arg_count = dec_insn.vA_;
3412 for (size_t ui = 0; ui < arg_count; ui++) {
3413 uint32_t get_reg;
3414
3415 if (is_range)
3416 get_reg = dec_insn.vC_ + ui;
3417 else
3418 get_reg = dec_insn.arg_[ui];
3419
3420 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3421 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3422 << ") not valid";
3423 return;
3424 }
3425 }
3426}
3427
3428void DexVerifier::ReplaceFailingInstruction() {
3429 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3430 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3431 VerifyErrorRefType ref_type;
3432 switch (inst->Opcode()) {
3433 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003434 case Instruction::CHECK_CAST:
3435 case Instruction::INSTANCE_OF:
3436 case Instruction::NEW_INSTANCE:
3437 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003439 case Instruction::FILLED_NEW_ARRAY_RANGE:
3440 ref_type = VERIFY_ERROR_REF_CLASS;
3441 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003443 case Instruction::IGET_BOOLEAN:
3444 case Instruction::IGET_BYTE:
3445 case Instruction::IGET_CHAR:
3446 case Instruction::IGET_SHORT:
3447 case Instruction::IGET_WIDE:
3448 case Instruction::IGET_OBJECT:
3449 case Instruction::IPUT:
3450 case Instruction::IPUT_BOOLEAN:
3451 case Instruction::IPUT_BYTE:
3452 case Instruction::IPUT_CHAR:
3453 case Instruction::IPUT_SHORT:
3454 case Instruction::IPUT_WIDE:
3455 case Instruction::IPUT_OBJECT:
3456 case Instruction::SGET:
3457 case Instruction::SGET_BOOLEAN:
3458 case Instruction::SGET_BYTE:
3459 case Instruction::SGET_CHAR:
3460 case Instruction::SGET_SHORT:
3461 case Instruction::SGET_WIDE:
3462 case Instruction::SGET_OBJECT:
3463 case Instruction::SPUT:
3464 case Instruction::SPUT_BOOLEAN:
3465 case Instruction::SPUT_BYTE:
3466 case Instruction::SPUT_CHAR:
3467 case Instruction::SPUT_SHORT:
3468 case Instruction::SPUT_WIDE:
3469 case Instruction::SPUT_OBJECT:
3470 ref_type = VERIFY_ERROR_REF_FIELD;
3471 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003473 case Instruction::INVOKE_VIRTUAL_RANGE:
3474 case Instruction::INVOKE_SUPER:
3475 case Instruction::INVOKE_SUPER_RANGE:
3476 case Instruction::INVOKE_DIRECT:
3477 case Instruction::INVOKE_DIRECT_RANGE:
3478 case Instruction::INVOKE_STATIC:
3479 case Instruction::INVOKE_STATIC_RANGE:
3480 case Instruction::INVOKE_INTERFACE:
3481 case Instruction::INVOKE_INTERFACE_RANGE:
3482 ref_type = VERIFY_ERROR_REF_METHOD;
3483 break;
jeffhaobdb76512011-09-07 11:43:16 -07003484 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003485 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003486 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003487 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3489 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3490 // instruction, so assert it.
3491 size_t width = inst->SizeInCodeUnits();
3492 CHECK_GT(width, 1u);
3493 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3494 // NOPs
3495 for (size_t i = 2; i < width; i++) {
3496 insns[work_insn_idx_ + i] = Instruction::NOP;
3497 }
3498 // Encode the opcode, with the failure code in the high byte
3499 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3500 (failure_ << 8) | // AA - component
3501 (ref_type << (8 + kVerifyErrorRefTypeShift));
3502 insns[work_insn_idx_] = new_instruction;
3503 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3504 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003505 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3506 << fail_messages_.str();
3507 if (gDebugVerify) {
3508 std::cout << std::endl << info_messages_.str();
3509 Dump(std::cout);
3510 }
jeffhaobdb76512011-09-07 11:43:16 -07003511}
jeffhaoba5ebb92011-08-25 17:24:37 -07003512
Ian Rogersd81871c2011-10-03 13:57:23 -07003513bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3514 const bool merge_debug = true;
3515 bool changed = true;
3516 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3517 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003518 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003519 * We haven't processed this instruction before, and we haven't touched the registers here, so
3520 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3521 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003522 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003523 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003524 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3526 copy->CopyFromLine(target_line);
3527 changed = target_line->MergeRegisters(merge_line);
3528 if (failure_ != VERIFY_ERROR_NONE) {
3529 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003530 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003531 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3533 << *copy.get() << " MERGE" << std::endl
3534 << *merge_line << " ==" << std::endl
3535 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003536 }
3537 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 if (changed) {
3539 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003540 }
3541 return true;
3542}
3543
Ian Rogersd81871c2011-10-03 13:57:23 -07003544void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3545 size_t* log2_max_gc_pc) {
3546 size_t local_gc_points = 0;
3547 size_t max_insn = 0;
3548 size_t max_ref_reg = -1;
3549 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3550 if (insn_flags_[i].IsGcPoint()) {
3551 local_gc_points++;
3552 max_insn = i;
3553 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003554 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003555 }
3556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 *gc_points = local_gc_points;
3558 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3559 size_t i = 0;
3560 while ((1U << i) < max_insn) {
3561 i++;
3562 }
3563 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003564}
3565
Ian Rogersd81871c2011-10-03 13:57:23 -07003566ByteArray* DexVerifier::GenerateGcMap() {
3567 size_t num_entries, ref_bitmap_bits, pc_bits;
3568 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3569 // There's a single byte to encode the size of each bitmap
3570 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3571 // TODO: either a better GC map format or per method failures
3572 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3573 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003574 return NULL;
3575 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3577 // There are 2 bytes to encode the number of entries
3578 if (num_entries >= 65536) {
3579 // TODO: either a better GC map format or per method failures
3580 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3581 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003582 return NULL;
3583 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003584 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003585 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003587 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 pc_bytes = 1;
3589 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003590 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003592 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003593 // TODO: either a better GC map format or per method failures
3594 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3595 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3596 return NULL;
3597 }
3598 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3599 ByteArray* table = ByteArray::Alloc(table_size);
3600 if (table == NULL) {
3601 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3602 return NULL;
3603 }
3604 // Write table header
3605 table->Set(0, format);
3606 table->Set(1, ref_bitmap_bytes);
3607 table->Set(2, num_entries & 0xFF);
3608 table->Set(3, (num_entries >> 8) & 0xFF);
3609 // Write table data
3610 size_t offset = 4;
3611 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3612 if (insn_flags_[i].IsGcPoint()) {
3613 table->Set(offset, i & 0xFF);
3614 offset++;
3615 if (pc_bytes == 2) {
3616 table->Set(offset, (i >> 8) & 0xFF);
3617 offset++;
3618 }
3619 RegisterLine* line = reg_table_.GetLine(i);
3620 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3621 offset += ref_bitmap_bytes;
3622 }
3623 }
3624 DCHECK(offset == table_size);
3625 return table;
3626}
jeffhaoa0a764a2011-09-16 10:43:38 -07003627
Ian Rogersd81871c2011-10-03 13:57:23 -07003628void DexVerifier::VerifyGcMap() {
3629 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3630 // that the table data is well formed and all references are marked (or not) in the bitmap
3631 PcToReferenceMap map(method_);
3632 size_t map_index = 0;
3633 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3634 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3635 if (insn_flags_[i].IsGcPoint()) {
3636 CHECK_LT(map_index, map.NumEntries());
3637 CHECK_EQ(map.GetPC(map_index), i);
3638 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3639 map_index++;
3640 RegisterLine* line = reg_table_.GetLine(i);
3641 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003642 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 CHECK_LT(j / 8, map.RegWidth());
3644 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3645 } else if ((j / 8) < map.RegWidth()) {
3646 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3647 } else {
3648 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3649 }
3650 }
3651 } else {
3652 CHECK(reg_bitmap == NULL);
3653 }
3654 }
3655}
jeffhaoa0a764a2011-09-16 10:43:38 -07003656
Ian Rogersd81871c2011-10-03 13:57:23 -07003657Class* DexVerifier::JavaLangThrowable() {
3658 if (java_lang_throwable_ == NULL) {
3659 java_lang_throwable_ =
3660 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3661 DCHECK(java_lang_throwable_ != NULL);
3662 }
3663 return java_lang_throwable_;
3664}
3665
3666const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3667 size_t num_entries = NumEntries();
3668 // Do linear or binary search?
3669 static const size_t kSearchThreshold = 8;
3670 if (num_entries < kSearchThreshold) {
3671 for (size_t i = 0; i < num_entries; i++) {
3672 if (GetPC(i) == dex_pc) {
3673 return GetBitMap(i);
3674 }
3675 }
3676 } else {
3677 int lo = 0;
3678 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003679 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 int mid = (hi + lo) / 2;
3681 int mid_pc = GetPC(mid);
3682 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003683 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003685 hi = mid - 1;
3686 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003688 }
3689 }
3690 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003691 if (error_if_not_present) {
3692 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3693 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003694 return NULL;
3695}
3696
Ian Rogersd81871c2011-10-03 13:57:23 -07003697} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003698} // namespace art