blob: d521e9145d39477d152a6843997a039b18d3b5cc [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
Shih-wei Liao371814f2011-10-27 16:52:10 -0700889void DexVerifier::VerifyMethodAndDump(Method* method) {
890 DexVerifier verifier(method);
891 verifier.Verify();
892
893 LogMessage log(__FILE__, __LINE__, INFO, -1);
894 log.stream() << "Dump of method " << PrettyMethod(method) << " "
895 << verifier.fail_messages_.str();
896 log.stream() << std::endl << verifier.info_messages_.str();
897
898 verifier.Dump(log.stream());
899}
900
Ian Rogersd81871c2011-10-03 13:57:23 -0700901DexVerifier::DexVerifier(Method* method) : java_lang_throwable_(NULL), work_insn_idx_(-1),
902 method_(method), failure_(VERIFY_ERROR_NONE),
903 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700904 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
905 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 dex_file_ = &class_linker->FindDexFile(dex_cache);
907 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700908}
909
Ian Rogersd81871c2011-10-03 13:57:23 -0700910bool DexVerifier::Verify() {
911 // If there aren't any instructions, make sure that's expected, then exit successfully.
912 if (code_item_ == NULL) {
913 if (!method_->IsNative() && !method_->IsAbstract()) {
914 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700915 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 } else {
917 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700918 }
jeffhaobdb76512011-09-07 11:43:16 -0700919 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
921 if (code_item_->ins_size_ > code_item_->registers_size_) {
922 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
923 << " regs=" << code_item_->registers_size_;
924 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700925 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 // Allocate and initialize an array to hold instruction data.
927 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
928 // Run through the instructions and see if the width checks out.
929 bool result = ComputeWidthsAndCountOps();
930 // Flag instructions guarded by a "try" block and check exception handlers.
931 result = result && ScanTryCatchBlocks();
932 // Perform static instruction verification.
933 result = result && VerifyInstructions();
934 // Perform code flow analysis.
935 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -0700936 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700937}
938
Ian Rogersd81871c2011-10-03 13:57:23 -0700939bool DexVerifier::ComputeWidthsAndCountOps() {
940 const uint16_t* insns = code_item_->insns_;
941 size_t insns_size = code_item_->insns_size_in_code_units_;
942 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700943 size_t new_instance_count = 0;
944 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700948 Instruction::Code opcode = inst->Opcode();
949 if (opcode == Instruction::NEW_INSTANCE) {
950 new_instance_count++;
951 } else if (opcode == Instruction::MONITOR_ENTER) {
952 monitor_enter_count++;
953 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 size_t inst_size = inst->SizeInCodeUnits();
955 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
956 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700957 inst = inst->Next();
958 }
959
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 if (dex_pc != insns_size) {
961 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
962 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700963 return false;
964 }
965
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 new_instance_count_ = new_instance_count;
967 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700968 return true;
969}
970
Ian Rogersd81871c2011-10-03 13:57:23 -0700971bool DexVerifier::ScanTryCatchBlocks() {
972 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700973 if (tries_size == 0) {
974 return true;
975 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 uint32_t insns_size = code_item_->insns_size_in_code_units_;
977 const DexFile::TryItem* tries = DexFile::dexGetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700978
979 for (uint32_t idx = 0; idx < tries_size; idx++) {
980 const DexFile::TryItem* try_item = &tries[idx];
981 uint32_t start = try_item->start_addr_;
982 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700983 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
985 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700986 return false;
987 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 if (!insn_flags_[start].IsOpcode()) {
989 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700990 return false;
991 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 for (uint32_t dex_pc = start; dex_pc < end;
993 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
994 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700995 }
996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700999 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
1000 for (uint32_t idx = 0; idx < handlers_size; idx++) {
1001 DexFile::CatchHandlerIterator iterator(handlers_ptr);
jeffhaobdb76512011-09-07 11:43:16 -07001002 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 uint32_t dex_pc= iterator.Get().address_;
1004 if (!insn_flags_[dex_pc].IsOpcode()) {
1005 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001006 return false;
1007 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 insn_flags_[dex_pc].SetBranchTarget();
jeffhaobdb76512011-09-07 11:43:16 -07001009 }
jeffhaobdb76512011-09-07 11:43:16 -07001010 handlers_ptr = iterator.GetData();
1011 }
jeffhaobdb76512011-09-07 11:43:16 -07001012 return true;
1013}
1014
Ian Rogersd81871c2011-10-03 13:57:23 -07001015bool DexVerifier::VerifyInstructions() {
1016 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001017
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 /* Flag the start of the method as a branch target. */
1019 insn_flags_[0].SetBranchTarget();
1020
1021 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1022 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1023 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001024 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1025 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 return false;
1027 }
1028 /* Flag instructions that are garbage collection points */
1029 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1030 insn_flags_[dex_pc].SetGcPoint();
1031 }
1032 dex_pc += inst->SizeInCodeUnits();
1033 inst = inst->Next();
1034 }
1035 return true;
1036}
1037
1038bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1039 Instruction::DecodedInstruction dec_insn(inst);
1040 bool result = true;
1041 switch (inst->GetVerifyTypeArgumentA()) {
1042 case Instruction::kVerifyRegA:
1043 result = result && CheckRegisterIndex(dec_insn.vA_);
1044 break;
1045 case Instruction::kVerifyRegAWide:
1046 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1047 break;
1048 }
1049 switch (inst->GetVerifyTypeArgumentB()) {
1050 case Instruction::kVerifyRegB:
1051 result = result && CheckRegisterIndex(dec_insn.vB_);
1052 break;
1053 case Instruction::kVerifyRegBField:
1054 result = result && CheckFieldIndex(dec_insn.vB_);
1055 break;
1056 case Instruction::kVerifyRegBMethod:
1057 result = result && CheckMethodIndex(dec_insn.vB_);
1058 break;
1059 case Instruction::kVerifyRegBNewInstance:
1060 result = result && CheckNewInstance(dec_insn.vB_);
1061 break;
1062 case Instruction::kVerifyRegBString:
1063 result = result && CheckStringIndex(dec_insn.vB_);
1064 break;
1065 case Instruction::kVerifyRegBType:
1066 result = result && CheckTypeIndex(dec_insn.vB_);
1067 break;
1068 case Instruction::kVerifyRegBWide:
1069 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1070 break;
1071 }
1072 switch (inst->GetVerifyTypeArgumentC()) {
1073 case Instruction::kVerifyRegC:
1074 result = result && CheckRegisterIndex(dec_insn.vC_);
1075 break;
1076 case Instruction::kVerifyRegCField:
1077 result = result && CheckFieldIndex(dec_insn.vC_);
1078 break;
1079 case Instruction::kVerifyRegCNewArray:
1080 result = result && CheckNewArray(dec_insn.vC_);
1081 break;
1082 case Instruction::kVerifyRegCType:
1083 result = result && CheckTypeIndex(dec_insn.vC_);
1084 break;
1085 case Instruction::kVerifyRegCWide:
1086 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1087 break;
1088 }
1089 switch (inst->GetVerifyExtraFlags()) {
1090 case Instruction::kVerifyArrayData:
1091 result = result && CheckArrayData(code_offset);
1092 break;
1093 case Instruction::kVerifyBranchTarget:
1094 result = result && CheckBranchTarget(code_offset);
1095 break;
1096 case Instruction::kVerifySwitchTargets:
1097 result = result && CheckSwitchTargets(code_offset);
1098 break;
1099 case Instruction::kVerifyVarArg:
1100 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1101 break;
1102 case Instruction::kVerifyVarArgRange:
1103 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1104 break;
1105 case Instruction::kVerifyError:
1106 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1107 result = false;
1108 break;
1109 }
1110 return result;
1111}
1112
1113bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1114 if (idx >= code_item_->registers_size_) {
1115 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1116 << code_item_->registers_size_ << ")";
1117 return false;
1118 }
1119 return true;
1120}
1121
1122bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1123 if (idx + 1 >= code_item_->registers_size_) {
1124 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1125 << "+1 >= " << code_item_->registers_size_ << ")";
1126 return false;
1127 }
1128 return true;
1129}
1130
1131bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1132 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1133 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1134 << dex_file_->GetHeader().field_ids_size_ << ")";
1135 return false;
1136 }
1137 return true;
1138}
1139
1140bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1141 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1142 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1143 << dex_file_->GetHeader().method_ids_size_ << ")";
1144 return false;
1145 }
1146 return true;
1147}
1148
1149bool DexVerifier::CheckNewInstance(uint32_t idx) {
1150 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1151 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1152 << dex_file_->GetHeader().type_ids_size_ << ")";
1153 return false;
1154 }
1155 // We don't need the actual class, just a pointer to the class name.
1156 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1157 if (descriptor[0] != 'L') {
1158 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1159 return false;
1160 }
1161 return true;
1162}
1163
1164bool DexVerifier::CheckStringIndex(uint32_t idx) {
1165 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1166 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1167 << dex_file_->GetHeader().string_ids_size_ << ")";
1168 return false;
1169 }
1170 return true;
1171}
1172
1173bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1174 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1175 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1176 << dex_file_->GetHeader().type_ids_size_ << ")";
1177 return false;
1178 }
1179 return true;
1180}
1181
1182bool DexVerifier::CheckNewArray(uint32_t idx) {
1183 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1184 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1185 << dex_file_->GetHeader().type_ids_size_ << ")";
1186 return false;
1187 }
1188 int bracket_count = 0;
1189 const char* descriptor = dex_file_->dexStringByTypeIdx(idx);
1190 const char* cp = descriptor;
1191 while (*cp++ == '[') {
1192 bracket_count++;
1193 }
1194 if (bracket_count == 0) {
1195 /* The given class must be an array type. */
1196 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1197 return false;
1198 } else if (bracket_count > 255) {
1199 /* It is illegal to create an array of more than 255 dimensions. */
1200 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1201 return false;
1202 }
1203 return true;
1204}
1205
1206bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1207 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1208 const uint16_t* insns = code_item_->insns_ + cur_offset;
1209 const uint16_t* array_data;
1210 int32_t array_data_offset;
1211
1212 DCHECK_LT(cur_offset, insn_count);
1213 /* make sure the start of the array data table is in range */
1214 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1215 if ((int32_t) cur_offset + array_data_offset < 0 ||
1216 cur_offset + array_data_offset + 2 >= insn_count) {
1217 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1218 << ", data offset " << array_data_offset << ", count " << insn_count;
1219 return false;
1220 }
1221 /* offset to array data table is a relative branch-style offset */
1222 array_data = insns + array_data_offset;
1223 /* make sure the table is 32-bit aligned */
1224 if ((((uint32_t) array_data) & 0x03) != 0) {
1225 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1226 << ", data offset " << array_data_offset;
1227 return false;
1228 }
1229 uint32_t value_width = array_data[1];
1230 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1231 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1232 /* make sure the end of the switch is in range */
1233 if (cur_offset + array_data_offset + table_size > insn_count) {
1234 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1235 << ", data offset " << array_data_offset << ", end "
1236 << cur_offset + array_data_offset + table_size
1237 << ", count " << insn_count;
1238 return false;
1239 }
1240 return true;
1241}
1242
1243bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1244 int32_t offset;
1245 bool isConditional, selfOkay;
1246 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1247 return false;
1248 }
1249 if (!selfOkay && offset == 0) {
1250 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1251 return false;
1252 }
1253 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1254 // identical "wrap-around" behavior, but it's unwise to depend on that.
1255 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1256 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1257 return false;
1258 }
1259 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1260 int32_t abs_offset = cur_offset + offset;
1261 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1262 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1263 << (void*) abs_offset << ") at " << (void*) cur_offset;
1264 return false;
1265 }
1266 insn_flags_[abs_offset].SetBranchTarget();
1267 return true;
1268}
1269
1270bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1271 bool* selfOkay) {
1272 const uint16_t* insns = code_item_->insns_ + cur_offset;
1273 *pConditional = false;
1274 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001275 switch (*insns & 0xff) {
1276 case Instruction::GOTO:
1277 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001278 break;
1279 case Instruction::GOTO_32:
1280 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001281 *selfOkay = true;
1282 break;
1283 case Instruction::GOTO_16:
1284 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001285 break;
1286 case Instruction::IF_EQ:
1287 case Instruction::IF_NE:
1288 case Instruction::IF_LT:
1289 case Instruction::IF_GE:
1290 case Instruction::IF_GT:
1291 case Instruction::IF_LE:
1292 case Instruction::IF_EQZ:
1293 case Instruction::IF_NEZ:
1294 case Instruction::IF_LTZ:
1295 case Instruction::IF_GEZ:
1296 case Instruction::IF_GTZ:
1297 case Instruction::IF_LEZ:
1298 *pOffset = (int16_t) insns[1];
1299 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001300 break;
1301 default:
1302 return false;
1303 break;
1304 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 return true;
1306}
1307
Ian Rogersd81871c2011-10-03 13:57:23 -07001308bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1309 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001310 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1314 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1315 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1316 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 return false;
1318 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001319 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001321 /* make sure the table is 32-bit aligned */
1322 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1324 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 return false;
1326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 uint32_t switch_count = switch_insns[1];
1328 int32_t keys_offset, targets_offset;
1329 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1331 /* 0=sig, 1=count, 2/3=firstKey */
1332 targets_offset = 4;
1333 keys_offset = -1;
1334 expected_signature = Instruction::kPackedSwitchSignature;
1335 } else {
1336 /* 0=sig, 1=count, 2..count*2 = keys */
1337 keys_offset = 2;
1338 targets_offset = 2 + 2 * switch_count;
1339 expected_signature = Instruction::kSparseSwitchSignature;
1340 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1344 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 return false;
1346 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001347 /* make sure the end of the switch is in range */
1348 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1350 << switch_offset << ", end "
1351 << (cur_offset + switch_offset + table_size)
1352 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 return false;
1354 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 /* for a sparse switch, verify the keys are in ascending order */
1356 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1358 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1360 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1361 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1363 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 return false;
1365 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 last_key = key;
1367 }
1368 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001369 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 for (uint32_t targ = 0; targ < switch_count; targ++) {
1371 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1372 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1373 int32_t abs_offset = cur_offset + offset;
1374 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1375 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1376 << (void*) abs_offset << ") at "
1377 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001378 return false;
1379 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 insn_flags_[abs_offset].SetBranchTarget();
1381 }
1382 return true;
1383}
1384
1385bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1386 if (vA > 5) {
1387 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1388 return false;
1389 }
1390 uint16_t registers_size = code_item_->registers_size_;
1391 for (uint32_t idx = 0; idx < vA; idx++) {
1392 if (arg[idx] > registers_size) {
1393 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1394 << ") in non-range invoke (> " << registers_size << ")";
1395 return false;
1396 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 }
1398
1399 return true;
1400}
1401
Ian Rogersd81871c2011-10-03 13:57:23 -07001402bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1403 uint16_t registers_size = code_item_->registers_size_;
1404 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1405 // integer overflow when adding them here.
1406 if (vA + vC > registers_size) {
1407 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1408 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 return false;
1410 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001411 return true;
1412}
1413
Ian Rogersd81871c2011-10-03 13:57:23 -07001414bool DexVerifier::VerifyCodeFlow() {
1415 uint16_t registers_size = code_item_->registers_size_;
1416 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001417
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 if (registers_size * insns_size > 4*1024*1024) {
1419 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1420 << " insns_size=" << insns_size << ")";
1421 }
1422 /* Create and initialize table holding register status */
1423 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1424 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001425
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 work_line_.reset(new RegisterLine(registers_size, this));
1427 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001428
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 /* Initialize register types of method arguments. */
1430 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001431 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1432 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 return false;
1434 }
1435 /* Perform code flow verification. */
1436 if (!CodeFlowVerifyMethod()) {
1437 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001438 }
1439
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 /* Generate a register map and add it to the method. */
1441 ByteArray* map = GenerateGcMap();
1442 if (map == NULL) {
1443 return false; // Not a real failure, but a failure to encode
1444 }
1445 method_->SetGcMap(map);
1446#ifndef NDEBUG
1447 VerifyGcMap();
1448#endif
jeffhaobdb76512011-09-07 11:43:16 -07001449 return true;
1450}
1451
Ian Rogersd81871c2011-10-03 13:57:23 -07001452void DexVerifier::Dump(std::ostream& os) {
1453 if (method_->IsNative()) {
1454 os << "Native method" << std::endl;
1455 return;
jeffhaobdb76512011-09-07 11:43:16 -07001456 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 DCHECK(code_item_ != NULL);
1458 const Instruction* inst = Instruction::At(code_item_->insns_);
1459 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1460 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001461 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1462 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1464 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001465 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001466 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 inst = inst->Next();
1468 }
jeffhaobdb76512011-09-07 11:43:16 -07001469}
1470
Ian Rogersd81871c2011-10-03 13:57:23 -07001471static bool IsPrimitiveDescriptor(char descriptor) {
1472 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001473 case 'I':
1474 case 'C':
1475 case 'S':
1476 case 'B':
1477 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001478 case 'F':
1479 case 'D':
1480 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001481 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001482 default:
1483 return false;
1484 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001485}
1486
Ian Rogersd81871c2011-10-03 13:57:23 -07001487bool DexVerifier::SetTypesFromSignature() {
1488 RegisterLine* reg_line = reg_table_.GetLine(0);
1489 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1490 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001491
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1493 //Include the "this" pointer.
1494 size_t cur_arg = 0;
1495 if (!method_->IsStatic()) {
1496 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1497 // argument as uninitialized. This restricts field access until the superclass constructor is
1498 // called.
1499 Class* declaring_class = method_->GetDeclaringClass();
1500 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1501 reg_line->SetRegisterType(arg_start + cur_arg,
1502 reg_types_.UninitializedThisArgument(declaring_class));
1503 } else {
1504 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001505 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001507 }
1508
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_->GetProtoIdx());
1510 DexFile::ParameterIterator iterator(*dex_file_, proto_id);
1511
1512 for (; iterator.HasNext(); iterator.Next()) {
1513 const char* descriptor = iterator.GetDescriptor();
1514 if (descriptor == NULL) {
1515 LOG(FATAL) << "Null descriptor";
1516 }
1517 if (cur_arg >= expected_args) {
1518 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1519 << " args, found more (" << descriptor << ")";
1520 return false;
1521 }
1522 switch (descriptor[0]) {
1523 case 'L':
1524 case '[':
1525 // We assume that reference arguments are initialized. The only way it could be otherwise
1526 // (assuming the caller was verified) is if the current method is <init>, but in that case
1527 // it's effectively considered initialized the instant we reach here (in the sense that we
1528 // can return without doing anything or call virtual methods).
1529 {
1530 const RegType& reg_type =
1531 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001532 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 }
1534 break;
1535 case 'Z':
1536 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1537 break;
1538 case 'C':
1539 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1540 break;
1541 case 'B':
1542 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1543 break;
1544 case 'I':
1545 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1546 break;
1547 case 'S':
1548 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1549 break;
1550 case 'F':
1551 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1552 break;
1553 case 'J':
1554 case 'D': {
1555 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1556 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1557 cur_arg++;
1558 break;
1559 }
1560 default:
1561 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1562 return false;
1563 }
1564 cur_arg++;
1565 }
1566 if (cur_arg != expected_args) {
1567 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1568 return false;
1569 }
1570 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1571 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1572 // format. Only major difference from the method argument format is that 'V' is supported.
1573 bool result;
1574 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1575 result = descriptor[1] == '\0';
1576 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1577 size_t i = 0;
1578 do {
1579 i++;
1580 } while (descriptor[i] == '['); // process leading [
1581 if (descriptor[i] == 'L') { // object array
1582 do {
1583 i++; // find closing ;
1584 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1585 result = descriptor[i] == ';';
1586 } else { // primitive array
1587 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1588 }
1589 } else if (descriptor[0] == 'L') {
1590 // could be more thorough here, but shouldn't be required
1591 size_t i = 0;
1592 do {
1593 i++;
1594 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1595 result = descriptor[i] == ';';
1596 } else {
1597 result = false;
1598 }
1599 if (!result) {
1600 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1601 << descriptor << "'";
1602 }
1603 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001604}
1605
Ian Rogersd81871c2011-10-03 13:57:23 -07001606bool DexVerifier::CodeFlowVerifyMethod() {
1607 const uint16_t* insns = code_item_->insns_;
1608 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001609
jeffhaobdb76512011-09-07 11:43:16 -07001610 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 insn_flags_[0].SetChanged();
1612 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001613
jeffhaobdb76512011-09-07 11:43:16 -07001614 /* Continue until no instructions are marked "changed". */
1615 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1617 uint32_t insn_idx = start_guess;
1618 for (; insn_idx < insns_size; insn_idx++) {
1619 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001620 break;
1621 }
jeffhaobdb76512011-09-07 11:43:16 -07001622 if (insn_idx == insns_size) {
1623 if (start_guess != 0) {
1624 /* try again, starting from the top */
1625 start_guess = 0;
1626 continue;
1627 } else {
1628 /* all flags are clear */
1629 break;
1630 }
1631 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 // We carry the working set of registers from instruction to instruction. If this address can
1633 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1634 // "changed" flags, we need to load the set of registers from the table.
1635 // Because we always prefer to continue on to the next instruction, we should never have a
1636 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1637 // target.
1638 work_insn_idx_ = insn_idx;
1639 if (insn_flags_[insn_idx].IsBranchTarget()) {
1640 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001641 } else {
1642#ifndef NDEBUG
1643 /*
1644 * Sanity check: retrieve the stored register line (assuming
1645 * a full table) and make sure it actually matches.
1646 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1648 if (register_line != NULL) {
1649 if (work_line_->CompareLine(register_line) != 0) {
1650 Dump(std::cout);
1651 std::cout << info_messages_.str();
1652 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1653 << "@" << (void*)work_insn_idx_ << std::endl
1654 << " work_line=" << *work_line_ << std::endl
1655 << " expected=" << *register_line;
1656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 }
1658#endif
1659 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 if (!CodeFlowVerifyInstruction(&start_guess)) {
1661 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001662 return false;
1663 }
jeffhaobdb76512011-09-07 11:43:16 -07001664 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 insn_flags_[insn_idx].SetVisited();
1666 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001667 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001668
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001670 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001672 * (besides the wasted space), but it indicates a flaw somewhere
1673 * down the line, possibly in the verifier.
1674 *
1675 * If we've substituted "always throw" instructions into the stream,
1676 * we are almost certainly going to have some dead code.
1677 */
1678 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 uint32_t insn_idx = 0;
1680 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001681 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001682 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001683 * may or may not be preceded by a padding NOP (for alignment).
1684 */
1685 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1686 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1687 insns[insn_idx] == Instruction::kArrayDataSignature ||
1688 (insns[insn_idx] == Instruction::NOP &&
1689 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1690 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1691 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001693 }
1694
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001696 if (dead_start < 0)
1697 dead_start = insn_idx;
1698 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001700 dead_start = -1;
1701 }
1702 }
1703 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001705 }
1706 }
jeffhaobdb76512011-09-07 11:43:16 -07001707 return true;
1708}
1709
Ian Rogersd81871c2011-10-03 13:57:23 -07001710bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001711#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001713 gDvm.verifierStats.instrsReexamined++;
1714 } else {
1715 gDvm.verifierStats.instrsExamined++;
1716 }
1717#endif
1718
1719 /*
1720 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001721 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001722 * control to another statement:
1723 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001724 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001725 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001726 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001727 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001728 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001729 * throw an exception that is handled by an encompassing "try"
1730 * block.
1731 *
1732 * We can also return, in which case there is no successor instruction
1733 * from this point.
1734 *
1735 * The behavior can be determined from the OpcodeFlags.
1736 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1738 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001739 Instruction::DecodedInstruction dec_insn(inst);
1740 int opcode_flag = inst->Flag();
1741
jeffhaobdb76512011-09-07 11:43:16 -07001742 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001743 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001744 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001746 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1747 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
jeffhaobdb76512011-09-07 11:43:16 -07001749
1750 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001751 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001752 * can throw an exception, we will copy/merge this into the "catch"
1753 * address rather than work_line, because we don't want the result
1754 * from the "successful" code path (e.g. a check-cast that "improves"
1755 * a type) to be visible to the exception handler.
1756 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1758 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001759 } else {
1760#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001762#endif
1763 }
1764
1765 switch (dec_insn.opcode_) {
1766 case Instruction::NOP:
1767 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001768 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001769 * a signature that looks like a NOP; if we see one of these in
1770 * the course of executing code then we have a problem.
1771 */
1772 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001774 }
1775 break;
1776
1777 case Instruction::MOVE:
1778 case Instruction::MOVE_FROM16:
1779 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
1782 case Instruction::MOVE_WIDE:
1783 case Instruction::MOVE_WIDE_FROM16:
1784 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001786 break;
1787 case Instruction::MOVE_OBJECT:
1788 case Instruction::MOVE_OBJECT_FROM16:
1789 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
1792
1793 /*
1794 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001795 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001796 * might want to hold the result in an actual CPU register, so the
1797 * Dalvik spec requires that these only appear immediately after an
1798 * invoke or filled-new-array.
1799 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001800 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001801 * redundant with the reset done below, but it can make the debug info
1802 * easier to read in some cases.)
1803 */
1804 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
1807 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001812 break;
1813
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001815 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 * This statement can only appear as the first instruction in an exception handler (though not
1817 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001818 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001819 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 Class* res_class = GetCaughtExceptionType();
jeffhaobdb76512011-09-07 11:43:16 -07001821 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 DCHECK(failure_ != VERIFY_ERROR_NONE);
jeffhaobdb76512011-09-07 11:43:16 -07001823 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001825 }
1826 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 }
jeffhaobdb76512011-09-07 11:43:16 -07001828 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1830 if (!GetMethodReturnType().IsUnknown()) {
1831 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1832 }
jeffhaobdb76512011-09-07 11:43:16 -07001833 }
1834 break;
1835 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001837 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 const RegType& return_type = GetMethodReturnType();
1839 if (!return_type.IsCategory1Types()) {
1840 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1841 } else {
1842 // Compilers may generate synthetic functions that write byte values into boolean fields.
1843 // Also, it may use integer values for boolean, byte, short, and character return types.
1844 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1845 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1846 ((return_type.IsBoolean() || return_type.IsByte() ||
1847 return_type.IsShort() || return_type.IsChar()) &&
1848 src_type.IsInteger()));
1849 /* check the register contents */
1850 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1851 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001852 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
1855 }
1856 break;
1857 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001859 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 const RegType& return_type = GetMethodReturnType();
1861 if (!return_type.IsCategory2Types()) {
1862 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1863 } else {
1864 /* check the register contents */
1865 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1866 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001867 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 }
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
1870 }
1871 break;
1872 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1874 const RegType& return_type = GetMethodReturnType();
1875 if (!return_type.IsReferenceTypes()) {
1876 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1877 } else {
1878 /* return_type is the *expected* return type, not register value */
1879 DCHECK(!return_type.IsZero());
1880 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001881 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1882 // Disallow returning uninitialized values and verify that the reference in vAA is an
1883 // instance of the "return_type"
1884 if (reg_type.IsUninitializedTypes()) {
1885 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1886 } else if (!return_type.IsAssignableFrom(reg_type)) {
1887 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1888 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890 }
1891 }
1892 break;
1893
1894 case Instruction::CONST_4:
1895 case Instruction::CONST_16:
1896 case Instruction::CONST:
1897 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900 case Instruction::CONST_HIGH16:
1901 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 work_line_->SetRegisterType(dec_insn.vA_,
1903 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905 case Instruction::CONST_WIDE_16:
1906 case Instruction::CONST_WIDE_32:
1907 case Instruction::CONST_WIDE:
1908 case Instruction::CONST_WIDE_HIGH16:
1909 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::CONST_STRING:
1913 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001915 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 case Instruction::CONST_CLASS: {
jeffhaobdb76512011-09-07 11:43:16 -07001917 /* make sure we can resolve the class; access check is important */
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001919 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001921 fail_messages_ << "unable to resolve const-class " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 << " (" << bad_class_desc << ") in "
1923 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1924 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001925 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001927 }
1928 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 }
jeffhaobdb76512011-09-07 11:43:16 -07001930 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001932 break;
1933 case Instruction::MONITOR_EXIT:
1934 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001935 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001936 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001937 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001938 * to the need to handle asynchronous exceptions, a now-deprecated
1939 * feature that Dalvik doesn't support.)
1940 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001941 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001942 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001943 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001944 * structured locking checks are working, the former would have
1945 * failed on the -enter instruction, and the latter is impossible.
1946 *
1947 * This is fortunate, because issue 3221411 prevents us from
1948 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001949 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001950 * some catch blocks (which will show up as "dead" code when
1951 * we skip them here); if we can't, then the code path could be
1952 * "live" so we still need to check it.
1953 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 opcode_flag &= ~Instruction::kThrow;
1955 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001956 break;
1957
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 case Instruction::CHECK_CAST: {
jeffhaobdb76512011-09-07 11:43:16 -07001959 /*
1960 * If this instruction succeeds, we will promote register vA to
jeffhaod1f0fde2011-09-08 17:25:33 -07001961 * the type in vB. (This could be a demotion -- not expected, so
jeffhaobdb76512011-09-07 11:43:16 -07001962 * we don't try to address it.)
1963 *
1964 * If it fails, an exception is thrown, which we deal with later
1965 * by ignoring the update to dec_insn.vA_ when branching to a handler.
1966 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001968 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001970 fail_messages_ << "unable to resolve check-cast " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 << " (" << bad_class_desc << ") in "
1972 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1973 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07001974 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 const RegType& orig_type = work_line_->GetRegisterType(dec_insn.vA_);
1976 if (!orig_type.IsReferenceTypes()) {
1977 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
1978 } else {
1979 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 }
1982 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
1984 case Instruction::INSTANCE_OF: {
jeffhaobdb76512011-09-07 11:43:16 -07001985 /* make sure we're checking a reference type */
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 const RegType& tmp_type = work_line_->GetRegisterType(dec_insn.vB_);
1987 if (!tmp_type.IsReferenceTypes()) {
1988 Fail(VERIFY_ERROR_GENERIC) << "vB not a reference (" << tmp_type << ")";
jeffhao2a8a90e2011-09-26 14:25:31 -07001989 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 /* make sure we can resolve the class; access check is important */
1991 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
1992 if (res_class == NULL) {
1993 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07001994 fail_messages_ << "unable to resolve instance of " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 << " (" << bad_class_desc << ") in "
1996 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
1997 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
1998 } else {
1999 /* result is boolean */
2000 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002003 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 }
2005 case Instruction::ARRAY_LENGTH: {
2006 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vB_);
2007 if (failure_ == VERIFY_ERROR_NONE) {
2008 if (res_class != NULL && !res_class->IsArrayClass()) {
2009 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array";
2010 } else {
2011 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2012 }
2013 }
2014 break;
2015 }
2016 case Instruction::NEW_INSTANCE: {
2017 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002018 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002020 fail_messages_ << "unable to resolve new-instance " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 << " (" << bad_class_desc << ") in "
2022 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2023 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
2024 } else {
2025 /* can't create an instance of an interface or abstract class */
2026 if (res_class->IsPrimitive() || res_class->IsAbstract() || res_class->IsInterface()) {
2027 Fail(VERIFY_ERROR_INSTANTIATION)
2028 << "new-instance on primitive, interface or abstract class"
2029 << PrettyDescriptor(res_class->GetDescriptor());
2030 } else {
2031 const RegType& uninit_type = reg_types_.Uninitialized(res_class, work_insn_idx_);
2032 // Any registers holding previous allocations from this address that have not yet been
2033 // initialized must be marked invalid.
2034 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2035
2036 /* add the new uninitialized reference to the register state */
2037 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
2038 }
2039 }
2040 break;
2041 }
2042 case Instruction::NEW_ARRAY: {
2043 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vC_);
2044 if (res_class == NULL) {
2045 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vC_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002046 fail_messages_ << "unable to resolve new-array " << dec_insn.vC_
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 << " (" << bad_class_desc << ") in "
2048 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2049 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002050 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002052 } else {
2053 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 /* set register type to array class */
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002057 }
2058 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 }
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2062 Class* res_class = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07002063 if (res_class == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 const char* bad_class_desc = dex_file_->dexStringByTypeIdx(dec_insn.vB_);
Ian Rogersb5e95b92011-10-25 23:28:55 -07002065 fail_messages_ << "unable to resolve filled-array " << dec_insn.vB_
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 << " (" << bad_class_desc << ") in "
2067 << PrettyDescriptor(method_->GetDeclaringClass()->GetDescriptor());
2068 DCHECK(failure_ != VERIFY_ERROR_GENERIC);
jeffhao2a8a90e2011-09-26 14:25:31 -07002069 } else if (!res_class->IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002071 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002072 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002073 /* check the arguments to the instruction */
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 VerifyFilledNewArrayRegs(dec_insn, res_class, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002075 /* filled-array result goes into "result" register */
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 work_line_->SetResultRegisterType(reg_types_.FromClass(res_class));
jeffhaobdb76512011-09-07 11:43:16 -07002077 just_set_result = true;
2078 }
2079 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 }
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::CMPL_FLOAT:
2082 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2084 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2085 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::CMPL_DOUBLE:
2088 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2090 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2091 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2095 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2096 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 case Instruction::THROW: {
2099 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2100 if (failure_ == VERIFY_ERROR_NONE && res_class != NULL) {
2101 if (!JavaLangThrowable()->IsAssignableFrom(res_class)) {
2102 Fail(VERIFY_ERROR_GENERIC) << "thrown class "
2103 << PrettyDescriptor(res_class->GetDescriptor()) << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002104 }
2105 }
2106 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 }
jeffhaobdb76512011-09-07 11:43:16 -07002108 case Instruction::GOTO:
2109 case Instruction::GOTO_16:
2110 case Instruction::GOTO_32:
2111 /* no effect on or use of registers */
2112 break;
2113
2114 case Instruction::PACKED_SWITCH:
2115 case Instruction::SPARSE_SWITCH:
2116 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
2119
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 case Instruction::FILL_ARRAY_DATA: {
2121 /* Similar to the verification done for APUT */
2122 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2123 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002124 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 if (res_class != NULL) {
2126 Class* component_type = res_class->GetComponentType();
2127 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2128 component_type->IsPrimitiveVoid()) {
2129 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
2130 << PrettyDescriptor(res_class->GetDescriptor());
2131 } else {
2132 const RegType& value_type = reg_types_.FromClass(component_type);
2133 DCHECK(!value_type.IsUnknown());
2134 // Now verify if the element width in the table matches the element width declared in
2135 // the array
2136 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2137 if (array_data[0] != Instruction::kArrayDataSignature) {
2138 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2139 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002140 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 // Since we don't compress the data in Dex, expect to see equal width of data stored
2142 // in the table and expected from the array class.
2143 if (array_data[1] != elem_width) {
2144 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2145 << " vs " << elem_width << ")";
2146 }
2147 }
2148 }
jeffhaobdb76512011-09-07 11:43:16 -07002149 }
2150 }
2151 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 }
jeffhaobdb76512011-09-07 11:43:16 -07002153 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 case Instruction::IF_NE: {
2155 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2156 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2157 bool mismatch = false;
2158 if (reg_type1.IsZero()) { // zero then integral or reference expected
2159 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2160 } else if (reg_type1.IsReferenceTypes()) { // both references?
2161 mismatch = !reg_type2.IsReferenceTypes();
2162 } else { // both integral?
2163 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2164 }
2165 if (mismatch) {
2166 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2167 << ") must both be references or 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_LT:
2172 case Instruction::IF_GE:
2173 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 case Instruction::IF_LE: {
2175 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2176 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2177 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2178 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2179 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002180 }
2181 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 case Instruction::IF_NEZ: {
2185 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2186 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2187 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2188 }
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 case Instruction::IF_LTZ:
2192 case Instruction::IF_GEZ:
2193 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 case Instruction::IF_LEZ: {
2195 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2196 if (!reg_type.IsIntegralTypes()) {
2197 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2198 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 }
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2204 break;
jeffhaobdb76512011-09-07 11:43:16 -07002205 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2207 break;
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 VerifyAGet(dec_insn, reg_types_.Char(), true);
2210 break;
jeffhaobdb76512011-09-07 11:43:16 -07002211 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 case Instruction::AGET:
2215 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2216 break;
jeffhaobdb76512011-09-07 11:43:16 -07002217 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 VerifyAGet(dec_insn, reg_types_.Long(), true);
2219 break;
2220 case Instruction::AGET_OBJECT:
2221 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 case Instruction::APUT_BOOLEAN:
2225 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2226 break;
2227 case Instruction::APUT_BYTE:
2228 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2229 break;
2230 case Instruction::APUT_CHAR:
2231 VerifyAPut(dec_insn, reg_types_.Char(), true);
2232 break;
2233 case Instruction::APUT_SHORT:
2234 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
2236 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
2239 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
2245
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002247 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002250 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 break;
jeffhaobdb76512011-09-07 11:43:16 -07002252 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002253 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002256 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 break;
2258 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002259 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002262 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002265 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 break;
jeffhaobdb76512011-09-07 11:43:16 -07002267
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002269 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 break;
2271 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002272 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
2274 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002275 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
2277 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002278 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002281 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002284 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 break;
jeffhaobdb76512011-09-07 11:43:16 -07002286 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002287 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002300 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
2302 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002303 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002306 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002309 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
2311
2312 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
2315 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 break;
2321 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002322 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002325 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002328 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002331 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333
2334 case Instruction::INVOKE_VIRTUAL:
2335 case Instruction::INVOKE_VIRTUAL_RANGE:
2336 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 case Instruction::INVOKE_SUPER_RANGE: {
2338 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2339 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2340 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2341 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2342 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2343 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers9074b992011-10-26 17:41:55 -07002344 const RegType& return_type =
2345 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2346 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002348 just_set_result = true;
2349 }
2350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 }
jeffhaobdb76512011-09-07 11:43:16 -07002352 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 case Instruction::INVOKE_DIRECT_RANGE: {
2354 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2355 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2356 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002357 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 * Some additional checks when calling a constructor. We know from the invocation arg check
2359 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2360 * that to require that called_method->klass is the same as this->klass or this->super,
2361 * allowing the latter only if the "this" argument is the same as the "this" argument to
2362 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002363 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 if (called_method->IsConstructor()) {
2365 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2366 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368
2369 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 if (this_type.IsZero()) {
2371 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 Class* this_class = this_type.GetClass();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002375 DCHECK(this_class != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07002376
2377 /* must be in same class or in superclass */
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2379 if (this_class != method_->GetDeclaringClass()) {
2380 Fail(VERIFY_ERROR_GENERIC)
2381 << "invoke-direct <init> on super only allowed for 'this' in <init>";
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383 }
2384 } else if (called_method->GetDeclaringClass() != this_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 }
2388
2389 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002390 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2392 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 }
2395
2396 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002397 * Replace the uninitialized reference with an initialized one. We need to do this for all
2398 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002399 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 work_line_->MarkRefsAsInitialized(this_type);
2401 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002403 }
Ian Rogers9074b992011-10-26 17:41:55 -07002404 const RegType& return_type =
2405 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2406 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002408 just_set_result = true;
2409 }
2410 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 }
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 case Instruction::INVOKE_STATIC_RANGE: {
2414 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2415 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2416 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers9074b992011-10-26 17:41:55 -07002417 const RegType& return_type =
2418 reg_types_.FromDescriptor(called_method->GetDeclaringClass()->GetClassLoader(),
2419 called_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 work_line_->SetResultRegisterType(return_type);
2421 just_set_result = true;
2422 }
jeffhaobdb76512011-09-07 11:43:16 -07002423 }
2424 break;
2425 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 case Instruction::INVOKE_INTERFACE_RANGE: {
2427 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2428 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2429 if (failure_ == VERIFY_ERROR_NONE) {
2430 Class* called_interface = abs_method->GetDeclaringClass();
2431 if (!called_interface->IsInterface()) {
2432 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2433 << PrettyMethod(abs_method) << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
jeffhaobdb76512011-09-07 11:43:16 -07002435 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002436 /* Get the type of the "this" arg, which should either be a sub-interface of called
2437 * interface or Object (see comments in RegType::JoinClass).
jeffhaobdb76512011-09-07 11:43:16 -07002438 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2440 if (failure_ == VERIFY_ERROR_NONE) {
2441 if (this_type.IsZero()) {
2442 /* null pointer always passes (and always fails at runtime) */
2443 } else {
Ian Rogersb94a27b2011-10-26 00:33:41 -07002444 if (this_type.IsUninitializedTypes()) {
2445 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2446 << this_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 break;
2448 }
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002449 // In the past we have tried to assert that "called_interface" is assignable
2450 // from "this_type.GetClass()", however, as we do an imprecise Join
2451 // (RegType::JoinClass) we don't have full information on what interfaces are
2452 // implemented by "this_type". For example, two classes may implement the same
2453 // interfaces and have a common parent that doesn't implement the interface. The
2454 // join will set "this_type" to the parent class and a test that this implements
2455 // the interface will incorrectly fail.
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 }
jeffhaobdb76512011-09-07 11:43:16 -07002457 }
2458 }
jeffhaobdb76512011-09-07 11:43:16 -07002459 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 * We don't have an object instance, so we can't find the concrete method. However, all of
2461 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002462 */
Ian Rogers9074b992011-10-26 17:41:55 -07002463 const RegType& return_type =
2464 reg_types_.FromDescriptor(abs_method->GetDeclaringClass()->GetClassLoader(),
2465 abs_method->GetReturnTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002467 just_set_result = true;
2468 }
2469 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 }
jeffhaobdb76512011-09-07 11:43:16 -07002471 case Instruction::NEG_INT:
2472 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::NEG_LONG:
2476 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530
2531 case Instruction::ADD_INT:
2532 case Instruction::SUB_INT:
2533 case Instruction::MUL_INT:
2534 case Instruction::REM_INT:
2535 case Instruction::DIV_INT:
2536 case Instruction::SHL_INT:
2537 case Instruction::SHR_INT:
2538 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::AND_INT:
2542 case Instruction::OR_INT:
2543 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002545 break;
2546 case Instruction::ADD_LONG:
2547 case Instruction::SUB_LONG:
2548 case Instruction::MUL_LONG:
2549 case Instruction::DIV_LONG:
2550 case Instruction::REM_LONG:
2551 case Instruction::AND_LONG:
2552 case Instruction::OR_LONG:
2553 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::SHL_LONG:
2557 case Instruction::SHR_LONG:
2558 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 /* shift distance is Int, making these different from other binary operations */
2560 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002561 break;
2562 case Instruction::ADD_FLOAT:
2563 case Instruction::SUB_FLOAT:
2564 case Instruction::MUL_FLOAT:
2565 case Instruction::DIV_FLOAT:
2566 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::ADD_DOUBLE:
2570 case Instruction::SUB_DOUBLE:
2571 case Instruction::MUL_DOUBLE:
2572 case Instruction::DIV_DOUBLE:
2573 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::ADD_INT_2ADDR:
2577 case Instruction::SUB_INT_2ADDR:
2578 case Instruction::MUL_INT_2ADDR:
2579 case Instruction::REM_INT_2ADDR:
2580 case Instruction::SHL_INT_2ADDR:
2581 case Instruction::SHR_INT_2ADDR:
2582 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::AND_INT_2ADDR:
2586 case Instruction::OR_INT_2ADDR:
2587 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::ADD_LONG_2ADDR:
2594 case Instruction::SUB_LONG_2ADDR:
2595 case Instruction::MUL_LONG_2ADDR:
2596 case Instruction::DIV_LONG_2ADDR:
2597 case Instruction::REM_LONG_2ADDR:
2598 case Instruction::AND_LONG_2ADDR:
2599 case Instruction::OR_LONG_2ADDR:
2600 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::SHL_LONG_2ADDR:
2604 case Instruction::SHR_LONG_2ADDR:
2605 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::ADD_FLOAT_2ADDR:
2609 case Instruction::SUB_FLOAT_2ADDR:
2610 case Instruction::MUL_FLOAT_2ADDR:
2611 case Instruction::DIV_FLOAT_2ADDR:
2612 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::ADD_DOUBLE_2ADDR:
2616 case Instruction::SUB_DOUBLE_2ADDR:
2617 case Instruction::MUL_DOUBLE_2ADDR:
2618 case Instruction::DIV_DOUBLE_2ADDR:
2619 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::ADD_INT_LIT16:
2623 case Instruction::RSUB_INT:
2624 case Instruction::MUL_INT_LIT16:
2625 case Instruction::DIV_INT_LIT16:
2626 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::AND_INT_LIT16:
2630 case Instruction::OR_INT_LIT16:
2631 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::ADD_INT_LIT8:
2635 case Instruction::RSUB_INT_LIT8:
2636 case Instruction::MUL_INT_LIT8:
2637 case Instruction::DIV_INT_LIT8:
2638 case Instruction::REM_INT_LIT8:
2639 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002640 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::AND_INT_LIT8:
2645 case Instruction::OR_INT_LIT8:
2646 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649
2650 /*
2651 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002652 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002653 * inserted in the course of verification, we can expect to see it here.
2654 */
jeffhaob4df5142011-09-19 20:25:32 -07002655 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002659 case Instruction::UNUSED_EE:
2660 case Instruction::UNUSED_EF:
2661 case Instruction::UNUSED_F2:
2662 case Instruction::UNUSED_F3:
2663 case Instruction::UNUSED_F4:
2664 case Instruction::UNUSED_F5:
2665 case Instruction::UNUSED_F6:
2666 case Instruction::UNUSED_F7:
2667 case Instruction::UNUSED_F8:
2668 case Instruction::UNUSED_F9:
2669 case Instruction::UNUSED_FA:
2670 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002671 case Instruction::UNUSED_F0:
2672 case Instruction::UNUSED_F1:
2673 case Instruction::UNUSED_E3:
2674 case Instruction::UNUSED_E8:
2675 case Instruction::UNUSED_E7:
2676 case Instruction::UNUSED_E4:
2677 case Instruction::UNUSED_E9:
2678 case Instruction::UNUSED_FC:
2679 case Instruction::UNUSED_E5:
2680 case Instruction::UNUSED_EA:
2681 case Instruction::UNUSED_FD:
2682 case Instruction::UNUSED_E6:
2683 case Instruction::UNUSED_EB:
2684 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002685 case Instruction::UNUSED_3E:
2686 case Instruction::UNUSED_3F:
2687 case Instruction::UNUSED_40:
2688 case Instruction::UNUSED_41:
2689 case Instruction::UNUSED_42:
2690 case Instruction::UNUSED_43:
2691 case Instruction::UNUSED_73:
2692 case Instruction::UNUSED_79:
2693 case Instruction::UNUSED_7A:
2694 case Instruction::UNUSED_EC:
2695 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002696 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698
2699 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002700 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002701 * complain if an instruction is missing (which is desirable).
2702 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002704
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 if (failure_ != VERIFY_ERROR_NONE) {
2706 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002707 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002708 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002709 return false;
2710 } else {
2711 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002712 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002714 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002716 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002718 opcode_flag = Instruction::kThrow;
2719 }
2720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 * If we didn't just set the result register, clear it out. This ensures that you can only use
2723 * "move-result" immediately after the result is set. (We could check this statically, but it's
2724 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002725 */
2726 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002728 }
2729
jeffhaoa0a764a2011-09-16 10:43:38 -07002730 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002731 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2733 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2734 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002735 return false;
2736 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2738 // next instruction isn't one.
2739 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002740 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 }
2742 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2743 if (next_line != NULL) {
2744 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2745 // needed.
2746 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002747 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 }
jeffhaobdb76512011-09-07 11:43:16 -07002749 } else {
2750 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 * We're not recording register data for the next instruction, so we don't know what the prior
2752 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002753 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002755 }
2756 }
2757
2758 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002759 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002760 *
2761 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002762 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002763 * somebody could get a reference field, check it for zero, and if the
2764 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002765 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002766 * that, and will reject the code.
2767 *
2768 * TODO: avoid re-fetching the branch target
2769 */
2770 if ((opcode_flag & Instruction::kBranch) != 0) {
2771 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002773 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002775 return false;
2776 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002777 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002779 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 }
jeffhaobdb76512011-09-07 11:43:16 -07002781 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002783 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 }
jeffhaobdb76512011-09-07 11:43:16 -07002785 }
2786
2787 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002788 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002789 *
2790 * We've already verified that the table is structurally sound, so we
2791 * just need to walk through and tag the targets.
2792 */
2793 if ((opcode_flag & Instruction::kSwitch) != 0) {
2794 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2795 const uint16_t* switch_insns = insns + offset_to_switch;
2796 int switch_count = switch_insns[1];
2797 int offset_to_targets, targ;
2798
2799 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2800 /* 0 = sig, 1 = count, 2/3 = first key */
2801 offset_to_targets = 4;
2802 } else {
2803 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002804 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002805 offset_to_targets = 2 + 2 * switch_count;
2806 }
2807
2808 /* verify each switch target */
2809 for (targ = 0; targ < switch_count; targ++) {
2810 int offset;
2811 uint32_t abs_offset;
2812
2813 /* offsets are 32-bit, and only partly endian-swapped */
2814 offset = switch_insns[offset_to_targets + targ * 2] |
2815 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 abs_offset = work_insn_idx_ + offset;
2817 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2818 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002819 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 }
2821 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002822 return false;
2823 }
2824 }
2825
2826 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2828 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002829 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2831 bool within_catch_all = false;
2832 DexFile::CatchHandlerIterator iterator =
2833 DexFile::dexFindCatchHandler(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002834
2835 for (; !iterator.HasNext(); iterator.Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 if (iterator.Get().type_idx_ == DexFile::kDexNoIndex) {
2837 within_catch_all = true;
2838 }
jeffhaobdb76512011-09-07 11:43:16 -07002839 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2841 * "work_regs", because at runtime the exception will be thrown before the instruction
2842 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002843 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 if (!UpdateRegisters(iterator.Get().address_, saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002845 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 }
jeffhaobdb76512011-09-07 11:43:16 -07002847 }
2848
2849 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2851 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002852 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002854 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 * The state in work_line reflects the post-execution state. If the current instruction is a
2856 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002857 * it will do so before grabbing the lock).
2858 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2860 Fail(VERIFY_ERROR_GENERIC)
2861 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002862 return false;
2863 }
2864 }
2865 }
2866
jeffhaod1f0fde2011-09-08 17:25:33 -07002867 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 if ((opcode_flag & Instruction::kReturn) != 0) {
2869 if(!work_line_->VerifyMonitorStackEmpty()) {
2870 return false;
2871 }
jeffhaobdb76512011-09-07 11:43:16 -07002872 }
2873
2874 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002875 * Update start_guess. Advance to the next instruction of that's
2876 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002877 * neither of those exists we're in a return or throw; leave start_guess
2878 * alone and let the caller sort it out.
2879 */
2880 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002882 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2883 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002885 }
2886
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2888 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002889
2890 return true;
2891}
2892
Ian Rogersd81871c2011-10-03 13:57:23 -07002893Class* DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
2894 const Class* referrer = method_->GetDeclaringClass();
2895 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2896 Class* res_class = class_linker->ResolveType(*dex_file_, class_idx, referrer);
jeffhaobdb76512011-09-07 11:43:16 -07002897
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 if (res_class == NULL) {
2899 Thread::Current()->ClearException();
2900 Fail(VERIFY_ERROR_NO_CLASS) << "can't find class with index " << (void*) class_idx;
2901 } else if (!referrer->CanAccess(res_class)) { /* Check if access is allowed. */
2902 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: "
2903 << referrer->GetDescriptor()->ToModifiedUtf8() << " -> "
2904 << res_class->GetDescriptor()->ToModifiedUtf8();
2905 }
2906 return res_class;
2907}
2908
2909Class* DexVerifier::GetCaughtExceptionType() {
2910 Class* common_super = NULL;
2911 if (code_item_->tries_size_ != 0) {
2912 const byte* handlers_ptr = DexFile::dexGetCatchHandlerData(*code_item_, 0);
2913 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2914 for (uint32_t i = 0; i < handlers_size; i++) {
2915 DexFile::CatchHandlerIterator iterator(handlers_ptr);
2916 for (; !iterator.HasNext(); iterator.Next()) {
2917 DexFile::CatchHandlerItem handler = iterator.Get();
2918 if (handler.address_ == (uint32_t) work_insn_idx_) {
2919 if (handler.type_idx_ == DexFile::kDexNoIndex) {
2920 common_super = JavaLangThrowable();
2921 } else {
2922 Class* klass = ResolveClassAndCheckAccess(handler.type_idx_);
2923 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2924 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2925 * test, so is essentially harmless.
2926 */
2927 if (klass == NULL) {
2928 Fail(VERIFY_ERROR_GENERIC) << "unable to resolve exception class "
2929 << handler.type_idx_ << " ("
2930 << dex_file_->dexStringByTypeIdx(handler.type_idx_) << ")";
2931 return NULL;
2932 } else if(!JavaLangThrowable()->IsAssignableFrom(klass)) {
2933 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << PrettyClass(klass);
2934 return NULL;
2935 } else if (common_super == NULL) {
2936 common_super = klass;
2937 } else {
2938 common_super = RegType::ClassJoin(common_super, klass);
2939 }
2940 }
2941 }
2942 }
2943 handlers_ptr = iterator.GetData();
2944 }
2945 }
2946 if (common_super == NULL) {
2947 /* no catch blocks, or no catches with classes we can find */
2948 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
2949 }
2950 return common_super;
2951}
2952
2953Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
2954 Class* referrer = method_->GetDeclaringClass();
2955 DexCache* dex_cache = referrer->GetDexCache();
2956 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
2957 if (res_method == NULL) {
2958 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2959 Class* klass = ResolveClassAndCheckAccess(method_id.class_idx_);
2960 if (klass == NULL) {
2961 DCHECK(failure_ != VERIFY_ERROR_NONE);
2962 return NULL;
2963 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002964 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 std::string signature(dex_file_->CreateMethodDescriptor(method_id.proto_idx_, NULL));
2966 if (is_direct) {
2967 res_method = klass->FindDirectMethod(name, signature);
2968 } else if (klass->IsInterface()) {
2969 res_method = klass->FindInterfaceMethod(name, signature);
2970 } else {
2971 res_method = klass->FindVirtualMethod(name, signature);
2972 }
2973 if (res_method != NULL) {
2974 dex_cache->SetResolvedMethod(method_idx, res_method);
2975 } else {
2976 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2977 << PrettyDescriptor(klass->GetDescriptor()) << "." << name
2978 << " " << signature;
2979 return NULL;
2980 }
2981 }
2982 /* Check if access is allowed. */
2983 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
2984 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
2985 << " from " << PrettyDescriptor(referrer->GetDescriptor()) << ")";
2986 return NULL;
2987 }
2988 return res_method;
2989}
2990
2991Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
2992 MethodType method_type, bool is_range, bool is_super) {
2993 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2994 // we're making.
2995 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
2996 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
2997 if (res_method == NULL) {
2998 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dec_insn.vB_);
2999 const char* method_name = dex_file_->GetMethodName(method_id);
3000 std::string method_signature = dex_file_->GetMethodSignature(method_id);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003001 const char* class_descriptor = dex_file_->GetMethodDeclaringClassDescriptor(method_id);
Ian Rogersb5e95b92011-10-25 23:28:55 -07003002 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
3003 fail_messages_ << "unable to resolve method " << dec_insn.vB_ << ": "
3004 << class_descriptor << "." << method_name << " " << method_signature;
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 return NULL;
3006 }
3007 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3008 // enforce them here.
3009 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3010 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3011 << PrettyMethod(res_method);
3012 return NULL;
3013 }
3014 // See if the method type implied by the invoke instruction matches the access flags for the
3015 // target method.
3016 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3017 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3018 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3019 ) {
3020 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3021 << PrettyMethod(res_method);
3022 return NULL;
3023 }
3024 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3025 // has a vtable entry for the target method.
3026 if (is_super) {
3027 DCHECK(method_type == METHOD_VIRTUAL);
3028 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3029 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3030 if (super == NULL) { // Only Object has no super class
3031 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3032 << " to super " << PrettyMethod(res_method);
3033 } else {
3034 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3035 << " to super " << PrettyDescriptor(super->GetDescriptor())
3036 << "." << res_method->GetName()->ToModifiedUtf8()
3037 << " " << res_method->GetSignature()->ToModifiedUtf8();
3038
3039 }
3040 return NULL;
3041 }
3042 }
3043 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3044 // match the call to the signature. Also, we might might be calling through an abstract method
3045 // definition (which doesn't have register count values).
3046 int expected_args = dec_insn.vA_;
3047 /* caught by static verifier */
3048 DCHECK(is_range || expected_args <= 5);
3049 if (expected_args > code_item_->outs_size_) {
3050 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3051 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3052 return NULL;
3053 }
3054 std::string sig = res_method->GetSignature()->ToModifiedUtf8();
3055 if (sig[0] != '(') {
3056 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3057 << " as descriptor doesn't start with '(': " << sig;
3058 return NULL;
3059 }
jeffhaobdb76512011-09-07 11:43:16 -07003060 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 * Check the "this" argument, which must be an instance of the class
3062 * that declared the method. For an interface class, we don't do the
3063 * full interface merge, so we can't do a rigorous check here (which
3064 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003065 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 int actual_args = 0;
3067 if (!res_method->IsStatic()) {
3068 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3069 if (failure_ != VERIFY_ERROR_NONE) {
3070 return NULL;
3071 }
3072 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3073 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3074 return NULL;
3075 }
3076 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003077 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3078 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3079 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3080 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 return NULL;
3082 }
3083 }
3084 actual_args++;
3085 }
3086 /*
3087 * Process the target method's signature. This signature may or may not
3088 * have been verified, so we can't assume it's properly formed.
3089 */
3090 size_t sig_offset = 0;
3091 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3092 if (actual_args >= expected_args) {
3093 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3094 << "'. Expected " << expected_args << " args, found more ("
3095 << sig.substr(sig_offset) << ")";
3096 return NULL;
3097 }
3098 std::string descriptor;
3099 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3100 size_t end;
3101 if (sig[sig_offset] == 'L') {
3102 end = sig.find(';', sig_offset);
3103 } else {
3104 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3105 if (sig[end] == 'L') {
3106 end = sig.find(';', end);
3107 }
3108 }
3109 if (end == std::string::npos) {
3110 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3111 << "bad signature component '" << sig << "' (missing ';')";
3112 return NULL;
3113 }
3114 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3115 sig_offset = end;
3116 } else {
3117 descriptor = sig[sig_offset];
3118 }
3119 const RegType& reg_type =
3120 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003121 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3122 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3123 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003124 }
3125 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3126 }
3127 if (sig[sig_offset] != ')') {
3128 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3129 return NULL;
3130 }
3131 if (actual_args != expected_args) {
3132 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3133 << " expected " << expected_args << " args, found " << actual_args;
3134 return NULL;
3135 } else {
3136 return res_method;
3137 }
3138}
3139
3140void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3141 const RegType& insn_type, bool is_primitive) {
3142 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3143 if (!index_type.IsArrayIndexTypes()) {
3144 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3145 } else {
3146 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3147 if (failure_ == VERIFY_ERROR_NONE) {
3148 if (array_class == NULL) {
3149 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3150 // instruction type. TODO: have a proper notion of bottom here.
3151 if (!is_primitive || insn_type.IsCategory1Types()) {
3152 // Reference or category 1
3153 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3154 } else {
3155 // Category 2
3156 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3157 }
3158 } else {
3159 /* verify the class */
3160 Class* component_class = array_class->GetComponentType();
3161 const RegType& component_type = reg_types_.FromClass(component_class);
3162 if (!array_class->IsArrayClass()) {
3163 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3164 << PrettyDescriptor(array_class->GetDescriptor()) << " with aget";
3165 } else if (component_class->IsPrimitive() && !is_primitive) {
3166 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3167 << PrettyDescriptor(array_class->GetDescriptor())
3168 << " source for aget-object";
3169 } else if (!component_class->IsPrimitive() && is_primitive) {
3170 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3171 << PrettyDescriptor(array_class->GetDescriptor())
3172 << " source for category 1 aget";
3173 } else if (is_primitive && !insn_type.Equals(component_type) &&
3174 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3175 (insn_type.IsLong() && component_type.IsDouble()))) {
3176 Fail(VERIFY_ERROR_GENERIC) << "array type "
3177 << PrettyDescriptor(array_class->GetDescriptor())
3178 << " incompatible with aget of type " << insn_type;
3179 } else {
3180 // Use knowledge of the field type which is stronger than the type inferred from the
3181 // instruction, which can't differentiate object types and ints from floats, longs from
3182 // doubles.
3183 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3184 }
3185 }
3186 }
3187 }
3188}
3189
3190void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3191 const RegType& insn_type, bool is_primitive) {
3192 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3193 if (!index_type.IsArrayIndexTypes()) {
3194 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3195 } else {
3196 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3197 if (failure_ == VERIFY_ERROR_NONE) {
3198 if (array_class == NULL) {
3199 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3200 // instruction type.
3201 } else {
3202 /* verify the class */
3203 Class* component_class = array_class->GetComponentType();
3204 const RegType& component_type = reg_types_.FromClass(component_class);
3205 if (!array_class->IsArrayClass()) {
3206 Fail(VERIFY_ERROR_GENERIC) << "not array type "
3207 << PrettyDescriptor(array_class->GetDescriptor()) << " with aput";
3208 } else if (component_class->IsPrimitive() && !is_primitive) {
3209 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
3210 << PrettyDescriptor(array_class->GetDescriptor())
3211 << " source for aput-object";
3212 } else if (!component_class->IsPrimitive() && is_primitive) {
3213 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
3214 << PrettyDescriptor(array_class->GetDescriptor())
3215 << " source for category 1 aput";
3216 } else if (is_primitive && !insn_type.Equals(component_type) &&
3217 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3218 (insn_type.IsLong() && component_type.IsDouble()))) {
3219 Fail(VERIFY_ERROR_GENERIC) << "array type "
3220 << PrettyDescriptor(array_class->GetDescriptor())
3221 << " incompatible with aput of type " << insn_type;
3222 } else {
3223 // The instruction agrees with the type of array, confirm the value to be stored does too
3224 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3225 }
3226 }
3227 }
3228 }
3229}
3230
3231Field* DexVerifier::GetStaticField(int field_idx) {
3232 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3233 if (field == NULL) {
3234 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3235 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve static field " << field_idx << " ("
3236 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003237 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 DCHECK(Thread::Current()->IsExceptionPending());
3239 Thread::Current()->ClearException();
3240 return NULL;
3241 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3242 field->GetAccessFlags())) {
3243 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3244 << " from " << PrettyClass(method_->GetDeclaringClass());
3245 return NULL;
3246 } else if (!field->IsStatic()) {
3247 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3248 return NULL;
3249 } else {
3250 return field;
3251 }
3252}
3253
Ian Rogersd81871c2011-10-03 13:57:23 -07003254Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3255 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3256 if (field == NULL) {
3257 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3258 Fail(VERIFY_ERROR_NO_FIELD) << "unable to resolve instance field " << field_idx << " ("
3259 << dex_file_->GetFieldName(field_id) << ") in "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003260 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 DCHECK(Thread::Current()->IsExceptionPending());
3262 Thread::Current()->ClearException();
3263 return NULL;
3264 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3265 field->GetAccessFlags())) {
3266 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3267 << " from " << PrettyClass(method_->GetDeclaringClass());
3268 return NULL;
3269 } else if (field->IsStatic()) {
3270 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3271 << " to not be static";
3272 return NULL;
3273 } else if (obj_type.IsZero()) {
3274 // Cannot infer and check type, however, access will cause null pointer exception
3275 return field;
3276 } else if(obj_type.IsUninitializedReference() &&
3277 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3278 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3279 // Field accesses through uninitialized references are only allowable for constructors where
3280 // the field is declared in this class
3281 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3282 << " of a not fully initialized object within the context of "
3283 << PrettyMethod(method_);
3284 return NULL;
3285 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3286 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3287 // of C1. For resolution to occur the declared class of the field must be compatible with
3288 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3289 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3290 << " from object of type " << PrettyClass(obj_type.GetClass());
3291 return NULL;
3292 } else {
3293 return field;
3294 }
3295}
3296
Ian Rogersb94a27b2011-10-26 00:33:41 -07003297void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3298 const RegType& insn_type, bool is_primitive, bool is_static) {
3299 Field* field;
3300 if (is_static) {
3301 field = GetStaticField(dec_insn.vB_);
3302 } else {
3303 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3304 field = GetInstanceField(object_type, dec_insn.vC_);
3305 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 if (field != NULL) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003307 const RegType& field_type =
3308 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3309 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003311 if (field_type.Equals(insn_type) ||
3312 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3313 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 // expected that read is of the correct primitive type or that int reads are reading
3315 // floats or long reads are reading doubles
3316 } else {
3317 // This is a global failure rather than a class change failure as the instructions and
3318 // the descriptors for the type should have been consistent within the same file at
3319 // compile time
3320 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003321 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003322 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 return;
3324 }
3325 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003326 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003327 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003328 << " to be compatible with type '" << insn_type
3329 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003330 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 return;
3332 }
3333 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003334 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 }
3336}
3337
Ian Rogersb94a27b2011-10-26 00:33:41 -07003338void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3339 const RegType& insn_type, bool is_primitive, bool is_static) {
3340 Field* field;
3341 if (is_static) {
3342 field = GetStaticField(dec_insn.vB_);
3343 } else {
3344 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
3345 field = GetInstanceField(object_type, dec_insn.vC_);
3346 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003347 if (field != NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3349 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3350 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3351 return;
3352 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003353 const RegType& field_type =
3354 reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3355 field->GetTypeDescriptor());
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003357 // Primitive field assignability rules are weaker than regular assignability rules
3358 bool instruction_compatible;
3359 bool value_compatible;
3360 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3361 if (field_type.IsIntegralTypes()) {
3362 instruction_compatible = insn_type.IsIntegralTypes();
3363 value_compatible = value_type.IsIntegralTypes();
3364 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003365 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003366 value_compatible = value_type.IsFloatTypes();
3367 } else if (field_type.IsLong()) {
3368 instruction_compatible = insn_type.IsLong();
3369 value_compatible = value_type.IsLongTypes();
3370 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003371 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003372 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003374 instruction_compatible = false; // reference field with primitive store
3375 value_compatible = false; // unused
3376 }
3377 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 // This is a global failure rather than a class change failure as the instructions and
3379 // the descriptors for the type should have been consistent within the same file at
3380 // compile time
3381 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003382 << " to be of type '" << insn_type
3383 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003384 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003385 return;
3386 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003387 if (!value_compatible) {
3388 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3389 << " of type " << value_type
3390 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003391 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003392 return;
3393 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003395 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003397 << " to be compatible with type '" << insn_type
3398 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003399 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003400 return;
3401 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003402 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003404 }
3405}
3406
3407bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3408 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3409 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3410 return false;
3411 }
3412 return true;
3413}
3414
3415void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
3416 Class* res_class, bool is_range) {
3417 DCHECK(res_class->IsArrayClass()) << PrettyClass(res_class); // Checked before calling.
3418 /*
3419 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3420 * list and fail. It's legal, if silly, for arg_count to be zero.
3421 */
3422 const RegType& expected_type = reg_types_.FromClass(res_class->GetComponentType());
3423 uint32_t arg_count = dec_insn.vA_;
3424 for (size_t ui = 0; ui < arg_count; ui++) {
3425 uint32_t get_reg;
3426
3427 if (is_range)
3428 get_reg = dec_insn.vC_ + ui;
3429 else
3430 get_reg = dec_insn.arg_[ui];
3431
3432 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3433 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3434 << ") not valid";
3435 return;
3436 }
3437 }
3438}
3439
3440void DexVerifier::ReplaceFailingInstruction() {
3441 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3442 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3443 VerifyErrorRefType ref_type;
3444 switch (inst->Opcode()) {
3445 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003446 case Instruction::CHECK_CAST:
3447 case Instruction::INSTANCE_OF:
3448 case Instruction::NEW_INSTANCE:
3449 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003451 case Instruction::FILLED_NEW_ARRAY_RANGE:
3452 ref_type = VERIFY_ERROR_REF_CLASS;
3453 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003455 case Instruction::IGET_BOOLEAN:
3456 case Instruction::IGET_BYTE:
3457 case Instruction::IGET_CHAR:
3458 case Instruction::IGET_SHORT:
3459 case Instruction::IGET_WIDE:
3460 case Instruction::IGET_OBJECT:
3461 case Instruction::IPUT:
3462 case Instruction::IPUT_BOOLEAN:
3463 case Instruction::IPUT_BYTE:
3464 case Instruction::IPUT_CHAR:
3465 case Instruction::IPUT_SHORT:
3466 case Instruction::IPUT_WIDE:
3467 case Instruction::IPUT_OBJECT:
3468 case Instruction::SGET:
3469 case Instruction::SGET_BOOLEAN:
3470 case Instruction::SGET_BYTE:
3471 case Instruction::SGET_CHAR:
3472 case Instruction::SGET_SHORT:
3473 case Instruction::SGET_WIDE:
3474 case Instruction::SGET_OBJECT:
3475 case Instruction::SPUT:
3476 case Instruction::SPUT_BOOLEAN:
3477 case Instruction::SPUT_BYTE:
3478 case Instruction::SPUT_CHAR:
3479 case Instruction::SPUT_SHORT:
3480 case Instruction::SPUT_WIDE:
3481 case Instruction::SPUT_OBJECT:
3482 ref_type = VERIFY_ERROR_REF_FIELD;
3483 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003485 case Instruction::INVOKE_VIRTUAL_RANGE:
3486 case Instruction::INVOKE_SUPER:
3487 case Instruction::INVOKE_SUPER_RANGE:
3488 case Instruction::INVOKE_DIRECT:
3489 case Instruction::INVOKE_DIRECT_RANGE:
3490 case Instruction::INVOKE_STATIC:
3491 case Instruction::INVOKE_STATIC_RANGE:
3492 case Instruction::INVOKE_INTERFACE:
3493 case Instruction::INVOKE_INTERFACE_RANGE:
3494 ref_type = VERIFY_ERROR_REF_METHOD;
3495 break;
jeffhaobdb76512011-09-07 11:43:16 -07003496 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003497 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003498 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003499 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3501 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3502 // instruction, so assert it.
3503 size_t width = inst->SizeInCodeUnits();
3504 CHECK_GT(width, 1u);
3505 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3506 // NOPs
3507 for (size_t i = 2; i < width; i++) {
3508 insns[work_insn_idx_ + i] = Instruction::NOP;
3509 }
3510 // Encode the opcode, with the failure code in the high byte
3511 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3512 (failure_ << 8) | // AA - component
3513 (ref_type << (8 + kVerifyErrorRefTypeShift));
3514 insns[work_insn_idx_] = new_instruction;
3515 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3516 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003517 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3518 << fail_messages_.str();
3519 if (gDebugVerify) {
3520 std::cout << std::endl << info_messages_.str();
3521 Dump(std::cout);
3522 }
jeffhaobdb76512011-09-07 11:43:16 -07003523}
jeffhaoba5ebb92011-08-25 17:24:37 -07003524
Ian Rogersd81871c2011-10-03 13:57:23 -07003525bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3526 const bool merge_debug = true;
3527 bool changed = true;
3528 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3529 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003530 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 * We haven't processed this instruction before, and we haven't touched the registers here, so
3532 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3533 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003534 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003536 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3538 copy->CopyFromLine(target_line);
3539 changed = target_line->MergeRegisters(merge_line);
3540 if (failure_ != VERIFY_ERROR_NONE) {
3541 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003542 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003543 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3545 << *copy.get() << " MERGE" << std::endl
3546 << *merge_line << " ==" << std::endl
3547 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003548 }
3549 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 if (changed) {
3551 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003552 }
3553 return true;
3554}
3555
Ian Rogersd81871c2011-10-03 13:57:23 -07003556void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3557 size_t* log2_max_gc_pc) {
3558 size_t local_gc_points = 0;
3559 size_t max_insn = 0;
3560 size_t max_ref_reg = -1;
3561 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3562 if (insn_flags_[i].IsGcPoint()) {
3563 local_gc_points++;
3564 max_insn = i;
3565 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003566 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003567 }
3568 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 *gc_points = local_gc_points;
3570 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3571 size_t i = 0;
3572 while ((1U << i) < max_insn) {
3573 i++;
3574 }
3575 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003576}
3577
Ian Rogersd81871c2011-10-03 13:57:23 -07003578ByteArray* DexVerifier::GenerateGcMap() {
3579 size_t num_entries, ref_bitmap_bits, pc_bits;
3580 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3581 // There's a single byte to encode the size of each bitmap
3582 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3583 // TODO: either a better GC map format or per method failures
3584 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3585 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003586 return NULL;
3587 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3589 // There are 2 bytes to encode the number of entries
3590 if (num_entries >= 65536) {
3591 // TODO: either a better GC map format or per method failures
3592 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3593 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003594 return NULL;
3595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003597 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003599 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 pc_bytes = 1;
3601 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003602 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003604 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003605 // TODO: either a better GC map format or per method failures
3606 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3607 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3608 return NULL;
3609 }
3610 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3611 ByteArray* table = ByteArray::Alloc(table_size);
3612 if (table == NULL) {
3613 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3614 return NULL;
3615 }
3616 // Write table header
3617 table->Set(0, format);
3618 table->Set(1, ref_bitmap_bytes);
3619 table->Set(2, num_entries & 0xFF);
3620 table->Set(3, (num_entries >> 8) & 0xFF);
3621 // Write table data
3622 size_t offset = 4;
3623 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3624 if (insn_flags_[i].IsGcPoint()) {
3625 table->Set(offset, i & 0xFF);
3626 offset++;
3627 if (pc_bytes == 2) {
3628 table->Set(offset, (i >> 8) & 0xFF);
3629 offset++;
3630 }
3631 RegisterLine* line = reg_table_.GetLine(i);
3632 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3633 offset += ref_bitmap_bytes;
3634 }
3635 }
3636 DCHECK(offset == table_size);
3637 return table;
3638}
jeffhaoa0a764a2011-09-16 10:43:38 -07003639
Ian Rogersd81871c2011-10-03 13:57:23 -07003640void DexVerifier::VerifyGcMap() {
3641 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3642 // that the table data is well formed and all references are marked (or not) in the bitmap
3643 PcToReferenceMap map(method_);
3644 size_t map_index = 0;
3645 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3646 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3647 if (insn_flags_[i].IsGcPoint()) {
3648 CHECK_LT(map_index, map.NumEntries());
3649 CHECK_EQ(map.GetPC(map_index), i);
3650 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3651 map_index++;
3652 RegisterLine* line = reg_table_.GetLine(i);
3653 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003654 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 CHECK_LT(j / 8, map.RegWidth());
3656 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3657 } else if ((j / 8) < map.RegWidth()) {
3658 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3659 } else {
3660 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3661 }
3662 }
3663 } else {
3664 CHECK(reg_bitmap == NULL);
3665 }
3666 }
3667}
jeffhaoa0a764a2011-09-16 10:43:38 -07003668
Ian Rogersd81871c2011-10-03 13:57:23 -07003669Class* DexVerifier::JavaLangThrowable() {
3670 if (java_lang_throwable_ == NULL) {
3671 java_lang_throwable_ =
3672 Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
3673 DCHECK(java_lang_throwable_ != NULL);
3674 }
3675 return java_lang_throwable_;
3676}
3677
3678const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3679 size_t num_entries = NumEntries();
3680 // Do linear or binary search?
3681 static const size_t kSearchThreshold = 8;
3682 if (num_entries < kSearchThreshold) {
3683 for (size_t i = 0; i < num_entries; i++) {
3684 if (GetPC(i) == dex_pc) {
3685 return GetBitMap(i);
3686 }
3687 }
3688 } else {
3689 int lo = 0;
3690 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003691 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 int mid = (hi + lo) / 2;
3693 int mid_pc = GetPC(mid);
3694 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003695 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003696 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003697 hi = mid - 1;
3698 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003700 }
3701 }
3702 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 if (error_if_not_present) {
3704 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3705 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003706 return NULL;
3707}
3708
Ian Rogersd81871c2011-10-03 13:57:23 -07003709} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003710} // namespace art