blob: 768880adec3f2165c2aa047a02e34592b479445e [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"
Ian Rogers0571d352011-11-03 19:51:38 -070014#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070015#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080016#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070018#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
20namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070021namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022
Ian Rogers2c8a8572011-10-24 17:11:36 -070023static const bool gDebugVerify = false;
24
Ian Rogersd81871c2011-10-03 13:57:23 -070025std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
26 return os << (int)rhs;
27}
jeffhaobdb76512011-09-07 11:43:16 -070028
Ian Rogers84fa0742011-10-25 18:13:30 -070029static const char* type_strings[] = {
30 "Unknown",
31 "Conflict",
32 "Boolean",
33 "Byte",
34 "Short",
35 "Char",
36 "Integer",
37 "Float",
38 "Long (Low Half)",
39 "Long (High Half)",
40 "Double (Low Half)",
41 "Double (High Half)",
42 "64-bit Constant (Low Half)",
43 "64-bit Constant (High Half)",
44 "32-bit Constant",
45 "Unresolved Reference",
46 "Uninitialized Reference",
47 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070048 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070049 "Reference",
50};
Ian Rogersd81871c2011-10-03 13:57:23 -070051
Ian Rogers2c8a8572011-10-24 17:11:36 -070052std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070053 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
54 std::string result;
55 if (IsConstant()) {
56 uint32_t val = ConstantValue();
57 if (val == 0) {
58 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070059 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070060 if(IsConstantShort()) {
61 result = StringPrintf("32-bit Constant: %d", val);
62 } else {
63 result = StringPrintf("32-bit Constant: 0x%x", val);
64 }
65 }
66 } else {
67 result = type_strings[type_];
68 if (IsReferenceTypes()) {
69 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -070070 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070071 result += PrettyDescriptor(GetDescriptor());
72 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -070074 }
Ian Rogersd81871c2011-10-03 13:57:23 -070075 }
76 }
Ian Rogers84fa0742011-10-25 18:13:30 -070077 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070078}
79
80const RegType& RegType::HighHalf(RegTypeCache* cache) const {
81 CHECK(IsLowHalf());
82 if (type_ == kRegTypeLongLo) {
83 return cache->FromType(kRegTypeLongHi);
84 } else if (type_ == kRegTypeDoubleLo) {
85 return cache->FromType(kRegTypeDoubleHi);
86 } else {
87 return cache->FromType(kRegTypeConstHi);
88 }
89}
90
91/*
92 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
93 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
94 * 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
95 * is the deepest (lowest upper bound) parent of S and T).
96 *
97 * This operation applies for regular classes and arrays, however, for interface types there needn't
98 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
99 * introducing sets of types, however, the only operation permissible on an interface is
100 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
101 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700102 * the perversion of any Object being assignable to an interface type (note, however, that we don't
103 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
104 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700105 */
106Class* RegType::ClassJoin(Class* s, Class* t) {
107 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
108 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
109 if (s == t) {
110 return s;
111 } else if (s->IsAssignableFrom(t)) {
112 return s;
113 } else if (t->IsAssignableFrom(s)) {
114 return t;
115 } else if (s->IsArrayClass() && t->IsArrayClass()) {
116 Class* s_ct = s->GetComponentType();
117 Class* t_ct = t->GetComponentType();
118 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
119 // Given the types aren't the same, if either array is of primitive types then the only
120 // common parent is java.lang.Object
121 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
122 DCHECK(result->IsObjectClass());
123 return result;
124 }
125 Class* common_elem = ClassJoin(s_ct, t_ct);
126 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
127 const ClassLoader* class_loader = s->GetClassLoader();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 std::string descriptor = "[";
129 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700130 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
131 DCHECK(array_class != NULL);
132 return array_class;
133 } else {
134 size_t s_depth = s->Depth();
135 size_t t_depth = t->Depth();
136 // Get s and t to the same depth in the hierarchy
137 if (s_depth > t_depth) {
138 while (s_depth > t_depth) {
139 s = s->GetSuperClass();
140 s_depth--;
141 }
142 } else {
143 while (t_depth > s_depth) {
144 t = t->GetSuperClass();
145 t_depth--;
146 }
147 }
148 // Go up the hierarchy until we get to the common parent
149 while (s != t) {
150 s = s->GetSuperClass();
151 t = t->GetSuperClass();
152 }
153 return s;
154 }
155}
156
Ian Rogersb5e95b92011-10-25 23:28:55 -0700157bool RegType::IsAssignableFrom(const RegType& src) const {
158 if (Equals(src)) {
159 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700160 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700161 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700162 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
163 case RegType::kRegTypeByte: return src.IsByteTypes();
164 case RegType::kRegTypeShort: return src.IsShortTypes();
165 case RegType::kRegTypeChar: return src.IsCharTypes();
166 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
167 case RegType::kRegTypeFloat: return src.IsFloatTypes();
168 case RegType::kRegTypeLongLo: return src.IsLongTypes();
169 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700170 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700171 if (!IsReferenceTypes()) {
172 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700173 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700174 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700175 return true; // all reference types can be assigned null
176 } else if (!src.IsReferenceTypes()) {
177 return false; // expect src to be a reference type
178 } else if (IsJavaLangObject()) {
179 return true; // all reference types can be assigned to Object
180 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700181 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700182 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700183 GetClass()->IsAssignableFrom(src.GetClass())) {
184 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700185 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700186 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700187 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700188 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700189 }
190 }
191}
192
Ian Rogers84fa0742011-10-25 18:13:30 -0700193static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
194 return a.IsConstant() ? b : a;
195}
jeffhaobdb76512011-09-07 11:43:16 -0700196
Ian Rogersd81871c2011-10-03 13:57:23 -0700197const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
198 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700199 if (IsUnknown() && incoming_type.IsUnknown()) {
200 return *this; // Unknown MERGE Unknown => Unknown
201 } else if (IsConflict()) {
202 return *this; // Conflict MERGE * => Conflict
203 } else if (incoming_type.IsConflict()) {
204 return incoming_type; // * MERGE Conflict => Conflict
205 } else if (IsUnknown() || incoming_type.IsUnknown()) {
206 return reg_types->Conflict(); // Unknown MERGE * => Conflict
207 } else if(IsConstant() && incoming_type.IsConstant()) {
208 int32_t val1 = ConstantValue();
209 int32_t val2 = incoming_type.ConstantValue();
210 if (val1 >= 0 && val2 >= 0) {
211 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
212 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700213 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700214 } else {
215 return incoming_type;
216 }
217 } else if (val1 < 0 && val2 < 0) {
218 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
219 if (val1 <= val2) {
220 return *this;
221 } else {
222 return incoming_type;
223 }
224 } else {
225 // Values are +ve and -ve, choose smallest signed type in which they both fit
226 if (IsConstantByte()) {
227 if (incoming_type.IsConstantByte()) {
228 return reg_types->ByteConstant();
229 } else if (incoming_type.IsConstantShort()) {
230 return reg_types->ShortConstant();
231 } else {
232 return reg_types->IntConstant();
233 }
234 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700235 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700236 return reg_types->ShortConstant();
237 } else {
238 return reg_types->IntConstant();
239 }
240 } else {
241 return reg_types->IntConstant();
242 }
243 }
244 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
245 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
246 return reg_types->Boolean(); // boolean MERGE boolean => boolean
247 }
248 if (IsByteTypes() && incoming_type.IsByteTypes()) {
249 return reg_types->Byte(); // byte MERGE byte => byte
250 }
251 if (IsShortTypes() && incoming_type.IsShortTypes()) {
252 return reg_types->Short(); // short MERGE short => short
253 }
254 if (IsCharTypes() && incoming_type.IsCharTypes()) {
255 return reg_types->Char(); // char MERGE char => char
256 }
257 return reg_types->Integer(); // int MERGE * => int
258 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
259 (IsLongTypes() && incoming_type.IsLongTypes()) ||
260 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
261 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
262 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
263 // check constant case was handled prior to entry
264 DCHECK(!IsConstant() || !incoming_type.IsConstant());
265 // float/long/double MERGE float/long/double_constant => float/long/double
266 return SelectNonConstant(*this, incoming_type);
267 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700268 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700269 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700270 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
271 return reg_types->JavaLangObject(); // Object MERGE ref => Object
272 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
273 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
274 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
275 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700276 return reg_types->Conflict();
277 } else { // Two reference types, compute Join
278 Class* c1 = GetClass();
279 Class* c2 = incoming_type.GetClass();
280 DCHECK(c1 != NULL && !c1->IsPrimitive());
281 DCHECK(c2 != NULL && !c2->IsPrimitive());
282 Class* join_class = ClassJoin(c1, c2);
283 if (c1 == join_class) {
284 return *this;
285 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 return incoming_type;
287 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700288 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700289 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700290 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700291 } else {
292 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700293 }
294}
295
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700296static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700297 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700298 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
299 case Primitive::kPrimByte: return RegType::kRegTypeByte;
300 case Primitive::kPrimShort: return RegType::kRegTypeShort;
301 case Primitive::kPrimChar: return RegType::kRegTypeChar;
302 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
303 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
304 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
305 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
306 case Primitive::kPrimVoid:
307 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700308 }
309}
310
311static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
312 if (descriptor.length() == 1) {
313 switch (descriptor[0]) {
314 case 'Z': return RegType::kRegTypeBoolean;
315 case 'B': return RegType::kRegTypeByte;
316 case 'S': return RegType::kRegTypeShort;
317 case 'C': return RegType::kRegTypeChar;
318 case 'I': return RegType::kRegTypeInteger;
319 case 'J': return RegType::kRegTypeLongLo;
320 case 'F': return RegType::kRegTypeFloat;
321 case 'D': return RegType::kRegTypeDoubleLo;
322 case 'V':
323 default: return RegType::kRegTypeUnknown;
324 }
325 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
326 return RegType::kRegTypeReference;
327 } else {
328 return RegType::kRegTypeUnknown;
329 }
330}
331
332std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700333 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700334 return os;
335}
336
337const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
338 const std::string& descriptor) {
339 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
340}
341
342const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
343 const std::string& descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700344 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 // entries should be sized greater than primitive types
346 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
347 RegType* entry = entries_[type];
348 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700349 Class* klass = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 if (descriptor.size() != 0) {
351 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
352 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700353 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700354 entries_[type] = entry;
355 }
356 return *entry;
357 } else {
358 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800359 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700360 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700361 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700362 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 if (cur_entry->IsReference()) {
364 kh.ChangeClass(cur_entry->GetClass());
365 if (descriptor == kh.GetDescriptor()) {
366 return *cur_entry;
367 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700368 } else if (cur_entry->IsUnresolvedReference() &&
369 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 return *cur_entry;
371 }
372 }
373 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700374 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700375 // Able to resolve so create resolved register type
376 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700377 entries_.push_back(entry);
378 return *entry;
379 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700380 // TODO: we assume unresolved, but we may be able to do better by validating whether the
381 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700382 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700383 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700384 Thread::Current()->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700385 if (IsValidDescriptor(descriptor.c_str())) {
386 String* string_descriptor =
387 Runtime::Current()->GetInternTable()->InternStrong(descriptor.c_str());
388 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
389 entries_.size());
390 entries_.push_back(entry);
391 return *entry;
392 } else {
393 // The descriptor is broken return the unknown type as there's nothing sensible that
394 // could be done at runtime
395 return Unknown();
396 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700397 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700398 }
399}
400
401const RegType& RegTypeCache::FromClass(Class* klass) {
402 if (klass->IsPrimitive()) {
403 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
404 // entries should be sized greater than primitive types
405 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
406 RegType* entry = entries_[type];
407 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700408 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 entries_[type] = entry;
410 }
411 return *entry;
412 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700413 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700414 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700415 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700416 return *cur_entry;
417 }
418 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700419 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700420 entries_.push_back(entry);
421 return *entry;
422 }
423}
424
Ian Rogers28ad40d2011-10-27 15:19:26 -0700425const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
426 RegType* entry;
427 if (type.IsUnresolvedTypes()) {
428 String* descriptor = type.GetDescriptor();
429 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
430 RegType* cur_entry = entries_[i];
431 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
432 cur_entry->GetAllocationPc() == allocation_pc &&
433 cur_entry->GetDescriptor() == descriptor) {
434 return *cur_entry;
435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700437 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
438 descriptor, allocation_pc, entries_.size());
439 } else {
440 Class* klass = type.GetClass();
441 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
442 RegType* cur_entry = entries_[i];
443 if (cur_entry->IsUninitializedReference() &&
444 cur_entry->GetAllocationPc() == allocation_pc &&
445 cur_entry->GetClass() == klass) {
446 return *cur_entry;
447 }
448 }
449 entry = new RegType(RegType::kRegTypeUninitializedReference,
450 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700452 entries_.push_back(entry);
453 return *entry;
454}
455
456const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
457 RegType* entry;
458 if (uninit_type.IsUnresolvedTypes()) {
459 String* descriptor = uninit_type.GetDescriptor();
460 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
461 RegType* cur_entry = entries_[i];
462 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
463 return *cur_entry;
464 }
465 }
466 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
467 } else {
468 Class* klass = uninit_type.GetClass();
469 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
470 RegType* cur_entry = entries_[i];
471 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
472 return *cur_entry;
473 }
474 }
475 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
476 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 entries_.push_back(entry);
478 return *entry;
479}
480
481const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700482 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 RegType* cur_entry = entries_[i];
484 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
485 return *cur_entry;
486 }
487 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700488 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 entries_.size());
490 entries_.push_back(entry);
491 return *entry;
492}
493
494const RegType& RegTypeCache::FromType(RegType::Type type) {
495 CHECK(type < RegType::kRegTypeReference);
496 switch (type) {
497 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
498 case RegType::kRegTypeByte: return From(type, NULL, "B");
499 case RegType::kRegTypeShort: return From(type, NULL, "S");
500 case RegType::kRegTypeChar: return From(type, NULL, "C");
501 case RegType::kRegTypeInteger: return From(type, NULL, "I");
502 case RegType::kRegTypeFloat: return From(type, NULL, "F");
503 case RegType::kRegTypeLongLo:
504 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
505 case RegType::kRegTypeDoubleLo:
506 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
507 default: return From(type, NULL, "");
508 }
509}
510
511const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700512 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
513 RegType* cur_entry = entries_[i];
514 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
515 return *cur_entry;
516 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700517 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700518 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
519 entries_.push_back(entry);
520 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700521}
522
Ian Rogers28ad40d2011-10-27 15:19:26 -0700523const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
524 CHECK(array.IsArrayClass());
525 if (array.IsUnresolvedTypes()) {
526 std::string descriptor = array.GetDescriptor()->ToModifiedUtf8();
527 std::string component = descriptor.substr(1, descriptor.size() - 1);
528 return FromDescriptor(loader, component);
529 } else {
530 return FromClass(array.GetClass()->GetComponentType());
531 }
532}
533
534
Ian Rogersd81871c2011-10-03 13:57:23 -0700535bool RegisterLine::CheckConstructorReturn() const {
536 for (size_t i = 0; i < num_regs_; i++) {
537 if (GetRegisterType(i).IsUninitializedThisReference()) {
538 verifier_->Fail(VERIFY_ERROR_GENERIC)
539 << "Constructor returning without calling superclass constructor";
540 return false;
541 }
542 }
543 return true;
544}
545
546void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
547 DCHECK(vdst < num_regs_);
548 if (new_type.IsLowHalf()) {
549 line_[vdst] = new_type.GetId();
550 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
551 } else if (new_type.IsHighHalf()) {
552 /* should never set these explicitly */
553 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
554 } else if (new_type.IsConflict()) { // should only be set during a merge
555 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
556 } else {
557 line_[vdst] = new_type.GetId();
558 }
559 // Clear the monitor entry bits for this register.
560 ClearAllRegToLockDepths(vdst);
561}
562
563void RegisterLine::SetResultTypeToUnknown() {
564 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
565 result_[0] = unknown_id;
566 result_[1] = unknown_id;
567}
568
569void RegisterLine::SetResultRegisterType(const RegType& new_type) {
570 result_[0] = new_type.GetId();
571 if(new_type.IsLowHalf()) {
572 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
573 result_[1] = new_type.GetId() + 1;
574 } else {
575 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
576 }
577}
578
579const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
580 // The register index was validated during the static pass, so we don't need to check it here.
581 DCHECK_LT(vsrc, num_regs_);
582 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
583}
584
585const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
586 if (dec_insn.vA_ < 1) {
587 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
588 return verifier_->GetRegTypeCache()->Unknown();
589 }
590 /* get the element type of the array held in vsrc */
591 const RegType& this_type = GetRegisterType(dec_insn.vC_);
592 if (!this_type.IsReferenceTypes()) {
593 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
594 << dec_insn.vC_ << " (type=" << this_type << ")";
595 return verifier_->GetRegTypeCache()->Unknown();
596 }
597 return this_type;
598}
599
600Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
601 /* get the element type of the array held in vsrc */
602 const RegType& type = GetRegisterType(vsrc);
603 /* if "always zero", we allow it to fail at runtime */
604 if (type.IsZero()) {
605 return NULL;
606 } else if (!type.IsReferenceTypes()) {
607 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
608 << " (type=" << type << ")";
609 return NULL;
610 } else if (type.IsUninitializedReference()) {
611 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
612 return NULL;
613 } else {
614 return type.GetClass();
615 }
616}
617
618bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
619 // Verify the src register type against the check type refining the type of the register
620 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700621 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
623 << " but expected " << check_type;
624 return false;
625 }
626 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
627 // precise than the subtype in vsrc so leave it for reference types. For primitive types
628 // if they are a defined type then they are as precise as we can get, however, for constant
629 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
630 return true;
631}
632
633void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700634 DCHECK(uninit_type.IsUninitializedTypes());
635 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
636 size_t changed = 0;
637 for (size_t i = 0; i < num_regs_; i++) {
638 if (GetRegisterType(i).Equals(uninit_type)) {
639 line_[i] = init_type.GetId();
640 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700643 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644}
645
646void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
647 for (size_t i = 0; i < num_regs_; i++) {
648 if (GetRegisterType(i).Equals(uninit_type)) {
649 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
650 ClearAllRegToLockDepths(i);
651 }
652 }
653}
654
655void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
656 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
657 const RegType& type = GetRegisterType(vsrc);
658 SetRegisterType(vdst, type);
659 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
660 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
661 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
662 << " cat=" << static_cast<int>(cat);
663 } else if (cat == kTypeCategoryRef) {
664 CopyRegToLockDepth(vdst, vsrc);
665 }
666}
667
668void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
669 const RegType& type_l = GetRegisterType(vsrc);
670 const RegType& type_h = GetRegisterType(vsrc + 1);
671
672 if (!type_l.CheckWidePair(type_h)) {
673 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
674 << " type=" << type_l << "/" << type_h;
675 } else {
676 SetRegisterType(vdst, type_l); // implicitly sets the second half
677 }
678}
679
680void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
681 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
682 if ((!is_reference && !type.IsCategory1Types()) ||
683 (is_reference && !type.IsReferenceTypes())) {
684 verifier_->Fail(VERIFY_ERROR_GENERIC)
685 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
686 } else {
687 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
688 SetRegisterType(vdst, type);
689 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
690 }
691}
692
693/*
694 * Implement "move-result-wide". Copy the category-2 value from the result
695 * register to another register, and reset the result register.
696 */
697void RegisterLine::CopyResultRegister2(uint32_t vdst) {
698 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
699 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
700 if (!type_l.IsCategory2Types()) {
701 verifier_->Fail(VERIFY_ERROR_GENERIC)
702 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
703 } else {
704 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
705 SetRegisterType(vdst, type_l); // also sets the high
706 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
707 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
708 }
709}
710
711void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
712 const RegType& dst_type, const RegType& src_type) {
713 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
714 SetRegisterType(dec_insn.vA_, dst_type);
715 }
716}
717
718void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
719 const RegType& dst_type,
720 const RegType& src_type1, const RegType& src_type2,
721 bool check_boolean_op) {
722 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
723 VerifyRegisterType(dec_insn.vC_, src_type2)) {
724 if (check_boolean_op) {
725 DCHECK(dst_type.IsInteger());
726 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
727 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
728 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
729 return;
730 }
731 }
732 SetRegisterType(dec_insn.vA_, dst_type);
733 }
734}
735
736void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
737 const RegType& dst_type, const RegType& src_type1,
738 const RegType& src_type2, bool check_boolean_op) {
739 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
740 VerifyRegisterType(dec_insn.vB_, src_type2)) {
741 if (check_boolean_op) {
742 DCHECK(dst_type.IsInteger());
743 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
744 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
745 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
746 return;
747 }
748 }
749 SetRegisterType(dec_insn.vA_, dst_type);
750 }
751}
752
753void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
754 const RegType& dst_type, const RegType& src_type,
755 bool check_boolean_op) {
756 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
757 if (check_boolean_op) {
758 DCHECK(dst_type.IsInteger());
759 /* check vB with the call, then check the constant manually */
760 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
761 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
762 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
763 return;
764 }
765 }
766 SetRegisterType(dec_insn.vA_, dst_type);
767 }
768}
769
770void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
771 const RegType& reg_type = GetRegisterType(reg_idx);
772 if (!reg_type.IsReferenceTypes()) {
773 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
774 } else {
775 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700776 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 }
778}
779
780void RegisterLine::PopMonitor(uint32_t reg_idx) {
781 const RegType& reg_type = GetRegisterType(reg_idx);
782 if (!reg_type.IsReferenceTypes()) {
783 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
784 } else if (monitors_.empty()) {
785 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
786 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700787 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
789 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
790 // format "036" the constant collector may create unlocks on the same object but referenced
791 // via different registers.
792 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
793 : verifier_->LogVerifyInfo())
794 << "monitor-exit not unlocking the top of the monitor stack";
795 } else {
796 // Record the register was unlocked
797 ClearRegToLockDepth(reg_idx, monitors_.size());
798 }
799 }
800}
801
802bool RegisterLine::VerifyMonitorStackEmpty() {
803 if (MonitorStackDepth() != 0) {
804 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
805 return false;
806 } else {
807 return true;
808 }
809}
810
811bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
812 bool changed = false;
813 for (size_t idx = 0; idx < num_regs_; idx++) {
814 if (line_[idx] != incoming_line->line_[idx]) {
815 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
816 const RegType& cur_type = GetRegisterType(idx);
817 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
818 changed = changed || !cur_type.Equals(new_type);
819 line_[idx] = new_type.GetId();
820 }
821 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700822 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
824 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
825 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
826 for (uint32_t idx = 0; idx < num_regs_; idx++) {
827 size_t depths = reg_to_lock_depths_.count(idx);
828 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
829 if (depths != incoming_depths) {
830 if (depths == 0 || incoming_depths == 0) {
831 reg_to_lock_depths_.erase(idx);
832 } else {
833 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
834 << ": " << depths << " != " << incoming_depths;
835 break;
836 }
837 }
838 }
839 }
840 return changed;
841}
842
843void RegisterLine::WriteReferenceBitMap(int8_t* data, size_t max_bytes) {
844 for (size_t i = 0; i < num_regs_; i += 8) {
845 uint8_t val = 0;
846 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
847 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700848 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 val |= 1 << j;
850 }
851 }
852 if (val != 0) {
853 DCHECK_LT(i / 8, max_bytes);
854 data[i / 8] = val;
855 }
856 }
857}
858
859std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700860 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 return os;
862}
863
864
865void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
866 uint32_t insns_size, uint16_t registers_size,
867 DexVerifier* verifier) {
868 DCHECK_GT(insns_size, 0U);
869
870 for (uint32_t i = 0; i < insns_size; i++) {
871 bool interesting = false;
872 switch (mode) {
873 case kTrackRegsAll:
874 interesting = flags[i].IsOpcode();
875 break;
876 case kTrackRegsGcPoints:
877 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
878 break;
879 case kTrackRegsBranches:
880 interesting = flags[i].IsBranchTarget();
881 break;
882 default:
883 break;
884 }
885 if (interesting) {
886 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
887 }
888 }
889}
890
891bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700892 if (klass->IsVerified()) {
893 return true;
894 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 Class* super = klass->GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 if (super == NULL && ClassHelper(klass).GetDescriptor() != "Ljava/lang/Object;") {
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
898 return false;
899 }
900 if (super != NULL) {
901 if (!super->IsVerified() && !super->IsErroneous()) {
902 Runtime::Current()->GetClassLinker()->VerifyClass(super);
903 }
904 if (!super->IsVerified()) {
905 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
906 << " that attempts to sub-class corrupt class " << PrettyClass(super);
907 return false;
908 } else if (super->IsFinal()) {
909 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
910 << " that attempts to sub-class final class " << PrettyClass(super);
911 return false;
912 }
913 }
jeffhaobdb76512011-09-07 11:43:16 -0700914 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
915 Method* method = klass->GetDirectMethod(i);
916 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
918 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700919 return false;
920 }
921 }
922 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
923 Method* method = klass->GetVirtualMethod(i);
924 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
926 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700927 return false;
928 }
929 }
930 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700931}
932
jeffhaobdb76512011-09-07 11:43:16 -0700933bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 DexVerifier verifier(method);
935 bool success = verifier.Verify();
936 // We expect either success and no verification error, or failure and a generic failure to
937 // reject the class.
938 if (success) {
939 if (verifier.failure_ != VERIFY_ERROR_NONE) {
940 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
941 << verifier.fail_messages_;
942 }
943 } else {
944 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700945 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700946 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700947 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700948 verifier.Dump(std::cout);
949 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
951 }
952 return success;
953}
954
Shih-wei Liao371814f2011-10-27 16:52:10 -0700955void DexVerifier::VerifyMethodAndDump(Method* method) {
956 DexVerifier verifier(method);
957 verifier.Verify();
958
Elliott Hughese0918552011-10-28 17:18:29 -0700959 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
960 << verifier.fail_messages_.str() << std::endl
961 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700962}
963
Ian Rogers28ad40d2011-10-27 15:19:26 -0700964DexVerifier::DexVerifier(Method* method) : work_insn_idx_(-1), method_(method),
965 failure_(VERIFY_ERROR_NONE),
966
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 new_instance_count_(0), monitor_enter_count_(0) {
jeffhaobdb76512011-09-07 11:43:16 -0700968 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
969 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 dex_file_ = &class_linker->FindDexFile(dex_cache);
971 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -0700972}
973
Ian Rogersd81871c2011-10-03 13:57:23 -0700974bool DexVerifier::Verify() {
975 // If there aren't any instructions, make sure that's expected, then exit successfully.
976 if (code_item_ == NULL) {
977 if (!method_->IsNative() && !method_->IsAbstract()) {
978 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700979 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 } else {
981 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700982 }
jeffhaobdb76512011-09-07 11:43:16 -0700983 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
985 if (code_item_->ins_size_ > code_item_->registers_size_) {
986 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
987 << " regs=" << code_item_->registers_size_;
988 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700989 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 // Allocate and initialize an array to hold instruction data.
991 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
992 // Run through the instructions and see if the width checks out.
993 bool result = ComputeWidthsAndCountOps();
994 // Flag instructions guarded by a "try" block and check exception handlers.
995 result = result && ScanTryCatchBlocks();
996 // Perform static instruction verification.
997 result = result && VerifyInstructions();
998 // Perform code flow analysis.
999 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001000 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001001}
1002
Ian Rogersd81871c2011-10-03 13:57:23 -07001003bool DexVerifier::ComputeWidthsAndCountOps() {
1004 const uint16_t* insns = code_item_->insns_;
1005 size_t insns_size = code_item_->insns_size_in_code_units_;
1006 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001007 size_t new_instance_count = 0;
1008 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001010
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001012 Instruction::Code opcode = inst->Opcode();
1013 if (opcode == Instruction::NEW_INSTANCE) {
1014 new_instance_count++;
1015 } else if (opcode == Instruction::MONITOR_ENTER) {
1016 monitor_enter_count++;
1017 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 size_t inst_size = inst->SizeInCodeUnits();
1019 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1020 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001021 inst = inst->Next();
1022 }
1023
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 if (dex_pc != insns_size) {
1025 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1026 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001027 return false;
1028 }
1029
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 new_instance_count_ = new_instance_count;
1031 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001032 return true;
1033}
1034
Ian Rogersd81871c2011-10-03 13:57:23 -07001035bool DexVerifier::ScanTryCatchBlocks() {
1036 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001037 if (tries_size == 0) {
1038 return true;
1039 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001041 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001042
1043 for (uint32_t idx = 0; idx < tries_size; idx++) {
1044 const DexFile::TryItem* try_item = &tries[idx];
1045 uint32_t start = try_item->start_addr_;
1046 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001047 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1049 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001050 return false;
1051 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 if (!insn_flags_[start].IsOpcode()) {
1053 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001054 return false;
1055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 for (uint32_t dex_pc = start; dex_pc < end;
1057 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1058 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001059 }
1060 }
jeffhaobdb76512011-09-07 11:43:16 -07001061 /* Iterate over each of the handlers to verify target addresses. */
Ian Rogers0571d352011-11-03 19:51:38 -07001062 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001063 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001064 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001065 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001066 CatchHandlerIterator iterator(handlers_ptr);
1067 for (; iterator.HasNext(); iterator.Next()) {
1068 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 if (!insn_flags_[dex_pc].IsOpcode()) {
1070 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001071 return false;
1072 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001074 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1075 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001076 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1077 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001078 if (exception_type == NULL) {
1079 DCHECK(Thread::Current()->IsExceptionPending());
1080 Thread::Current()->ClearException();
1081 }
1082 }
jeffhaobdb76512011-09-07 11:43:16 -07001083 }
Ian Rogers0571d352011-11-03 19:51:38 -07001084 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001085 }
jeffhaobdb76512011-09-07 11:43:16 -07001086 return true;
1087}
1088
Ian Rogersd81871c2011-10-03 13:57:23 -07001089bool DexVerifier::VerifyInstructions() {
1090 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001091
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 /* Flag the start of the method as a branch target. */
1093 insn_flags_[0].SetBranchTarget();
1094
1095 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1096 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1097 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001098 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1099 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return false;
1101 }
1102 /* Flag instructions that are garbage collection points */
1103 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1104 insn_flags_[dex_pc].SetGcPoint();
1105 }
1106 dex_pc += inst->SizeInCodeUnits();
1107 inst = inst->Next();
1108 }
1109 return true;
1110}
1111
1112bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1113 Instruction::DecodedInstruction dec_insn(inst);
1114 bool result = true;
1115 switch (inst->GetVerifyTypeArgumentA()) {
1116 case Instruction::kVerifyRegA:
1117 result = result && CheckRegisterIndex(dec_insn.vA_);
1118 break;
1119 case Instruction::kVerifyRegAWide:
1120 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1121 break;
1122 }
1123 switch (inst->GetVerifyTypeArgumentB()) {
1124 case Instruction::kVerifyRegB:
1125 result = result && CheckRegisterIndex(dec_insn.vB_);
1126 break;
1127 case Instruction::kVerifyRegBField:
1128 result = result && CheckFieldIndex(dec_insn.vB_);
1129 break;
1130 case Instruction::kVerifyRegBMethod:
1131 result = result && CheckMethodIndex(dec_insn.vB_);
1132 break;
1133 case Instruction::kVerifyRegBNewInstance:
1134 result = result && CheckNewInstance(dec_insn.vB_);
1135 break;
1136 case Instruction::kVerifyRegBString:
1137 result = result && CheckStringIndex(dec_insn.vB_);
1138 break;
1139 case Instruction::kVerifyRegBType:
1140 result = result && CheckTypeIndex(dec_insn.vB_);
1141 break;
1142 case Instruction::kVerifyRegBWide:
1143 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1144 break;
1145 }
1146 switch (inst->GetVerifyTypeArgumentC()) {
1147 case Instruction::kVerifyRegC:
1148 result = result && CheckRegisterIndex(dec_insn.vC_);
1149 break;
1150 case Instruction::kVerifyRegCField:
1151 result = result && CheckFieldIndex(dec_insn.vC_);
1152 break;
1153 case Instruction::kVerifyRegCNewArray:
1154 result = result && CheckNewArray(dec_insn.vC_);
1155 break;
1156 case Instruction::kVerifyRegCType:
1157 result = result && CheckTypeIndex(dec_insn.vC_);
1158 break;
1159 case Instruction::kVerifyRegCWide:
1160 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1161 break;
1162 }
1163 switch (inst->GetVerifyExtraFlags()) {
1164 case Instruction::kVerifyArrayData:
1165 result = result && CheckArrayData(code_offset);
1166 break;
1167 case Instruction::kVerifyBranchTarget:
1168 result = result && CheckBranchTarget(code_offset);
1169 break;
1170 case Instruction::kVerifySwitchTargets:
1171 result = result && CheckSwitchTargets(code_offset);
1172 break;
1173 case Instruction::kVerifyVarArg:
1174 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1175 break;
1176 case Instruction::kVerifyVarArgRange:
1177 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1178 break;
1179 case Instruction::kVerifyError:
1180 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1181 result = false;
1182 break;
1183 }
1184 return result;
1185}
1186
1187bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1188 if (idx >= code_item_->registers_size_) {
1189 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1190 << code_item_->registers_size_ << ")";
1191 return false;
1192 }
1193 return true;
1194}
1195
1196bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1197 if (idx + 1 >= code_item_->registers_size_) {
1198 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1199 << "+1 >= " << code_item_->registers_size_ << ")";
1200 return false;
1201 }
1202 return true;
1203}
1204
1205bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1206 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1207 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1208 << dex_file_->GetHeader().field_ids_size_ << ")";
1209 return false;
1210 }
1211 return true;
1212}
1213
1214bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1215 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1216 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1217 << dex_file_->GetHeader().method_ids_size_ << ")";
1218 return false;
1219 }
1220 return true;
1221}
1222
1223bool DexVerifier::CheckNewInstance(uint32_t idx) {
1224 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1225 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1226 << dex_file_->GetHeader().type_ids_size_ << ")";
1227 return false;
1228 }
1229 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001230 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 if (descriptor[0] != 'L') {
1232 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1233 return false;
1234 }
1235 return true;
1236}
1237
1238bool DexVerifier::CheckStringIndex(uint32_t idx) {
1239 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1240 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1241 << dex_file_->GetHeader().string_ids_size_ << ")";
1242 return false;
1243 }
1244 return true;
1245}
1246
1247bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1248 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1249 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1250 << dex_file_->GetHeader().type_ids_size_ << ")";
1251 return false;
1252 }
1253 return true;
1254}
1255
1256bool DexVerifier::CheckNewArray(uint32_t idx) {
1257 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1258 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1259 << dex_file_->GetHeader().type_ids_size_ << ")";
1260 return false;
1261 }
1262 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001263 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 const char* cp = descriptor;
1265 while (*cp++ == '[') {
1266 bracket_count++;
1267 }
1268 if (bracket_count == 0) {
1269 /* The given class must be an array type. */
1270 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1271 return false;
1272 } else if (bracket_count > 255) {
1273 /* It is illegal to create an array of more than 255 dimensions. */
1274 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1275 return false;
1276 }
1277 return true;
1278}
1279
1280bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1281 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1282 const uint16_t* insns = code_item_->insns_ + cur_offset;
1283 const uint16_t* array_data;
1284 int32_t array_data_offset;
1285
1286 DCHECK_LT(cur_offset, insn_count);
1287 /* make sure the start of the array data table is in range */
1288 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1289 if ((int32_t) cur_offset + array_data_offset < 0 ||
1290 cur_offset + array_data_offset + 2 >= insn_count) {
1291 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1292 << ", data offset " << array_data_offset << ", count " << insn_count;
1293 return false;
1294 }
1295 /* offset to array data table is a relative branch-style offset */
1296 array_data = insns + array_data_offset;
1297 /* make sure the table is 32-bit aligned */
1298 if ((((uint32_t) array_data) & 0x03) != 0) {
1299 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1300 << ", data offset " << array_data_offset;
1301 return false;
1302 }
1303 uint32_t value_width = array_data[1];
1304 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1305 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1306 /* make sure the end of the switch is in range */
1307 if (cur_offset + array_data_offset + table_size > insn_count) {
1308 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1309 << ", data offset " << array_data_offset << ", end "
1310 << cur_offset + array_data_offset + table_size
1311 << ", count " << insn_count;
1312 return false;
1313 }
1314 return true;
1315}
1316
1317bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1318 int32_t offset;
1319 bool isConditional, selfOkay;
1320 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1321 return false;
1322 }
1323 if (!selfOkay && offset == 0) {
1324 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1325 return false;
1326 }
1327 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1328 // identical "wrap-around" behavior, but it's unwise to depend on that.
1329 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1330 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1331 return false;
1332 }
1333 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1334 int32_t abs_offset = cur_offset + offset;
1335 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1336 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1337 << (void*) abs_offset << ") at " << (void*) cur_offset;
1338 return false;
1339 }
1340 insn_flags_[abs_offset].SetBranchTarget();
1341 return true;
1342}
1343
1344bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1345 bool* selfOkay) {
1346 const uint16_t* insns = code_item_->insns_ + cur_offset;
1347 *pConditional = false;
1348 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001349 switch (*insns & 0xff) {
1350 case Instruction::GOTO:
1351 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 break;
1353 case Instruction::GOTO_32:
1354 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 *selfOkay = true;
1356 break;
1357 case Instruction::GOTO_16:
1358 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 break;
1360 case Instruction::IF_EQ:
1361 case Instruction::IF_NE:
1362 case Instruction::IF_LT:
1363 case Instruction::IF_GE:
1364 case Instruction::IF_GT:
1365 case Instruction::IF_LE:
1366 case Instruction::IF_EQZ:
1367 case Instruction::IF_NEZ:
1368 case Instruction::IF_LTZ:
1369 case Instruction::IF_GEZ:
1370 case Instruction::IF_GTZ:
1371 case Instruction::IF_LEZ:
1372 *pOffset = (int16_t) insns[1];
1373 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 break;
1375 default:
1376 return false;
1377 break;
1378 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001379 return true;
1380}
1381
Ian Rogersd81871c2011-10-03 13:57:23 -07001382bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1383 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001384 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001386 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1388 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1389 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1390 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 return false;
1392 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 /* make sure the table is 32-bit aligned */
1396 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1398 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 return false;
1400 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 uint32_t switch_count = switch_insns[1];
1402 int32_t keys_offset, targets_offset;
1403 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001404 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1405 /* 0=sig, 1=count, 2/3=firstKey */
1406 targets_offset = 4;
1407 keys_offset = -1;
1408 expected_signature = Instruction::kPackedSwitchSignature;
1409 } else {
1410 /* 0=sig, 1=count, 2..count*2 = keys */
1411 keys_offset = 2;
1412 targets_offset = 2 + 2 * switch_count;
1413 expected_signature = Instruction::kSparseSwitchSignature;
1414 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 if (switch_insns[0] != expected_signature) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 Fail(VERIFY_ERROR_GENERIC) << "wrong signature for switch table (" << (void*) switch_insns[0]
1418 << ", wanted " << (void*) expected_signature << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001419 return false;
1420 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 /* make sure the end of the switch is in range */
1422 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1424 << switch_offset << ", end "
1425 << (cur_offset + switch_offset + table_size)
1426 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001427 return false;
1428 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001429 /* for a sparse switch, verify the keys are in ascending order */
1430 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1432 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1434 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1435 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1437 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 return false;
1439 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001440 last_key = key;
1441 }
1442 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 for (uint32_t targ = 0; targ < switch_count; targ++) {
1445 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1446 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1447 int32_t abs_offset = cur_offset + offset;
1448 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1449 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1450 << (void*) abs_offset << ") at "
1451 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 return false;
1453 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 insn_flags_[abs_offset].SetBranchTarget();
1455 }
1456 return true;
1457}
1458
1459bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1460 if (vA > 5) {
1461 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1462 return false;
1463 }
1464 uint16_t registers_size = code_item_->registers_size_;
1465 for (uint32_t idx = 0; idx < vA; idx++) {
1466 if (arg[idx] > registers_size) {
1467 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1468 << ") in non-range invoke (> " << registers_size << ")";
1469 return false;
1470 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 }
1472
1473 return true;
1474}
1475
Ian Rogersd81871c2011-10-03 13:57:23 -07001476bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1477 uint16_t registers_size = code_item_->registers_size_;
1478 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1479 // integer overflow when adding them here.
1480 if (vA + vC > registers_size) {
1481 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1482 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001483 return false;
1484 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 return true;
1486}
1487
Ian Rogersd81871c2011-10-03 13:57:23 -07001488bool DexVerifier::VerifyCodeFlow() {
1489 uint16_t registers_size = code_item_->registers_size_;
1490 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001491
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 if (registers_size * insns_size > 4*1024*1024) {
1493 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1494 << " insns_size=" << insns_size << ")";
1495 }
1496 /* Create and initialize table holding register status */
1497 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1498 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001499
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 work_line_.reset(new RegisterLine(registers_size, this));
1501 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001502
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 /* Initialize register types of method arguments. */
1504 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001505 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1506 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 return false;
1508 }
1509 /* Perform code flow verification. */
1510 if (!CodeFlowVerifyMethod()) {
1511 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001512 }
1513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 /* Generate a register map and add it to the method. */
1515 ByteArray* map = GenerateGcMap();
1516 if (map == NULL) {
1517 return false; // Not a real failure, but a failure to encode
1518 }
1519 method_->SetGcMap(map);
1520#ifndef NDEBUG
1521 VerifyGcMap();
1522#endif
jeffhaobdb76512011-09-07 11:43:16 -07001523 return true;
1524}
1525
Ian Rogersd81871c2011-10-03 13:57:23 -07001526void DexVerifier::Dump(std::ostream& os) {
1527 if (method_->IsNative()) {
1528 os << "Native method" << std::endl;
1529 return;
jeffhaobdb76512011-09-07 11:43:16 -07001530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 DCHECK(code_item_ != NULL);
1532 const Instruction* inst = Instruction::At(code_item_->insns_);
1533 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1534 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001535 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1536 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1538 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001539 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001541 inst = inst->Next();
1542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543}
1544
Ian Rogersd81871c2011-10-03 13:57:23 -07001545static bool IsPrimitiveDescriptor(char descriptor) {
1546 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001547 case 'I':
1548 case 'C':
1549 case 'S':
1550 case 'B':
1551 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001552 case 'F':
1553 case 'D':
1554 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001556 default:
1557 return false;
1558 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001559}
1560
Ian Rogersd81871c2011-10-03 13:57:23 -07001561bool DexVerifier::SetTypesFromSignature() {
1562 RegisterLine* reg_line = reg_table_.GetLine(0);
1563 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1564 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001565
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1567 //Include the "this" pointer.
1568 size_t cur_arg = 0;
1569 if (!method_->IsStatic()) {
1570 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1571 // argument as uninitialized. This restricts field access until the superclass constructor is
1572 // called.
1573 Class* declaring_class = method_->GetDeclaringClass();
1574 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1575 reg_line->SetRegisterType(arg_start + cur_arg,
1576 reg_types_.UninitializedThisArgument(declaring_class));
1577 } else {
1578 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
1582
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001583 const DexFile::ProtoId& proto_id =
1584 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001585 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586
1587 for (; iterator.HasNext(); iterator.Next()) {
1588 const char* descriptor = iterator.GetDescriptor();
1589 if (descriptor == NULL) {
1590 LOG(FATAL) << "Null descriptor";
1591 }
1592 if (cur_arg >= expected_args) {
1593 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1594 << " args, found more (" << descriptor << ")";
1595 return false;
1596 }
1597 switch (descriptor[0]) {
1598 case 'L':
1599 case '[':
1600 // We assume that reference arguments are initialized. The only way it could be otherwise
1601 // (assuming the caller was verified) is if the current method is <init>, but in that case
1602 // it's effectively considered initialized the instant we reach here (in the sense that we
1603 // can return without doing anything or call virtual methods).
1604 {
1605 const RegType& reg_type =
1606 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001607 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 }
1609 break;
1610 case 'Z':
1611 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1612 break;
1613 case 'C':
1614 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1615 break;
1616 case 'B':
1617 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1618 break;
1619 case 'I':
1620 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1621 break;
1622 case 'S':
1623 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1624 break;
1625 case 'F':
1626 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1627 break;
1628 case 'J':
1629 case 'D': {
1630 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1631 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1632 cur_arg++;
1633 break;
1634 }
1635 default:
1636 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1637 return false;
1638 }
1639 cur_arg++;
1640 }
1641 if (cur_arg != expected_args) {
1642 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1643 return false;
1644 }
1645 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1646 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1647 // format. Only major difference from the method argument format is that 'V' is supported.
1648 bool result;
1649 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1650 result = descriptor[1] == '\0';
1651 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1652 size_t i = 0;
1653 do {
1654 i++;
1655 } while (descriptor[i] == '['); // process leading [
1656 if (descriptor[i] == 'L') { // object array
1657 do {
1658 i++; // find closing ;
1659 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1660 result = descriptor[i] == ';';
1661 } else { // primitive array
1662 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1663 }
1664 } else if (descriptor[0] == 'L') {
1665 // could be more thorough here, but shouldn't be required
1666 size_t i = 0;
1667 do {
1668 i++;
1669 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1670 result = descriptor[i] == ';';
1671 } else {
1672 result = false;
1673 }
1674 if (!result) {
1675 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1676 << descriptor << "'";
1677 }
1678 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001679}
1680
Ian Rogersd81871c2011-10-03 13:57:23 -07001681bool DexVerifier::CodeFlowVerifyMethod() {
1682 const uint16_t* insns = code_item_->insns_;
1683 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001684
jeffhaobdb76512011-09-07 11:43:16 -07001685 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 insn_flags_[0].SetChanged();
1687 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001688
jeffhaobdb76512011-09-07 11:43:16 -07001689 /* Continue until no instructions are marked "changed". */
1690 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1692 uint32_t insn_idx = start_guess;
1693 for (; insn_idx < insns_size; insn_idx++) {
1694 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
1696 }
jeffhaobdb76512011-09-07 11:43:16 -07001697 if (insn_idx == insns_size) {
1698 if (start_guess != 0) {
1699 /* try again, starting from the top */
1700 start_guess = 0;
1701 continue;
1702 } else {
1703 /* all flags are clear */
1704 break;
1705 }
1706 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 // We carry the working set of registers from instruction to instruction. If this address can
1708 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1709 // "changed" flags, we need to load the set of registers from the table.
1710 // Because we always prefer to continue on to the next instruction, we should never have a
1711 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1712 // target.
1713 work_insn_idx_ = insn_idx;
1714 if (insn_flags_[insn_idx].IsBranchTarget()) {
1715 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001716 } else {
1717#ifndef NDEBUG
1718 /*
1719 * Sanity check: retrieve the stored register line (assuming
1720 * a full table) and make sure it actually matches.
1721 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1723 if (register_line != NULL) {
1724 if (work_line_->CompareLine(register_line) != 0) {
1725 Dump(std::cout);
1726 std::cout << info_messages_.str();
1727 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1728 << "@" << (void*)work_insn_idx_ << std::endl
1729 << " work_line=" << *work_line_ << std::endl
1730 << " expected=" << *register_line;
1731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
1733#endif
1734 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 if (!CodeFlowVerifyInstruction(&start_guess)) {
1736 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001737 return false;
1738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 insn_flags_[insn_idx].SetVisited();
1741 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001742 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001743
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001745 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001746 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001747 * (besides the wasted space), but it indicates a flaw somewhere
1748 * down the line, possibly in the verifier.
1749 *
1750 * If we've substituted "always throw" instructions into the stream,
1751 * we are almost certainly going to have some dead code.
1752 */
1753 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 uint32_t insn_idx = 0;
1755 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001756 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001757 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001758 * may or may not be preceded by a padding NOP (for alignment).
1759 */
1760 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1761 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1762 insns[insn_idx] == Instruction::kArrayDataSignature ||
1763 (insns[insn_idx] == Instruction::NOP &&
1764 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1765 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1766 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001768 }
1769
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001771 if (dead_start < 0)
1772 dead_start = insn_idx;
1773 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001775 dead_start = -1;
1776 }
1777 }
1778 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001780 }
1781 }
jeffhaobdb76512011-09-07 11:43:16 -07001782 return true;
1783}
1784
Ian Rogersd81871c2011-10-03 13:57:23 -07001785bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001786#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001788 gDvm.verifierStats.instrsReexamined++;
1789 } else {
1790 gDvm.verifierStats.instrsExamined++;
1791 }
1792#endif
1793
1794 /*
1795 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001796 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001797 * control to another statement:
1798 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001799 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001800 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001801 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001802 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001803 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001804 * throw an exception that is handled by an encompassing "try"
1805 * block.
1806 *
1807 * We can also return, in which case there is no successor instruction
1808 * from this point.
1809 *
1810 * The behavior can be determined from the OpcodeFlags.
1811 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1813 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001814 Instruction::DecodedInstruction dec_insn(inst);
1815 int opcode_flag = inst->Flag();
1816
jeffhaobdb76512011-09-07 11:43:16 -07001817 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001818 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001819 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001821 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1822 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 }
jeffhaobdb76512011-09-07 11:43:16 -07001824
1825 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001826 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001827 * can throw an exception, we will copy/merge this into the "catch"
1828 * address rather than work_line, because we don't want the result
1829 * from the "successful" code path (e.g. a check-cast that "improves"
1830 * a type) to be visible to the exception handler.
1831 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1833 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001834 } else {
1835#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001837#endif
1838 }
1839
1840 switch (dec_insn.opcode_) {
1841 case Instruction::NOP:
1842 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001843 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001844 * a signature that looks like a NOP; if we see one of these in
1845 * the course of executing code then we have a problem.
1846 */
1847 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001849 }
1850 break;
1851
1852 case Instruction::MOVE:
1853 case Instruction::MOVE_FROM16:
1854 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857 case Instruction::MOVE_WIDE:
1858 case Instruction::MOVE_WIDE_FROM16:
1859 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
1862 case Instruction::MOVE_OBJECT:
1863 case Instruction::MOVE_OBJECT_FROM16:
1864 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867
1868 /*
1869 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001870 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001871 * might want to hold the result in an actual CPU register, so the
1872 * Dalvik spec requires that these only appear immediately after an
1873 * invoke or filled-new-array.
1874 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001875 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001876 * redundant with the reset done below, but it can make the debug info
1877 * easier to read in some cases.)
1878 */
1879 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001884 break;
1885 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
1888
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001890 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 * This statement can only appear as the first instruction in an exception handler (though not
1892 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001893 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001894 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001895 const RegType& res_type = GetCaughtExceptionType();
1896 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 }
jeffhaobdb76512011-09-07 11:43:16 -07001899 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1901 if (!GetMethodReturnType().IsUnknown()) {
1902 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1903 }
jeffhaobdb76512011-09-07 11:43:16 -07001904 }
1905 break;
1906 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001908 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 const RegType& return_type = GetMethodReturnType();
1910 if (!return_type.IsCategory1Types()) {
1911 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1912 } else {
1913 // Compilers may generate synthetic functions that write byte values into boolean fields.
1914 // Also, it may use integer values for boolean, byte, short, and character return types.
1915 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1916 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1917 ((return_type.IsBoolean() || return_type.IsByte() ||
1918 return_type.IsShort() || return_type.IsChar()) &&
1919 src_type.IsInteger()));
1920 /* check the register contents */
1921 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1922 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001923 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 }
jeffhaobdb76512011-09-07 11:43:16 -07001925 }
1926 }
1927 break;
1928 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001930 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 const RegType& return_type = GetMethodReturnType();
1932 if (!return_type.IsCategory2Types()) {
1933 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1934 } else {
1935 /* check the register contents */
1936 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1937 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001938 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
1941 }
1942 break;
1943 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1945 const RegType& return_type = GetMethodReturnType();
1946 if (!return_type.IsReferenceTypes()) {
1947 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1948 } else {
1949 /* return_type is the *expected* return type, not register value */
1950 DCHECK(!return_type.IsZero());
1951 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07001952 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
1953 // Disallow returning uninitialized values and verify that the reference in vAA is an
1954 // instance of the "return_type"
1955 if (reg_type.IsUninitializedTypes()) {
1956 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
1957 } else if (!return_type.IsAssignableFrom(reg_type)) {
1958 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
1959 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001960 }
1961 }
1962 }
1963 break;
1964
1965 case Instruction::CONST_4:
1966 case Instruction::CONST_16:
1967 case Instruction::CONST:
1968 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07001970 break;
1971 case Instruction::CONST_HIGH16:
1972 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 work_line_->SetRegisterType(dec_insn.vA_,
1974 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
1976 case Instruction::CONST_WIDE_16:
1977 case Instruction::CONST_WIDE_32:
1978 case Instruction::CONST_WIDE:
1979 case Instruction::CONST_WIDE_HIGH16:
1980 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
1983 case Instruction::CONST_STRING:
1984 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001986 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001988 // Get type from instruction if unresolved then we need an access check
1989 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1990 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
1991 // Register holds class, ie its type is class, but on error we keep it Unknown
1992 work_line_->SetRegisterType(dec_insn.vA_,
1993 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 }
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
1999 case Instruction::MONITOR_EXIT:
2000 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002001 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002002 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002003 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002004 * to the need to handle asynchronous exceptions, a now-deprecated
2005 * feature that Dalvik doesn't support.)
2006 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002007 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002008 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002009 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002010 * structured locking checks are working, the former would have
2011 * failed on the -enter instruction, and the latter is impossible.
2012 *
2013 * This is fortunate, because issue 3221411 prevents us from
2014 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002015 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002016 * some catch blocks (which will show up as "dead" code when
2017 * we skip them here); if we can't, then the code path could be
2018 * "live" so we still need to check it.
2019 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 opcode_flag &= ~Instruction::kThrow;
2021 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023
Ian Rogers28ad40d2011-10-27 15:19:26 -07002024 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002026 /*
2027 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2028 * could be a "upcast" -- not expected, so we don't try to address it.)
2029 *
2030 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2031 * dec_insn.vA_ when branching to a handler.
2032 */
2033 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2034 const RegType& res_type =
2035 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
2036 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2037 const RegType& orig_type =
2038 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2039 if (!res_type.IsNonZeroReferenceTypes()) {
2040 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2041 } else if (!orig_type.IsReferenceTypes()) {
2042 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002043 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002044 if (is_checkcast) {
2045 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002048 }
jeffhaobdb76512011-09-07 11:43:16 -07002049 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002050 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 }
2052 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002053 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2054 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002055 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002056 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 } else {
2058 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2059 }
2060 }
2061 break;
2062 }
2063 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002064 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2065 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2066 // can't create an instance of an interface or abstract class */
2067 if (!res_type.IsInstantiableTypes()) {
2068 Fail(VERIFY_ERROR_INSTANTIATION)
2069 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002071 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2072 // Any registers holding previous allocations from this address that have not yet been
2073 // initialized must be marked invalid.
2074 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2075 // add the new uninitialized reference to the register state
2076 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 }
2078 break;
2079 }
2080 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002081 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2082 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2083 if (!res_type.IsArrayClass()) {
2084 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002085 } else {
2086 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002088 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002089 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002090 }
2091 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002095 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2096 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2097 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002099 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002100 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002101 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002102 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002103 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002104 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002105 just_set_result = true;
2106 }
2107 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 }
jeffhaobdb76512011-09-07 11:43:16 -07002109 case Instruction::CMPL_FLOAT:
2110 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2112 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2113 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::CMPL_DOUBLE:
2116 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2118 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2119 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2123 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2124 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002127 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2128 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2129 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002130 }
2131 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 }
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::GOTO:
2134 case Instruction::GOTO_16:
2135 case Instruction::GOTO_32:
2136 /* no effect on or use of registers */
2137 break;
2138
2139 case Instruction::PACKED_SWITCH:
2140 case Instruction::SPARSE_SWITCH:
2141 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 case Instruction::FILL_ARRAY_DATA: {
2146 /* Similar to the verification done for APUT */
2147 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2148 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002149 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 if (res_class != NULL) {
2151 Class* component_type = res_class->GetComponentType();
2152 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2153 component_type->IsPrimitiveVoid()) {
2154 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002155 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 } else {
2157 const RegType& value_type = reg_types_.FromClass(component_type);
2158 DCHECK(!value_type.IsUnknown());
2159 // Now verify if the element width in the table matches the element width declared in
2160 // the array
2161 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2162 if (array_data[0] != Instruction::kArrayDataSignature) {
2163 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2164 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002165 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 // Since we don't compress the data in Dex, expect to see equal width of data stored
2167 // in the table and expected from the array class.
2168 if (array_data[1] != elem_width) {
2169 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2170 << " vs " << elem_width << ")";
2171 }
2172 }
2173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 }
2175 }
2176 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 }
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 case Instruction::IF_NE: {
2180 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2181 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2182 bool mismatch = false;
2183 if (reg_type1.IsZero()) { // zero then integral or reference expected
2184 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2185 } else if (reg_type1.IsReferenceTypes()) { // both references?
2186 mismatch = !reg_type2.IsReferenceTypes();
2187 } else { // both integral?
2188 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2189 }
2190 if (mismatch) {
2191 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2192 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002193 }
2194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::IF_LT:
2197 case Instruction::IF_GE:
2198 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 case Instruction::IF_LE: {
2200 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2201 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2202 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2203 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2204 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002205 }
2206 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 }
jeffhaobdb76512011-09-07 11:43:16 -07002208 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 case Instruction::IF_NEZ: {
2210 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2211 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2212 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2213 }
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::IF_LTZ:
2217 case Instruction::IF_GEZ:
2218 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 case Instruction::IF_LEZ: {
2220 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2221 if (!reg_type.IsIntegralTypes()) {
2222 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2223 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2224 }
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 }
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2232 break;
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 VerifyAGet(dec_insn, reg_types_.Char(), true);
2235 break;
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 case Instruction::AGET:
2240 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2241 break;
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 VerifyAGet(dec_insn, reg_types_.Long(), true);
2244 break;
2245 case Instruction::AGET_OBJECT:
2246 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 case Instruction::APUT_BOOLEAN:
2250 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2251 break;
2252 case Instruction::APUT_BYTE:
2253 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2254 break;
2255 case Instruction::APUT_CHAR:
2256 VerifyAPut(dec_insn, reg_types_.Char(), true);
2257 break;
2258 case Instruction::APUT_SHORT:
2259 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270
jeffhaobdb76512011-09-07 11:43:16 -07002271 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002272 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002275 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002278 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
jeffhaobdb76512011-09-07 11:43:16 -07002280 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002281 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 break;
2283 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002284 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002287 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002290 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
jeffhaobdb76512011-09-07 11:43:16 -07002292
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
2296 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002297 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
2299 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002300 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
2302 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002303 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002306 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002309 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002312 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314
jeffhaobdb76512011-09-07 11:43:16 -07002315 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002319 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 break;
jeffhaobdb76512011-09-07 11:43:16 -07002321 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002322 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 break;
jeffhaobdb76512011-09-07 11:43:16 -07002324 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002325 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
2327 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002328 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002331 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002334 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 break;
2336
2337 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002338 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
2340 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002341 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002344 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
2346 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002347 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002350 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002353 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002356 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358
2359 case Instruction::INVOKE_VIRTUAL:
2360 case Instruction::INVOKE_VIRTUAL_RANGE:
2361 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 case Instruction::INVOKE_SUPER_RANGE: {
2363 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2364 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2365 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2366 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2367 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2368 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002369 const char* descriptor;
2370 if (called_method == NULL) {
2371 uint32_t method_idx = dec_insn.vB_;
2372 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2373 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002374 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002375 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002376 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002377 }
Ian Rogers9074b992011-10-26 17:41:55 -07002378 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002379 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002381 just_set_result = true;
2382 }
2383 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 }
jeffhaobdb76512011-09-07 11:43:16 -07002385 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 case Instruction::INVOKE_DIRECT_RANGE: {
2387 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2388 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2389 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002390 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 * Some additional checks when calling a constructor. We know from the invocation arg check
2392 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2393 * that to require that called_method->klass is the same as this->klass or this->super,
2394 * allowing the latter only if the "this" argument is the same as the "this" argument to
2395 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002396 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002397 bool is_constructor;
2398 if (called_method != NULL) {
2399 is_constructor = called_method->IsConstructor();
2400 } else {
2401 uint32_t method_idx = dec_insn.vB_;
2402 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2403 const char* name = dex_file_->GetMethodName(method_id);
2404 is_constructor = strcmp(name, "<init>") == 0;
2405 }
2406 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2408 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410
2411 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 if (this_type.IsZero()) {
2413 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 if (called_method != NULL) {
2417 Class* this_class = this_type.GetClass();
2418 DCHECK(this_class != NULL);
2419 /* must be in same class or in superclass */
2420 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2421 if (this_class != method_->GetDeclaringClass()) {
2422 Fail(VERIFY_ERROR_GENERIC)
2423 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2424 break;
2425 }
2426 } else if (called_method->GetDeclaringClass() != this_class) {
2427 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 }
jeffhaobdb76512011-09-07 11:43:16 -07002430 }
2431
2432 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002433 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2435 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 }
2438
2439 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002440 * Replace the uninitialized reference with an initialized one. We need to do this for all
2441 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002442 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 work_line_->MarkRefsAsInitialized(this_type);
2444 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002446 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002447 const char* descriptor;
2448 if (called_method == NULL) {
2449 uint32_t method_idx = dec_insn.vB_;
2450 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2451 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002452 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002453 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002454 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002455 }
Ian Rogers9074b992011-10-26 17:41:55 -07002456 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002457 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002459 just_set_result = true;
2460 }
2461 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 }
jeffhaobdb76512011-09-07 11:43:16 -07002463 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 case Instruction::INVOKE_STATIC_RANGE: {
2465 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2466 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2467 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002468 const char* descriptor;
2469 if (called_method == NULL) {
2470 uint32_t method_idx = dec_insn.vB_;
2471 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2472 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002473 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002475 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 }
Ian Rogers9074b992011-10-26 17:41:55 -07002477 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 work_line_->SetResultRegisterType(return_type);
2480 just_set_result = true;
2481 }
jeffhaobdb76512011-09-07 11:43:16 -07002482 }
2483 break;
2484 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 case Instruction::INVOKE_INTERFACE_RANGE: {
2486 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2487 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2488 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 if (abs_method != NULL) {
2490 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002491 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002492 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2493 << PrettyMethod(abs_method) << "'";
2494 break;
2495 }
2496 }
2497 /* Get the type of the "this" arg, which should either be a sub-interface of called
2498 * interface or Object (see comments in RegType::JoinClass).
2499 */
2500 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2501 if (failure_ == VERIFY_ERROR_NONE) {
2502 if (this_type.IsZero()) {
2503 /* null pointer always passes (and always fails at runtime) */
2504 } else {
2505 if (this_type.IsUninitializedTypes()) {
2506 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2507 << this_type;
2508 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002509 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002510 // In the past we have tried to assert that "called_interface" is assignable
2511 // from "this_type.GetClass()", however, as we do an imprecise Join
2512 // (RegType::JoinClass) we don't have full information on what interfaces are
2513 // implemented by "this_type". For example, two classes may implement the same
2514 // interfaces and have a common parent that doesn't implement the interface. The
2515 // join will set "this_type" to the parent class and a test that this implements
2516 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002517 }
2518 }
jeffhaobdb76512011-09-07 11:43:16 -07002519 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 * We don't have an object instance, so we can't find the concrete method. However, all of
2521 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002522 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002523 const char* descriptor;
2524 if (abs_method == NULL) {
2525 uint32_t method_idx = dec_insn.vB_;
2526 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2527 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002528 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002529 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002530 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002531 }
Ian Rogers9074b992011-10-26 17:41:55 -07002532 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002533 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2534 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002536 just_set_result = true;
2537 }
2538 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 }
jeffhaobdb76512011-09-07 11:43:16 -07002540 case Instruction::NEG_INT:
2541 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::NEG_LONG:
2545 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
2572 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
2587 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599
2600 case Instruction::ADD_INT:
2601 case Instruction::SUB_INT:
2602 case Instruction::MUL_INT:
2603 case Instruction::REM_INT:
2604 case Instruction::DIV_INT:
2605 case Instruction::SHL_INT:
2606 case Instruction::SHR_INT:
2607 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::AND_INT:
2611 case Instruction::OR_INT:
2612 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::ADD_LONG:
2616 case Instruction::SUB_LONG:
2617 case Instruction::MUL_LONG:
2618 case Instruction::DIV_LONG:
2619 case Instruction::REM_LONG:
2620 case Instruction::AND_LONG:
2621 case Instruction::OR_LONG:
2622 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::SHL_LONG:
2626 case Instruction::SHR_LONG:
2627 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 /* shift distance is Int, making these different from other binary operations */
2629 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::ADD_FLOAT:
2632 case Instruction::SUB_FLOAT:
2633 case Instruction::MUL_FLOAT:
2634 case Instruction::DIV_FLOAT:
2635 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::ADD_DOUBLE:
2639 case Instruction::SUB_DOUBLE:
2640 case Instruction::MUL_DOUBLE:
2641 case Instruction::DIV_DOUBLE:
2642 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::ADD_INT_2ADDR:
2646 case Instruction::SUB_INT_2ADDR:
2647 case Instruction::MUL_INT_2ADDR:
2648 case Instruction::REM_INT_2ADDR:
2649 case Instruction::SHL_INT_2ADDR:
2650 case Instruction::SHR_INT_2ADDR:
2651 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::AND_INT_2ADDR:
2655 case Instruction::OR_INT_2ADDR:
2656 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::ADD_LONG_2ADDR:
2663 case Instruction::SUB_LONG_2ADDR:
2664 case Instruction::MUL_LONG_2ADDR:
2665 case Instruction::DIV_LONG_2ADDR:
2666 case Instruction::REM_LONG_2ADDR:
2667 case Instruction::AND_LONG_2ADDR:
2668 case Instruction::OR_LONG_2ADDR:
2669 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::SHL_LONG_2ADDR:
2673 case Instruction::SHR_LONG_2ADDR:
2674 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::ADD_FLOAT_2ADDR:
2678 case Instruction::SUB_FLOAT_2ADDR:
2679 case Instruction::MUL_FLOAT_2ADDR:
2680 case Instruction::DIV_FLOAT_2ADDR:
2681 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::ADD_DOUBLE_2ADDR:
2685 case Instruction::SUB_DOUBLE_2ADDR:
2686 case Instruction::MUL_DOUBLE_2ADDR:
2687 case Instruction::DIV_DOUBLE_2ADDR:
2688 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::ADD_INT_LIT16:
2692 case Instruction::RSUB_INT:
2693 case Instruction::MUL_INT_LIT16:
2694 case Instruction::DIV_INT_LIT16:
2695 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::AND_INT_LIT16:
2699 case Instruction::OR_INT_LIT16:
2700 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::ADD_INT_LIT8:
2704 case Instruction::RSUB_INT_LIT8:
2705 case Instruction::MUL_INT_LIT8:
2706 case Instruction::DIV_INT_LIT8:
2707 case Instruction::REM_INT_LIT8:
2708 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002710 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::AND_INT_LIT8:
2714 case Instruction::OR_INT_LIT8:
2715 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002717 break;
2718
2719 /*
2720 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002721 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002722 * inserted in the course of verification, we can expect to see it here.
2723 */
jeffhaob4df5142011-09-19 20:25:32 -07002724 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002728 case Instruction::UNUSED_EE:
2729 case Instruction::UNUSED_EF:
2730 case Instruction::UNUSED_F2:
2731 case Instruction::UNUSED_F3:
2732 case Instruction::UNUSED_F4:
2733 case Instruction::UNUSED_F5:
2734 case Instruction::UNUSED_F6:
2735 case Instruction::UNUSED_F7:
2736 case Instruction::UNUSED_F8:
2737 case Instruction::UNUSED_F9:
2738 case Instruction::UNUSED_FA:
2739 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002740 case Instruction::UNUSED_F0:
2741 case Instruction::UNUSED_F1:
2742 case Instruction::UNUSED_E3:
2743 case Instruction::UNUSED_E8:
2744 case Instruction::UNUSED_E7:
2745 case Instruction::UNUSED_E4:
2746 case Instruction::UNUSED_E9:
2747 case Instruction::UNUSED_FC:
2748 case Instruction::UNUSED_E5:
2749 case Instruction::UNUSED_EA:
2750 case Instruction::UNUSED_FD:
2751 case Instruction::UNUSED_E6:
2752 case Instruction::UNUSED_EB:
2753 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002754 case Instruction::UNUSED_3E:
2755 case Instruction::UNUSED_3F:
2756 case Instruction::UNUSED_40:
2757 case Instruction::UNUSED_41:
2758 case Instruction::UNUSED_42:
2759 case Instruction::UNUSED_43:
2760 case Instruction::UNUSED_73:
2761 case Instruction::UNUSED_79:
2762 case Instruction::UNUSED_7A:
2763 case Instruction::UNUSED_EC:
2764 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002765 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767
2768 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002769 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002770 * complain if an instruction is missing (which is desirable).
2771 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002773
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 if (failure_ != VERIFY_ERROR_NONE) {
2775 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002776 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002777 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002778 return false;
2779 } else {
2780 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002781 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002783 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002785 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002787 opcode_flag = Instruction::kThrow;
2788 }
2789 }
jeffhaobdb76512011-09-07 11:43:16 -07002790 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 * If we didn't just set the result register, clear it out. This ensures that you can only use
2792 * "move-result" immediately after the result is set. (We could check this statically, but it's
2793 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002794 */
2795 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002797 }
2798
jeffhaoa0a764a2011-09-16 10:43:38 -07002799 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002800 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2802 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2803 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002804 return false;
2805 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2807 // next instruction isn't one.
2808 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002809 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 }
2811 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2812 if (next_line != NULL) {
2813 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2814 // needed.
2815 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002816 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
jeffhaobdb76512011-09-07 11:43:16 -07002818 } else {
2819 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 * We're not recording register data for the next instruction, so we don't know what the prior
2821 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002822 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002824 }
2825 }
2826
2827 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002828 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002829 *
2830 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002831 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002832 * somebody could get a reference field, check it for zero, and if the
2833 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002834 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002835 * that, and will reject the code.
2836 *
2837 * TODO: avoid re-fetching the branch target
2838 */
2839 if ((opcode_flag & Instruction::kBranch) != 0) {
2840 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002842 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002844 return false;
2845 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002846 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002848 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 }
jeffhaobdb76512011-09-07 11:43:16 -07002850 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002852 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 }
jeffhaobdb76512011-09-07 11:43:16 -07002854 }
2855
2856 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002857 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002858 *
2859 * We've already verified that the table is structurally sound, so we
2860 * just need to walk through and tag the targets.
2861 */
2862 if ((opcode_flag & Instruction::kSwitch) != 0) {
2863 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2864 const uint16_t* switch_insns = insns + offset_to_switch;
2865 int switch_count = switch_insns[1];
2866 int offset_to_targets, targ;
2867
2868 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2869 /* 0 = sig, 1 = count, 2/3 = first key */
2870 offset_to_targets = 4;
2871 } else {
2872 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002873 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002874 offset_to_targets = 2 + 2 * switch_count;
2875 }
2876
2877 /* verify each switch target */
2878 for (targ = 0; targ < switch_count; targ++) {
2879 int offset;
2880 uint32_t abs_offset;
2881
2882 /* offsets are 32-bit, and only partly endian-swapped */
2883 offset = switch_insns[offset_to_targets + targ * 2] |
2884 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 abs_offset = work_insn_idx_ + offset;
2886 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2887 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002888 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 }
2890 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002891 return false;
2892 }
2893 }
2894
2895 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2897 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002898 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2900 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002901 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002902
Ian Rogers0571d352011-11-03 19:51:38 -07002903 for (; iterator.HasNext(); iterator.Next()) {
2904 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 within_catch_all = true;
2906 }
jeffhaobdb76512011-09-07 11:43:16 -07002907 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2909 * "work_regs", because at runtime the exception will be thrown before the instruction
2910 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002911 */
Ian Rogers0571d352011-11-03 19:51:38 -07002912 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002913 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 }
jeffhaobdb76512011-09-07 11:43:16 -07002915 }
2916
2917 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2919 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002920 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002922 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 * The state in work_line reflects the post-execution state. If the current instruction is a
2924 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002925 * it will do so before grabbing the lock).
2926 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2928 Fail(VERIFY_ERROR_GENERIC)
2929 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002930 return false;
2931 }
2932 }
2933 }
2934
jeffhaod1f0fde2011-09-08 17:25:33 -07002935 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 if ((opcode_flag & Instruction::kReturn) != 0) {
2937 if(!work_line_->VerifyMonitorStackEmpty()) {
2938 return false;
2939 }
jeffhaobdb76512011-09-07 11:43:16 -07002940 }
2941
2942 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002943 * Update start_guess. Advance to the next instruction of that's
2944 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002945 * neither of those exists we're in a return or throw; leave start_guess
2946 * alone and let the caller sort it out.
2947 */
2948 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002950 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2951 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002953 }
2954
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2956 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002957
2958 return true;
2959}
2960
Ian Rogers28ad40d2011-10-27 15:19:26 -07002961const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002962 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002963 Class* referrer = method_->GetDeclaringClass();
2964 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2965 const RegType& result =
2966 klass != NULL ? reg_types_.FromClass(klass)
2967 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2968 if (klass == NULL && !result.IsUnresolvedTypes()) {
2969 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002971 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2972 // check at runtime if access is allowed and so pass here.
2973 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2974 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002975 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002976 << result << "'";
2977 return reg_types_.Unknown();
2978 } else {
2979 return result;
2980 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002981}
2982
Ian Rogers28ad40d2011-10-27 15:19:26 -07002983const RegType& DexVerifier::GetCaughtExceptionType() {
2984 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002986 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2988 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002989 CatchHandlerIterator iterator(handlers_ptr);
2990 for (; iterator.HasNext(); iterator.Next()) {
2991 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2992 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002993 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002995 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
2997 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
2998 * test, so is essentially harmless.
2999 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003000 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3001 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3002 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003004 common_super = &exception;
3005 } else if (common_super->Equals(exception)) {
3006 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003008 common_super = &common_super->Merge(exception, &reg_types_);
3009 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 }
3011 }
3012 }
3013 }
Ian Rogers0571d352011-11-03 19:51:38 -07003014 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 }
3016 }
3017 if (common_super == NULL) {
3018 /* no catch blocks, or no catches with classes we can find */
3019 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3020 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003021 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022}
3023
3024Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
3025 Class* referrer = method_->GetDeclaringClass();
3026 DexCache* dex_cache = referrer->GetDexCache();
3027 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3028 if (res_method == NULL) {
3029 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003030 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3031 if(klass_type.IsUnresolvedTypes()) {
3032 return NULL; // Can't resolve Class so no more to do here
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003034 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003035 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003036 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 if (is_direct) {
3038 res_method = klass->FindDirectMethod(name, signature);
3039 } else if (klass->IsInterface()) {
3040 res_method = klass->FindInterfaceMethod(name, signature);
3041 } else {
3042 res_method = klass->FindVirtualMethod(name, signature);
3043 }
3044 if (res_method != NULL) {
3045 dex_cache->SetResolvedMethod(method_idx, res_method);
3046 } else {
3047 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003048 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 << " " << signature;
3050 return NULL;
3051 }
3052 }
3053 /* Check if access is allowed. */
3054 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3055 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003056 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 return NULL;
3058 }
3059 return res_method;
3060}
3061
3062Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3063 MethodType method_type, bool is_range, bool is_super) {
3064 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3065 // we're making.
3066 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3067 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003068 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 return NULL;
3070 }
3071 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3072 // enforce them here.
3073 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3074 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3075 << PrettyMethod(res_method);
3076 return NULL;
3077 }
3078 // See if the method type implied by the invoke instruction matches the access flags for the
3079 // target method.
3080 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3081 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3082 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3083 ) {
3084 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3085 << PrettyMethod(res_method);
3086 return NULL;
3087 }
3088 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3089 // has a vtable entry for the target method.
3090 if (is_super) {
3091 DCHECK(method_type == METHOD_VIRTUAL);
3092 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3093 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3094 if (super == NULL) { // Only Object has no super class
3095 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3096 << " to super " << PrettyMethod(res_method);
3097 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003098 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003100 << " to super " << PrettyDescriptor(super)
3101 << "." << mh.GetName()
3102 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
3104 return NULL;
3105 }
3106 }
3107 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3108 // match the call to the signature. Also, we might might be calling through an abstract method
3109 // definition (which doesn't have register count values).
3110 int expected_args = dec_insn.vA_;
3111 /* caught by static verifier */
3112 DCHECK(is_range || expected_args <= 5);
3113 if (expected_args > code_item_->outs_size_) {
3114 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3115 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3116 return NULL;
3117 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003118 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 if (sig[0] != '(') {
3120 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3121 << " as descriptor doesn't start with '(': " << sig;
3122 return NULL;
3123 }
jeffhaobdb76512011-09-07 11:43:16 -07003124 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 * Check the "this" argument, which must be an instance of the class
3126 * that declared the method. For an interface class, we don't do the
3127 * full interface merge, so we can't do a rigorous check here (which
3128 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003129 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 int actual_args = 0;
3131 if (!res_method->IsStatic()) {
3132 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3133 if (failure_ != VERIFY_ERROR_NONE) {
3134 return NULL;
3135 }
3136 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3137 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3138 return NULL;
3139 }
3140 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003141 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3142 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3143 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3144 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 return NULL;
3146 }
3147 }
3148 actual_args++;
3149 }
3150 /*
3151 * Process the target method's signature. This signature may or may not
3152 * have been verified, so we can't assume it's properly formed.
3153 */
3154 size_t sig_offset = 0;
3155 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3156 if (actual_args >= expected_args) {
3157 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3158 << "'. Expected " << expected_args << " args, found more ("
3159 << sig.substr(sig_offset) << ")";
3160 return NULL;
3161 }
3162 std::string descriptor;
3163 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3164 size_t end;
3165 if (sig[sig_offset] == 'L') {
3166 end = sig.find(';', sig_offset);
3167 } else {
3168 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3169 if (sig[end] == 'L') {
3170 end = sig.find(';', end);
3171 }
3172 }
3173 if (end == std::string::npos) {
3174 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3175 << "bad signature component '" << sig << "' (missing ';')";
3176 return NULL;
3177 }
3178 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3179 sig_offset = end;
3180 } else {
3181 descriptor = sig[sig_offset];
3182 }
3183 const RegType& reg_type =
3184 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003185 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3186 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3187 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 }
3189 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3190 }
3191 if (sig[sig_offset] != ')') {
3192 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3193 return NULL;
3194 }
3195 if (actual_args != expected_args) {
3196 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3197 << " expected " << expected_args << " args, found " << actual_args;
3198 return NULL;
3199 } else {
3200 return res_method;
3201 }
3202}
3203
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003204const RegType& DexVerifier::GetMethodReturnType() {
3205 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3206 MethodHelper(method_).GetReturnTypeDescriptor());
3207}
3208
Ian Rogersd81871c2011-10-03 13:57:23 -07003209void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3210 const RegType& insn_type, bool is_primitive) {
3211 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3212 if (!index_type.IsArrayIndexTypes()) {
3213 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3214 } else {
3215 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3216 if (failure_ == VERIFY_ERROR_NONE) {
3217 if (array_class == NULL) {
3218 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3219 // instruction type. TODO: have a proper notion of bottom here.
3220 if (!is_primitive || insn_type.IsCategory1Types()) {
3221 // Reference or category 1
3222 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3223 } else {
3224 // Category 2
3225 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3226 }
3227 } else {
3228 /* verify the class */
3229 Class* component_class = array_class->GetComponentType();
3230 const RegType& component_type = reg_types_.FromClass(component_class);
3231 if (!array_class->IsArrayClass()) {
3232 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003233 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003234 } else if (component_class->IsPrimitive() && !is_primitive) {
3235 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003236 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 << " source for aget-object";
3238 } else if (!component_class->IsPrimitive() && is_primitive) {
3239 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003240 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 << " source for category 1 aget";
3242 } else if (is_primitive && !insn_type.Equals(component_type) &&
3243 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3244 (insn_type.IsLong() && component_type.IsDouble()))) {
3245 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003246 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 << " incompatible with aget of type " << insn_type;
3248 } else {
3249 // Use knowledge of the field type which is stronger than the type inferred from the
3250 // instruction, which can't differentiate object types and ints from floats, longs from
3251 // doubles.
3252 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3253 }
3254 }
3255 }
3256 }
3257}
3258
3259void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3260 const RegType& insn_type, bool is_primitive) {
3261 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3262 if (!index_type.IsArrayIndexTypes()) {
3263 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3264 } else {
3265 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3266 if (failure_ == VERIFY_ERROR_NONE) {
3267 if (array_class == NULL) {
3268 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3269 // instruction type.
3270 } else {
3271 /* verify the class */
3272 Class* component_class = array_class->GetComponentType();
3273 const RegType& component_type = reg_types_.FromClass(component_class);
3274 if (!array_class->IsArrayClass()) {
3275 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003276 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 } else if (component_class->IsPrimitive() && !is_primitive) {
3278 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003279 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 << " source for aput-object";
3281 } else if (!component_class->IsPrimitive() && is_primitive) {
3282 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003283 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 << " source for category 1 aput";
3285 } else if (is_primitive && !insn_type.Equals(component_type) &&
3286 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3287 (insn_type.IsLong() && component_type.IsDouble()))) {
3288 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003289 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 << " incompatible with aput of type " << insn_type;
3291 } else {
3292 // The instruction agrees with the type of array, confirm the value to be stored does too
3293 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3294 }
3295 }
3296 }
3297 }
3298}
3299
3300Field* DexVerifier::GetStaticField(int field_idx) {
3301 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3302 if (field == NULL) {
3303 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003304 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3305 << dex_file_->GetFieldName(field_id) << ") in "
3306 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 DCHECK(Thread::Current()->IsExceptionPending());
3308 Thread::Current()->ClearException();
3309 return NULL;
3310 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3311 field->GetAccessFlags())) {
3312 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3313 << " from " << PrettyClass(method_->GetDeclaringClass());
3314 return NULL;
3315 } else if (!field->IsStatic()) {
3316 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3317 return NULL;
3318 } else {
3319 return field;
3320 }
3321}
3322
Ian Rogersd81871c2011-10-03 13:57:23 -07003323Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3324 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3325 if (field == NULL) {
3326 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003327 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3328 << dex_file_->GetFieldName(field_id) << ") in "
3329 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 DCHECK(Thread::Current()->IsExceptionPending());
3331 Thread::Current()->ClearException();
3332 return NULL;
3333 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3334 field->GetAccessFlags())) {
3335 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3336 << " from " << PrettyClass(method_->GetDeclaringClass());
3337 return NULL;
3338 } else if (field->IsStatic()) {
3339 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3340 << " to not be static";
3341 return NULL;
3342 } else if (obj_type.IsZero()) {
3343 // Cannot infer and check type, however, access will cause null pointer exception
3344 return field;
3345 } else if(obj_type.IsUninitializedReference() &&
3346 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3347 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3348 // Field accesses through uninitialized references are only allowable for constructors where
3349 // the field is declared in this class
3350 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3351 << " of a not fully initialized object within the context of "
3352 << PrettyMethod(method_);
3353 return NULL;
3354 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3355 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3356 // of C1. For resolution to occur the declared class of the field must be compatible with
3357 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3358 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3359 << " from object of type " << PrettyClass(obj_type.GetClass());
3360 return NULL;
3361 } else {
3362 return field;
3363 }
3364}
3365
Ian Rogersb94a27b2011-10-26 00:33:41 -07003366void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3367 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003368 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003369 Field* field;
3370 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003371 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003372 } else {
3373 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003374 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003375 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003376 if (failure_ != VERIFY_ERROR_NONE) {
3377 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3378 } else {
3379 const char* descriptor;
3380 const ClassLoader* loader;
3381 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003382 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003383 loader = field->GetDeclaringClass()->GetClassLoader();
3384 } else {
3385 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3386 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3387 loader = method_->GetDeclaringClass()->GetClassLoader();
3388 }
3389 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003391 if (field_type.Equals(insn_type) ||
3392 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3393 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 // expected that read is of the correct primitive type or that int reads are reading
3395 // floats or long reads are reading doubles
3396 } else {
3397 // This is a global failure rather than a class change failure as the instructions and
3398 // the descriptors for the type should have been consistent within the same file at
3399 // compile time
3400 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003401 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003402 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 return;
3404 }
3405 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003406 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003408 << " to be compatible with type '" << insn_type
3409 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003410 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 return;
3412 }
3413 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003414 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 }
3416}
3417
Ian Rogersb94a27b2011-10-26 00:33:41 -07003418void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3419 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003420 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003421 Field* field;
3422 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003423 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003424 } else {
3425 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003426 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003427 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003428 if (failure_ != VERIFY_ERROR_NONE) {
3429 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3430 } else {
3431 const char* descriptor;
3432 const ClassLoader* loader;
3433 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003434 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003435 loader = field->GetDeclaringClass()->GetClassLoader();
3436 } else {
3437 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3438 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3439 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003440 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003441 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3442 if (field != NULL) {
3443 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3444 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3445 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3446 return;
3447 }
3448 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003449 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003450 // Primitive field assignability rules are weaker than regular assignability rules
3451 bool instruction_compatible;
3452 bool value_compatible;
3453 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3454 if (field_type.IsIntegralTypes()) {
3455 instruction_compatible = insn_type.IsIntegralTypes();
3456 value_compatible = value_type.IsIntegralTypes();
3457 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003458 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003459 value_compatible = value_type.IsFloatTypes();
3460 } else if (field_type.IsLong()) {
3461 instruction_compatible = insn_type.IsLong();
3462 value_compatible = value_type.IsLongTypes();
3463 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003464 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003465 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003467 instruction_compatible = false; // reference field with primitive store
3468 value_compatible = false; // unused
3469 }
3470 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003471 // This is a global failure rather than a class change failure as the instructions and
3472 // the descriptors for the type should have been consistent within the same file at
3473 // compile time
3474 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003475 << " to be of type '" << insn_type
3476 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003477 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 return;
3479 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003480 if (!value_compatible) {
3481 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3482 << " of type " << value_type
3483 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003484 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003485 return;
3486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003488 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003490 << " to be compatible with type '" << insn_type
3491 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003492 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003493 return;
3494 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003495 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 }
3498}
3499
3500bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3501 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3502 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3503 return false;
3504 }
3505 return true;
3506}
3507
3508void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003509 const RegType& res_type, bool is_range) {
3510 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 /*
3512 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3513 * list and fail. It's legal, if silly, for arg_count to be zero.
3514 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003515 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3516 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003517 uint32_t arg_count = dec_insn.vA_;
3518 for (size_t ui = 0; ui < arg_count; ui++) {
3519 uint32_t get_reg;
3520
3521 if (is_range)
3522 get_reg = dec_insn.vC_ + ui;
3523 else
3524 get_reg = dec_insn.arg_[ui];
3525
3526 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3527 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3528 << ") not valid";
3529 return;
3530 }
3531 }
3532}
3533
3534void DexVerifier::ReplaceFailingInstruction() {
3535 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3536 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3537 VerifyErrorRefType ref_type;
3538 switch (inst->Opcode()) {
3539 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003540 case Instruction::CHECK_CAST:
3541 case Instruction::INSTANCE_OF:
3542 case Instruction::NEW_INSTANCE:
3543 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003545 case Instruction::FILLED_NEW_ARRAY_RANGE:
3546 ref_type = VERIFY_ERROR_REF_CLASS;
3547 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003549 case Instruction::IGET_BOOLEAN:
3550 case Instruction::IGET_BYTE:
3551 case Instruction::IGET_CHAR:
3552 case Instruction::IGET_SHORT:
3553 case Instruction::IGET_WIDE:
3554 case Instruction::IGET_OBJECT:
3555 case Instruction::IPUT:
3556 case Instruction::IPUT_BOOLEAN:
3557 case Instruction::IPUT_BYTE:
3558 case Instruction::IPUT_CHAR:
3559 case Instruction::IPUT_SHORT:
3560 case Instruction::IPUT_WIDE:
3561 case Instruction::IPUT_OBJECT:
3562 case Instruction::SGET:
3563 case Instruction::SGET_BOOLEAN:
3564 case Instruction::SGET_BYTE:
3565 case Instruction::SGET_CHAR:
3566 case Instruction::SGET_SHORT:
3567 case Instruction::SGET_WIDE:
3568 case Instruction::SGET_OBJECT:
3569 case Instruction::SPUT:
3570 case Instruction::SPUT_BOOLEAN:
3571 case Instruction::SPUT_BYTE:
3572 case Instruction::SPUT_CHAR:
3573 case Instruction::SPUT_SHORT:
3574 case Instruction::SPUT_WIDE:
3575 case Instruction::SPUT_OBJECT:
3576 ref_type = VERIFY_ERROR_REF_FIELD;
3577 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003579 case Instruction::INVOKE_VIRTUAL_RANGE:
3580 case Instruction::INVOKE_SUPER:
3581 case Instruction::INVOKE_SUPER_RANGE:
3582 case Instruction::INVOKE_DIRECT:
3583 case Instruction::INVOKE_DIRECT_RANGE:
3584 case Instruction::INVOKE_STATIC:
3585 case Instruction::INVOKE_STATIC_RANGE:
3586 case Instruction::INVOKE_INTERFACE:
3587 case Instruction::INVOKE_INTERFACE_RANGE:
3588 ref_type = VERIFY_ERROR_REF_METHOD;
3589 break;
jeffhaobdb76512011-09-07 11:43:16 -07003590 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003591 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003592 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003593 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3595 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3596 // instruction, so assert it.
3597 size_t width = inst->SizeInCodeUnits();
3598 CHECK_GT(width, 1u);
3599 // If the instruction is larger than 2 code units, rewrite subqeuent code unit sized chunks with
3600 // NOPs
3601 for (size_t i = 2; i < width; i++) {
3602 insns[work_insn_idx_ + i] = Instruction::NOP;
3603 }
3604 // Encode the opcode, with the failure code in the high byte
3605 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3606 (failure_ << 8) | // AA - component
3607 (ref_type << (8 + kVerifyErrorRefTypeShift));
3608 insns[work_insn_idx_] = new_instruction;
3609 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3610 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003611 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3612 << fail_messages_.str();
3613 if (gDebugVerify) {
3614 std::cout << std::endl << info_messages_.str();
3615 Dump(std::cout);
3616 }
jeffhaobdb76512011-09-07 11:43:16 -07003617}
jeffhaoba5ebb92011-08-25 17:24:37 -07003618
Ian Rogersd81871c2011-10-03 13:57:23 -07003619bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3620 const bool merge_debug = true;
3621 bool changed = true;
3622 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3623 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003624 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 * We haven't processed this instruction before, and we haven't touched the registers here, so
3626 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3627 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003628 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003629 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003630 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3632 copy->CopyFromLine(target_line);
3633 changed = target_line->MergeRegisters(merge_line);
3634 if (failure_ != VERIFY_ERROR_NONE) {
3635 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003636 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003637 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3639 << *copy.get() << " MERGE" << std::endl
3640 << *merge_line << " ==" << std::endl
3641 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003642 }
3643 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 if (changed) {
3645 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003646 }
3647 return true;
3648}
3649
Ian Rogersd81871c2011-10-03 13:57:23 -07003650void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3651 size_t* log2_max_gc_pc) {
3652 size_t local_gc_points = 0;
3653 size_t max_insn = 0;
3654 size_t max_ref_reg = -1;
3655 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3656 if (insn_flags_[i].IsGcPoint()) {
3657 local_gc_points++;
3658 max_insn = i;
3659 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003660 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003661 }
3662 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 *gc_points = local_gc_points;
3664 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3665 size_t i = 0;
3666 while ((1U << i) < max_insn) {
3667 i++;
3668 }
3669 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003670}
3671
Ian Rogersd81871c2011-10-03 13:57:23 -07003672ByteArray* DexVerifier::GenerateGcMap() {
3673 size_t num_entries, ref_bitmap_bits, pc_bits;
3674 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3675 // There's a single byte to encode the size of each bitmap
3676 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3677 // TODO: either a better GC map format or per method failures
3678 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3679 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003680 return NULL;
3681 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3683 // There are 2 bytes to encode the number of entries
3684 if (num_entries >= 65536) {
3685 // TODO: either a better GC map format or per method failures
3686 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3687 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003688 return NULL;
3689 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003690 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003691 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003693 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 pc_bytes = 1;
3695 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003696 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003698 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 // TODO: either a better GC map format or per method failures
3700 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3701 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3702 return NULL;
3703 }
3704 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3705 ByteArray* table = ByteArray::Alloc(table_size);
3706 if (table == NULL) {
3707 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3708 return NULL;
3709 }
3710 // Write table header
3711 table->Set(0, format);
3712 table->Set(1, ref_bitmap_bytes);
3713 table->Set(2, num_entries & 0xFF);
3714 table->Set(3, (num_entries >> 8) & 0xFF);
3715 // Write table data
3716 size_t offset = 4;
3717 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3718 if (insn_flags_[i].IsGcPoint()) {
3719 table->Set(offset, i & 0xFF);
3720 offset++;
3721 if (pc_bytes == 2) {
3722 table->Set(offset, (i >> 8) & 0xFF);
3723 offset++;
3724 }
3725 RegisterLine* line = reg_table_.GetLine(i);
3726 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3727 offset += ref_bitmap_bytes;
3728 }
3729 }
3730 DCHECK(offset == table_size);
3731 return table;
3732}
jeffhaoa0a764a2011-09-16 10:43:38 -07003733
Ian Rogersd81871c2011-10-03 13:57:23 -07003734void DexVerifier::VerifyGcMap() {
3735 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3736 // that the table data is well formed and all references are marked (or not) in the bitmap
3737 PcToReferenceMap map(method_);
3738 size_t map_index = 0;
3739 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3740 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3741 if (insn_flags_[i].IsGcPoint()) {
3742 CHECK_LT(map_index, map.NumEntries());
3743 CHECK_EQ(map.GetPC(map_index), i);
3744 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3745 map_index++;
3746 RegisterLine* line = reg_table_.GetLine(i);
3747 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003748 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003749 CHECK_LT(j / 8, map.RegWidth());
3750 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3751 } else if ((j / 8) < map.RegWidth()) {
3752 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3753 } else {
3754 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3755 }
3756 }
3757 } else {
3758 CHECK(reg_bitmap == NULL);
3759 }
3760 }
3761}
jeffhaoa0a764a2011-09-16 10:43:38 -07003762
Ian Rogersd81871c2011-10-03 13:57:23 -07003763const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3764 size_t num_entries = NumEntries();
3765 // Do linear or binary search?
3766 static const size_t kSearchThreshold = 8;
3767 if (num_entries < kSearchThreshold) {
3768 for (size_t i = 0; i < num_entries; i++) {
3769 if (GetPC(i) == dex_pc) {
3770 return GetBitMap(i);
3771 }
3772 }
3773 } else {
3774 int lo = 0;
3775 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003776 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 int mid = (hi + lo) / 2;
3778 int mid_pc = GetPC(mid);
3779 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003780 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003782 hi = mid - 1;
3783 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003785 }
3786 }
3787 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 if (error_if_not_present) {
3789 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3790 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003791 return NULL;
3792}
3793
Ian Rogersd81871c2011-10-03 13:57:23 -07003794} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003795} // namespace art