blob: 90940679d1a325b67d3e61c2cf56ca40aab6715f [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_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002036 if (res_type.IsUnknown()) {
2037 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2038 break; // couldn't resolve class
2039 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002040 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2041 const RegType& orig_type =
2042 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2043 if (!res_type.IsNonZeroReferenceTypes()) {
2044 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2045 } else if (!orig_type.IsReferenceTypes()) {
2046 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002047 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002048 if (is_checkcast) {
2049 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002052 }
jeffhaobdb76512011-09-07 11:43:16 -07002053 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002054 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 }
2056 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002057 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2058 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002059 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002060 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 } else {
2062 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2063 }
2064 }
2065 break;
2066 }
2067 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002068 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2069 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2070 // can't create an instance of an interface or abstract class */
2071 if (!res_type.IsInstantiableTypes()) {
2072 Fail(VERIFY_ERROR_INSTANTIATION)
2073 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002075 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2076 // Any registers holding previous allocations from this address that have not yet been
2077 // initialized must be marked invalid.
2078 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2079 // add the new uninitialized reference to the register state
2080 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 }
2082 break;
2083 }
2084 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002085 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2086 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2087 if (!res_type.IsArrayClass()) {
2088 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002089 } else {
2090 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002092 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002093 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002094 }
2095 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002099 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2100 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2101 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002103 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002104 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002105 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002106 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002107 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002108 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002109 just_set_result = true;
2110 }
2111 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 }
jeffhaobdb76512011-09-07 11:43:16 -07002113 case Instruction::CMPL_FLOAT:
2114 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2116 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2117 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
2119 case Instruction::CMPL_DOUBLE:
2120 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2122 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2123 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2127 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2128 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002131 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2132 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2133 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002134 }
2135 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 }
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::GOTO:
2138 case Instruction::GOTO_16:
2139 case Instruction::GOTO_32:
2140 /* no effect on or use of registers */
2141 break;
2142
2143 case Instruction::PACKED_SWITCH:
2144 case Instruction::SPARSE_SWITCH:
2145 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 case Instruction::FILL_ARRAY_DATA: {
2150 /* Similar to the verification done for APUT */
2151 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2152 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002153 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 if (res_class != NULL) {
2155 Class* component_type = res_class->GetComponentType();
2156 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2157 component_type->IsPrimitiveVoid()) {
2158 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002159 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 } else {
2161 const RegType& value_type = reg_types_.FromClass(component_type);
2162 DCHECK(!value_type.IsUnknown());
2163 // Now verify if the element width in the table matches the element width declared in
2164 // the array
2165 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2166 if (array_data[0] != Instruction::kArrayDataSignature) {
2167 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2168 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002169 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 // Since we don't compress the data in Dex, expect to see equal width of data stored
2171 // in the table and expected from the array class.
2172 if (array_data[1] != elem_width) {
2173 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2174 << " vs " << elem_width << ")";
2175 }
2176 }
2177 }
jeffhaobdb76512011-09-07 11:43:16 -07002178 }
2179 }
2180 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 }
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 case Instruction::IF_NE: {
2184 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2185 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2186 bool mismatch = false;
2187 if (reg_type1.IsZero()) { // zero then integral or reference expected
2188 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2189 } else if (reg_type1.IsReferenceTypes()) { // both references?
2190 mismatch = !reg_type2.IsReferenceTypes();
2191 } else { // both integral?
2192 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2193 }
2194 if (mismatch) {
2195 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2196 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002197 }
2198 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 case Instruction::IF_LT:
2201 case Instruction::IF_GE:
2202 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 case Instruction::IF_LE: {
2204 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2205 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2206 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2207 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2208 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002209 }
2210 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 }
jeffhaobdb76512011-09-07 11:43:16 -07002212 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 case Instruction::IF_NEZ: {
2214 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2215 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2216 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2217 }
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 }
jeffhaobdb76512011-09-07 11:43:16 -07002220 case Instruction::IF_LTZ:
2221 case Instruction::IF_GEZ:
2222 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 case Instruction::IF_LEZ: {
2224 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2225 if (!reg_type.IsIntegralTypes()) {
2226 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2227 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2228 }
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2233 break;
jeffhaobdb76512011-09-07 11:43:16 -07002234 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2236 break;
jeffhaobdb76512011-09-07 11:43:16 -07002237 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 VerifyAGet(dec_insn, reg_types_.Char(), true);
2239 break;
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002242 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 case Instruction::AGET:
2244 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2245 break;
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 VerifyAGet(dec_insn, reg_types_.Long(), true);
2248 break;
2249 case Instruction::AGET_OBJECT:
2250 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 case Instruction::APUT_BOOLEAN:
2254 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2255 break;
2256 case Instruction::APUT_BYTE:
2257 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2258 break;
2259 case Instruction::APUT_CHAR:
2260 VerifyAPut(dec_insn, reg_types_.Char(), true);
2261 break;
2262 case Instruction::APUT_SHORT:
2263 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
2274
jeffhaobdb76512011-09-07 11:43:16 -07002275 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002276 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 break;
jeffhaobdb76512011-09-07 11:43:16 -07002278 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002279 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 break;
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002282 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002285 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
2287 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002288 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002291 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002294 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002298 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
2300 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002301 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 break;
2303 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002304 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
2306 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002307 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002310 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002313 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
jeffhaobdb76512011-09-07 11:43:16 -07002315 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002316 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002320 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 break;
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002323 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002326 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 break;
jeffhaobdb76512011-09-07 11:43:16 -07002328 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002329 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 break;
2331 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002332 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002335 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002338 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
2340
2341 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002342 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 break;
2344 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002345 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
2347 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002348 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 break;
2350 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002351 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002354 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002357 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002360 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
2363 case Instruction::INVOKE_VIRTUAL:
2364 case Instruction::INVOKE_VIRTUAL_RANGE:
2365 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 case Instruction::INVOKE_SUPER_RANGE: {
2367 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2368 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2369 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2370 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2371 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2372 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002373 const char* descriptor;
2374 if (called_method == NULL) {
2375 uint32_t method_idx = dec_insn.vB_;
2376 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2377 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002378 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002379 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002380 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002381 }
Ian Rogers9074b992011-10-26 17:41:55 -07002382 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002383 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002385 just_set_result = true;
2386 }
2387 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::INVOKE_DIRECT_RANGE: {
2391 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2392 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2393 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002394 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 * Some additional checks when calling a constructor. We know from the invocation arg check
2396 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2397 * that to require that called_method->klass is the same as this->klass or this->super,
2398 * allowing the latter only if the "this" argument is the same as the "this" argument to
2399 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002400 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002401 bool is_constructor;
2402 if (called_method != NULL) {
2403 is_constructor = called_method->IsConstructor();
2404 } else {
2405 uint32_t method_idx = dec_insn.vB_;
2406 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2407 const char* name = dex_file_->GetMethodName(method_id);
2408 is_constructor = strcmp(name, "<init>") == 0;
2409 }
2410 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2412 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414
2415 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 if (this_type.IsZero()) {
2417 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002420 if (called_method != NULL) {
2421 Class* this_class = this_type.GetClass();
2422 DCHECK(this_class != NULL);
2423 /* must be in same class or in superclass */
2424 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2425 if (this_class != method_->GetDeclaringClass()) {
2426 Fail(VERIFY_ERROR_GENERIC)
2427 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2428 break;
2429 }
2430 } else if (called_method->GetDeclaringClass() != this_class) {
2431 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 }
jeffhaobdb76512011-09-07 11:43:16 -07002434 }
2435
2436 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002437 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2439 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 }
2442
2443 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002444 * Replace the uninitialized reference with an initialized one. We need to do this for all
2445 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002446 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 work_line_->MarkRefsAsInitialized(this_type);
2448 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002450 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002451 const char* descriptor;
2452 if (called_method == NULL) {
2453 uint32_t method_idx = dec_insn.vB_;
2454 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2455 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002456 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002457 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002458 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002459 }
Ian Rogers9074b992011-10-26 17:41:55 -07002460 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002461 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002463 just_set_result = true;
2464 }
2465 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 }
jeffhaobdb76512011-09-07 11:43:16 -07002467 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 case Instruction::INVOKE_STATIC_RANGE: {
2469 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2470 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2471 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 const char* descriptor;
2473 if (called_method == NULL) {
2474 uint32_t method_idx = dec_insn.vB_;
2475 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2476 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002477 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002479 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 }
Ian Rogers9074b992011-10-26 17:41:55 -07002481 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002482 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 work_line_->SetResultRegisterType(return_type);
2484 just_set_result = true;
2485 }
jeffhaobdb76512011-09-07 11:43:16 -07002486 }
2487 break;
2488 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 case Instruction::INVOKE_INTERFACE_RANGE: {
2490 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2491 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2492 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002493 if (abs_method != NULL) {
2494 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002495 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002496 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2497 << PrettyMethod(abs_method) << "'";
2498 break;
2499 }
2500 }
2501 /* Get the type of the "this" arg, which should either be a sub-interface of called
2502 * interface or Object (see comments in RegType::JoinClass).
2503 */
2504 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2505 if (failure_ == VERIFY_ERROR_NONE) {
2506 if (this_type.IsZero()) {
2507 /* null pointer always passes (and always fails at runtime) */
2508 } else {
2509 if (this_type.IsUninitializedTypes()) {
2510 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2511 << this_type;
2512 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002514 // In the past we have tried to assert that "called_interface" is assignable
2515 // from "this_type.GetClass()", however, as we do an imprecise Join
2516 // (RegType::JoinClass) we don't have full information on what interfaces are
2517 // implemented by "this_type". For example, two classes may implement the same
2518 // interfaces and have a common parent that doesn't implement the interface. The
2519 // join will set "this_type" to the parent class and a test that this implements
2520 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002521 }
2522 }
jeffhaobdb76512011-09-07 11:43:16 -07002523 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 * We don't have an object instance, so we can't find the concrete method. However, all of
2525 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002526 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002527 const char* descriptor;
2528 if (abs_method == NULL) {
2529 uint32_t method_idx = dec_insn.vB_;
2530 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2531 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002532 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002533 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002534 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002535 }
Ian Rogers9074b992011-10-26 17:41:55 -07002536 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002537 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2538 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002540 just_set_result = true;
2541 }
2542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::NEG_INT:
2545 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::NEG_LONG:
2549 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002554 break;
2555 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603
2604 case Instruction::ADD_INT:
2605 case Instruction::SUB_INT:
2606 case Instruction::MUL_INT:
2607 case Instruction::REM_INT:
2608 case Instruction::DIV_INT:
2609 case Instruction::SHL_INT:
2610 case Instruction::SHR_INT:
2611 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::AND_INT:
2615 case Instruction::OR_INT:
2616 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002618 break;
2619 case Instruction::ADD_LONG:
2620 case Instruction::SUB_LONG:
2621 case Instruction::MUL_LONG:
2622 case Instruction::DIV_LONG:
2623 case Instruction::REM_LONG:
2624 case Instruction::AND_LONG:
2625 case Instruction::OR_LONG:
2626 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::SHL_LONG:
2630 case Instruction::SHR_LONG:
2631 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 /* shift distance is Int, making these different from other binary operations */
2633 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::ADD_FLOAT:
2636 case Instruction::SUB_FLOAT:
2637 case Instruction::MUL_FLOAT:
2638 case Instruction::DIV_FLOAT:
2639 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::ADD_DOUBLE:
2643 case Instruction::SUB_DOUBLE:
2644 case Instruction::MUL_DOUBLE:
2645 case Instruction::DIV_DOUBLE:
2646 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::ADD_INT_2ADDR:
2650 case Instruction::SUB_INT_2ADDR:
2651 case Instruction::MUL_INT_2ADDR:
2652 case Instruction::REM_INT_2ADDR:
2653 case Instruction::SHL_INT_2ADDR:
2654 case Instruction::SHR_INT_2ADDR:
2655 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::AND_INT_2ADDR:
2659 case Instruction::OR_INT_2ADDR:
2660 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666 case Instruction::ADD_LONG_2ADDR:
2667 case Instruction::SUB_LONG_2ADDR:
2668 case Instruction::MUL_LONG_2ADDR:
2669 case Instruction::DIV_LONG_2ADDR:
2670 case Instruction::REM_LONG_2ADDR:
2671 case Instruction::AND_LONG_2ADDR:
2672 case Instruction::OR_LONG_2ADDR:
2673 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::SHL_LONG_2ADDR:
2677 case Instruction::SHR_LONG_2ADDR:
2678 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::ADD_FLOAT_2ADDR:
2682 case Instruction::SUB_FLOAT_2ADDR:
2683 case Instruction::MUL_FLOAT_2ADDR:
2684 case Instruction::DIV_FLOAT_2ADDR:
2685 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
2688 case Instruction::ADD_DOUBLE_2ADDR:
2689 case Instruction::SUB_DOUBLE_2ADDR:
2690 case Instruction::MUL_DOUBLE_2ADDR:
2691 case Instruction::DIV_DOUBLE_2ADDR:
2692 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::ADD_INT_LIT16:
2696 case Instruction::RSUB_INT:
2697 case Instruction::MUL_INT_LIT16:
2698 case Instruction::DIV_INT_LIT16:
2699 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::AND_INT_LIT16:
2703 case Instruction::OR_INT_LIT16:
2704 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::ADD_INT_LIT8:
2708 case Instruction::RSUB_INT_LIT8:
2709 case Instruction::MUL_INT_LIT8:
2710 case Instruction::DIV_INT_LIT8:
2711 case Instruction::REM_INT_LIT8:
2712 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002713 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002714 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717 case Instruction::AND_INT_LIT8:
2718 case Instruction::OR_INT_LIT8:
2719 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
2722
2723 /*
2724 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002725 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002726 * inserted in the course of verification, we can expect to see it here.
2727 */
jeffhaob4df5142011-09-19 20:25:32 -07002728 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002732 case Instruction::UNUSED_EE:
2733 case Instruction::UNUSED_EF:
2734 case Instruction::UNUSED_F2:
2735 case Instruction::UNUSED_F3:
2736 case Instruction::UNUSED_F4:
2737 case Instruction::UNUSED_F5:
2738 case Instruction::UNUSED_F6:
2739 case Instruction::UNUSED_F7:
2740 case Instruction::UNUSED_F8:
2741 case Instruction::UNUSED_F9:
2742 case Instruction::UNUSED_FA:
2743 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::UNUSED_F0:
2745 case Instruction::UNUSED_F1:
2746 case Instruction::UNUSED_E3:
2747 case Instruction::UNUSED_E8:
2748 case Instruction::UNUSED_E7:
2749 case Instruction::UNUSED_E4:
2750 case Instruction::UNUSED_E9:
2751 case Instruction::UNUSED_FC:
2752 case Instruction::UNUSED_E5:
2753 case Instruction::UNUSED_EA:
2754 case Instruction::UNUSED_FD:
2755 case Instruction::UNUSED_E6:
2756 case Instruction::UNUSED_EB:
2757 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002758 case Instruction::UNUSED_3E:
2759 case Instruction::UNUSED_3F:
2760 case Instruction::UNUSED_40:
2761 case Instruction::UNUSED_41:
2762 case Instruction::UNUSED_42:
2763 case Instruction::UNUSED_43:
2764 case Instruction::UNUSED_73:
2765 case Instruction::UNUSED_79:
2766 case Instruction::UNUSED_7A:
2767 case Instruction::UNUSED_EC:
2768 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002769 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771
2772 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002773 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002774 * complain if an instruction is missing (which is desirable).
2775 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002777
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 if (failure_ != VERIFY_ERROR_NONE) {
2779 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002780 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002781 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002782 return false;
2783 } else {
2784 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002785 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002787 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002789 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002791 opcode_flag = Instruction::kThrow;
2792 }
2793 }
jeffhaobdb76512011-09-07 11:43:16 -07002794 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 * If we didn't just set the result register, clear it out. This ensures that you can only use
2796 * "move-result" immediately after the result is set. (We could check this statically, but it's
2797 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002798 */
2799 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002801 }
2802
jeffhaoa0a764a2011-09-16 10:43:38 -07002803 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002804 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2806 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2807 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002808 return false;
2809 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2811 // next instruction isn't one.
2812 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002813 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 }
2815 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2816 if (next_line != NULL) {
2817 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2818 // needed.
2819 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002820 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 }
jeffhaobdb76512011-09-07 11:43:16 -07002822 } else {
2823 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 * We're not recording register data for the next instruction, so we don't know what the prior
2825 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002826 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002828 }
2829 }
2830
2831 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002832 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002833 *
2834 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002835 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002836 * somebody could get a reference field, check it for zero, and if the
2837 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002838 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002839 * that, and will reject the code.
2840 *
2841 * TODO: avoid re-fetching the branch target
2842 */
2843 if ((opcode_flag & Instruction::kBranch) != 0) {
2844 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002846 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002848 return false;
2849 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002850 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002852 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 }
jeffhaobdb76512011-09-07 11:43:16 -07002854 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002856 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 }
jeffhaobdb76512011-09-07 11:43:16 -07002858 }
2859
2860 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002861 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002862 *
2863 * We've already verified that the table is structurally sound, so we
2864 * just need to walk through and tag the targets.
2865 */
2866 if ((opcode_flag & Instruction::kSwitch) != 0) {
2867 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2868 const uint16_t* switch_insns = insns + offset_to_switch;
2869 int switch_count = switch_insns[1];
2870 int offset_to_targets, targ;
2871
2872 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2873 /* 0 = sig, 1 = count, 2/3 = first key */
2874 offset_to_targets = 4;
2875 } else {
2876 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002877 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002878 offset_to_targets = 2 + 2 * switch_count;
2879 }
2880
2881 /* verify each switch target */
2882 for (targ = 0; targ < switch_count; targ++) {
2883 int offset;
2884 uint32_t abs_offset;
2885
2886 /* offsets are 32-bit, and only partly endian-swapped */
2887 offset = switch_insns[offset_to_targets + targ * 2] |
2888 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 abs_offset = work_insn_idx_ + offset;
2890 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2891 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002892 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 }
2894 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002895 return false;
2896 }
2897 }
2898
2899 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2901 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002902 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2904 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002905 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002906
Ian Rogers0571d352011-11-03 19:51:38 -07002907 for (; iterator.HasNext(); iterator.Next()) {
2908 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 within_catch_all = true;
2910 }
jeffhaobdb76512011-09-07 11:43:16 -07002911 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2913 * "work_regs", because at runtime the exception will be thrown before the instruction
2914 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002915 */
Ian Rogers0571d352011-11-03 19:51:38 -07002916 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002917 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 }
jeffhaobdb76512011-09-07 11:43:16 -07002919 }
2920
2921 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2923 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002924 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002926 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 * The state in work_line reflects the post-execution state. If the current instruction is a
2928 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002929 * it will do so before grabbing the lock).
2930 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2932 Fail(VERIFY_ERROR_GENERIC)
2933 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002934 return false;
2935 }
2936 }
2937 }
2938
jeffhaod1f0fde2011-09-08 17:25:33 -07002939 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 if ((opcode_flag & Instruction::kReturn) != 0) {
2941 if(!work_line_->VerifyMonitorStackEmpty()) {
2942 return false;
2943 }
jeffhaobdb76512011-09-07 11:43:16 -07002944 }
2945
2946 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002947 * Update start_guess. Advance to the next instruction of that's
2948 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002949 * neither of those exists we're in a return or throw; leave start_guess
2950 * alone and let the caller sort it out.
2951 */
2952 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002953 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07002954 } else if ((opcode_flag & Instruction::kBranch) != 0) {
2955 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002957 }
2958
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2960 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002961
2962 return true;
2963}
2964
Ian Rogers28ad40d2011-10-27 15:19:26 -07002965const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002966 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002967 Class* referrer = method_->GetDeclaringClass();
2968 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
2969 const RegType& result =
2970 klass != NULL ? reg_types_.FromClass(klass)
2971 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
2972 if (klass == NULL && !result.IsUnresolvedTypes()) {
2973 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002975 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
2976 // check at runtime if access is allowed and so pass here.
2977 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
2978 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002979 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07002980 << result << "'";
2981 return reg_types_.Unknown();
2982 } else {
2983 return result;
2984 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002985}
2986
Ian Rogers28ad40d2011-10-27 15:19:26 -07002987const RegType& DexVerifier::GetCaughtExceptionType() {
2988 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002990 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2992 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002993 CatchHandlerIterator iterator(handlers_ptr);
2994 for (; iterator.HasNext(); iterator.Next()) {
2995 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2996 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002997 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002999 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3001 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3002 * test, so is essentially harmless.
3003 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003004 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3005 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3006 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003008 common_super = &exception;
3009 } else if (common_super->Equals(exception)) {
3010 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003011 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003012 common_super = &common_super->Merge(exception, &reg_types_);
3013 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 }
3015 }
3016 }
3017 }
Ian Rogers0571d352011-11-03 19:51:38 -07003018 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
3020 }
3021 if (common_super == NULL) {
3022 /* no catch blocks, or no catches with classes we can find */
3023 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3024 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003025 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003026}
3027
3028Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
3029 Class* referrer = method_->GetDeclaringClass();
3030 DexCache* dex_cache = referrer->GetDexCache();
3031 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3032 if (res_method == NULL) {
3033 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003034 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08003035 if(klass_type.IsUnresolvedTypes() || klass_type.IsUnknown()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003036 return NULL; // Can't resolve Class so no more to do here
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003038 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003039 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003040 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 if (is_direct) {
3042 res_method = klass->FindDirectMethod(name, signature);
3043 } else if (klass->IsInterface()) {
3044 res_method = klass->FindInterfaceMethod(name, signature);
3045 } else {
3046 res_method = klass->FindVirtualMethod(name, signature);
3047 }
3048 if (res_method != NULL) {
3049 dex_cache->SetResolvedMethod(method_idx, res_method);
3050 } else {
3051 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003052 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 << " " << signature;
3054 return NULL;
3055 }
3056 }
3057 /* Check if access is allowed. */
3058 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3059 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003060 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 return NULL;
3062 }
3063 return res_method;
3064}
3065
3066Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3067 MethodType method_type, bool is_range, bool is_super) {
3068 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3069 // we're making.
3070 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3071 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003072 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 return NULL;
3074 }
3075 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3076 // enforce them here.
3077 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3078 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3079 << PrettyMethod(res_method);
3080 return NULL;
3081 }
3082 // See if the method type implied by the invoke instruction matches the access flags for the
3083 // target method.
3084 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3085 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3086 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3087 ) {
3088 Fail(VERIFY_ERROR_GENERIC) << "invoke type does not match method type of "
3089 << PrettyMethod(res_method);
3090 return NULL;
3091 }
3092 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3093 // has a vtable entry for the target method.
3094 if (is_super) {
3095 DCHECK(method_type == METHOD_VIRTUAL);
3096 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3097 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3098 if (super == NULL) { // Only Object has no super class
3099 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3100 << " to super " << PrettyMethod(res_method);
3101 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003102 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003104 << " to super " << PrettyDescriptor(super)
3105 << "." << mh.GetName()
3106 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 }
3108 return NULL;
3109 }
3110 }
3111 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3112 // match the call to the signature. Also, we might might be calling through an abstract method
3113 // definition (which doesn't have register count values).
3114 int expected_args = dec_insn.vA_;
3115 /* caught by static verifier */
3116 DCHECK(is_range || expected_args <= 5);
3117 if (expected_args > code_item_->outs_size_) {
3118 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3119 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3120 return NULL;
3121 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003122 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 if (sig[0] != '(') {
3124 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3125 << " as descriptor doesn't start with '(': " << sig;
3126 return NULL;
3127 }
jeffhaobdb76512011-09-07 11:43:16 -07003128 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 * Check the "this" argument, which must be an instance of the class
3130 * that declared the method. For an interface class, we don't do the
3131 * full interface merge, so we can't do a rigorous check here (which
3132 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003133 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 int actual_args = 0;
3135 if (!res_method->IsStatic()) {
3136 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3137 if (failure_ != VERIFY_ERROR_NONE) {
3138 return NULL;
3139 }
3140 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3141 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3142 return NULL;
3143 }
3144 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003145 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3146 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3147 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3148 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 return NULL;
3150 }
3151 }
3152 actual_args++;
3153 }
3154 /*
3155 * Process the target method's signature. This signature may or may not
3156 * have been verified, so we can't assume it's properly formed.
3157 */
3158 size_t sig_offset = 0;
3159 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3160 if (actual_args >= expected_args) {
3161 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3162 << "'. Expected " << expected_args << " args, found more ("
3163 << sig.substr(sig_offset) << ")";
3164 return NULL;
3165 }
3166 std::string descriptor;
3167 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3168 size_t end;
3169 if (sig[sig_offset] == 'L') {
3170 end = sig.find(';', sig_offset);
3171 } else {
3172 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3173 if (sig[end] == 'L') {
3174 end = sig.find(';', end);
3175 }
3176 }
3177 if (end == std::string::npos) {
3178 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3179 << "bad signature component '" << sig << "' (missing ';')";
3180 return NULL;
3181 }
3182 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3183 sig_offset = end;
3184 } else {
3185 descriptor = sig[sig_offset];
3186 }
3187 const RegType& reg_type =
3188 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003189 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3190 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3191 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 }
3193 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3194 }
3195 if (sig[sig_offset] != ')') {
3196 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3197 return NULL;
3198 }
3199 if (actual_args != expected_args) {
3200 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3201 << " expected " << expected_args << " args, found " << actual_args;
3202 return NULL;
3203 } else {
3204 return res_method;
3205 }
3206}
3207
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003208const RegType& DexVerifier::GetMethodReturnType() {
3209 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3210 MethodHelper(method_).GetReturnTypeDescriptor());
3211}
3212
Ian Rogersd81871c2011-10-03 13:57:23 -07003213void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3214 const RegType& insn_type, bool is_primitive) {
3215 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3216 if (!index_type.IsArrayIndexTypes()) {
3217 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3218 } else {
3219 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3220 if (failure_ == VERIFY_ERROR_NONE) {
3221 if (array_class == NULL) {
3222 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3223 // instruction type. TODO: have a proper notion of bottom here.
3224 if (!is_primitive || insn_type.IsCategory1Types()) {
3225 // Reference or category 1
3226 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3227 } else {
3228 // Category 2
3229 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3230 }
3231 } else {
3232 /* verify the class */
3233 Class* component_class = array_class->GetComponentType();
3234 const RegType& component_type = reg_types_.FromClass(component_class);
3235 if (!array_class->IsArrayClass()) {
3236 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003237 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 } else if (component_class->IsPrimitive() && !is_primitive) {
3239 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003240 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 << " source for aget-object";
3242 } else if (!component_class->IsPrimitive() && is_primitive) {
3243 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003244 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 << " source for category 1 aget";
3246 } else if (is_primitive && !insn_type.Equals(component_type) &&
3247 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3248 (insn_type.IsLong() && component_type.IsDouble()))) {
3249 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003250 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 << " incompatible with aget of type " << insn_type;
3252 } else {
3253 // Use knowledge of the field type which is stronger than the type inferred from the
3254 // instruction, which can't differentiate object types and ints from floats, longs from
3255 // doubles.
3256 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3257 }
3258 }
3259 }
3260 }
3261}
3262
3263void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3264 const RegType& insn_type, bool is_primitive) {
3265 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3266 if (!index_type.IsArrayIndexTypes()) {
3267 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3268 } else {
3269 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3270 if (failure_ == VERIFY_ERROR_NONE) {
3271 if (array_class == NULL) {
3272 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3273 // instruction type.
3274 } else {
3275 /* verify the class */
3276 Class* component_class = array_class->GetComponentType();
3277 const RegType& component_type = reg_types_.FromClass(component_class);
3278 if (!array_class->IsArrayClass()) {
3279 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003280 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 } else if (component_class->IsPrimitive() && !is_primitive) {
3282 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003283 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 << " source for aput-object";
3285 } else if (!component_class->IsPrimitive() && is_primitive) {
3286 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003287 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 << " source for category 1 aput";
3289 } else if (is_primitive && !insn_type.Equals(component_type) &&
3290 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3291 (insn_type.IsLong() && component_type.IsDouble()))) {
3292 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003293 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 << " incompatible with aput of type " << insn_type;
3295 } else {
3296 // The instruction agrees with the type of array, confirm the value to be stored does too
3297 work_line_->VerifyRegisterType(dec_insn.vA_, component_type);
3298 }
3299 }
3300 }
3301 }
3302}
3303
3304Field* DexVerifier::GetStaticField(int field_idx) {
3305 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, true);
3306 if (field == NULL) {
3307 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003308 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3309 << dex_file_->GetFieldName(field_id) << ") in "
3310 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 DCHECK(Thread::Current()->IsExceptionPending());
3312 Thread::Current()->ClearException();
3313 return NULL;
3314 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3315 field->GetAccessFlags())) {
3316 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3317 << " from " << PrettyClass(method_->GetDeclaringClass());
3318 return NULL;
3319 } else if (!field->IsStatic()) {
3320 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3321 return NULL;
3322 } else {
3323 return field;
3324 }
3325}
3326
Ian Rogersd81871c2011-10-03 13:57:23 -07003327Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
3328 Field* field = Runtime::Current()->GetClassLinker()->ResolveField(field_idx, method_, false);
3329 if (field == NULL) {
3330 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003331 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3332 << dex_file_->GetFieldName(field_id) << ") in "
3333 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 DCHECK(Thread::Current()->IsExceptionPending());
3335 Thread::Current()->ClearException();
3336 return NULL;
3337 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3338 field->GetAccessFlags())) {
3339 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3340 << " from " << PrettyClass(method_->GetDeclaringClass());
3341 return NULL;
3342 } else if (field->IsStatic()) {
3343 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3344 << " to not be static";
3345 return NULL;
3346 } else if (obj_type.IsZero()) {
3347 // Cannot infer and check type, however, access will cause null pointer exception
3348 return field;
3349 } else if(obj_type.IsUninitializedReference() &&
3350 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3351 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3352 // Field accesses through uninitialized references are only allowable for constructors where
3353 // the field is declared in this class
3354 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3355 << " of a not fully initialized object within the context of "
3356 << PrettyMethod(method_);
3357 return NULL;
3358 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3359 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3360 // of C1. For resolution to occur the declared class of the field must be compatible with
3361 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3362 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3363 << " from object of type " << PrettyClass(obj_type.GetClass());
3364 return NULL;
3365 } else {
3366 return field;
3367 }
3368}
3369
Ian Rogersb94a27b2011-10-26 00:33:41 -07003370void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3371 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003372 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003373 Field* field;
3374 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003375 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003376 } else {
3377 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003378 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003379 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003380 if (failure_ != VERIFY_ERROR_NONE) {
3381 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3382 } else {
3383 const char* descriptor;
3384 const ClassLoader* loader;
3385 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003386 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003387 loader = field->GetDeclaringClass()->GetClassLoader();
3388 } else {
3389 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3390 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3391 loader = method_->GetDeclaringClass()->GetClassLoader();
3392 }
3393 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003395 if (field_type.Equals(insn_type) ||
3396 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3397 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 // expected that read is of the correct primitive type or that int reads are reading
3399 // floats or long reads are reading doubles
3400 } else {
3401 // This is a global failure rather than a class change failure as the instructions and
3402 // the descriptors for the type should have been consistent within the same file at
3403 // compile time
3404 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003405 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003406 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 return;
3408 }
3409 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003410 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003412 << " to be compatible with type '" << insn_type
3413 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003414 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 return;
3416 }
3417 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003418 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 }
3420}
3421
Ian Rogersb94a27b2011-10-26 00:33:41 -07003422void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3423 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003424 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003425 Field* field;
3426 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003427 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003428 } else {
3429 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003430 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003431 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003432 if (failure_ != VERIFY_ERROR_NONE) {
3433 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3434 } else {
3435 const char* descriptor;
3436 const ClassLoader* loader;
3437 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003438 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003439 loader = field->GetDeclaringClass()->GetClassLoader();
3440 } else {
3441 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3442 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3443 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003445 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3446 if (field != NULL) {
3447 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3448 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3449 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3450 return;
3451 }
3452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003454 // Primitive field assignability rules are weaker than regular assignability rules
3455 bool instruction_compatible;
3456 bool value_compatible;
3457 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3458 if (field_type.IsIntegralTypes()) {
3459 instruction_compatible = insn_type.IsIntegralTypes();
3460 value_compatible = value_type.IsIntegralTypes();
3461 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003462 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003463 value_compatible = value_type.IsFloatTypes();
3464 } else if (field_type.IsLong()) {
3465 instruction_compatible = insn_type.IsLong();
3466 value_compatible = value_type.IsLongTypes();
3467 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003468 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003469 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003471 instruction_compatible = false; // reference field with primitive store
3472 value_compatible = false; // unused
3473 }
3474 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 // This is a global failure rather than a class change failure as the instructions and
3476 // the descriptors for the type should have been consistent within the same file at
3477 // compile time
3478 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003479 << " to be of type '" << insn_type
3480 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003481 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 return;
3483 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003484 if (!value_compatible) {
3485 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3486 << " of type " << value_type
3487 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003488 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003489 return;
3490 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003492 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003493 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003494 << " to be compatible with type '" << insn_type
3495 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003496 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 return;
3498 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003499 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 }
3502}
3503
3504bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3505 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3506 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3507 return false;
3508 }
3509 return true;
3510}
3511
3512void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003513 const RegType& res_type, bool is_range) {
3514 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 /*
3516 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3517 * list and fail. It's legal, if silly, for arg_count to be zero.
3518 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003519 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3520 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 uint32_t arg_count = dec_insn.vA_;
3522 for (size_t ui = 0; ui < arg_count; ui++) {
3523 uint32_t get_reg;
3524
3525 if (is_range)
3526 get_reg = dec_insn.vC_ + ui;
3527 else
3528 get_reg = dec_insn.arg_[ui];
3529
3530 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3531 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3532 << ") not valid";
3533 return;
3534 }
3535 }
3536}
3537
3538void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003539 if (Runtime::Current()->IsStarted()) {
3540 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3541 << " " << fail_messages_.str();
3542 return;
3543 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3545 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3546 VerifyErrorRefType ref_type;
3547 switch (inst->Opcode()) {
3548 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003549 case Instruction::CHECK_CAST:
3550 case Instruction::INSTANCE_OF:
3551 case Instruction::NEW_INSTANCE:
3552 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003553 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003554 case Instruction::FILLED_NEW_ARRAY_RANGE:
3555 ref_type = VERIFY_ERROR_REF_CLASS;
3556 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003558 case Instruction::IGET_BOOLEAN:
3559 case Instruction::IGET_BYTE:
3560 case Instruction::IGET_CHAR:
3561 case Instruction::IGET_SHORT:
3562 case Instruction::IGET_WIDE:
3563 case Instruction::IGET_OBJECT:
3564 case Instruction::IPUT:
3565 case Instruction::IPUT_BOOLEAN:
3566 case Instruction::IPUT_BYTE:
3567 case Instruction::IPUT_CHAR:
3568 case Instruction::IPUT_SHORT:
3569 case Instruction::IPUT_WIDE:
3570 case Instruction::IPUT_OBJECT:
3571 case Instruction::SGET:
3572 case Instruction::SGET_BOOLEAN:
3573 case Instruction::SGET_BYTE:
3574 case Instruction::SGET_CHAR:
3575 case Instruction::SGET_SHORT:
3576 case Instruction::SGET_WIDE:
3577 case Instruction::SGET_OBJECT:
3578 case Instruction::SPUT:
3579 case Instruction::SPUT_BOOLEAN:
3580 case Instruction::SPUT_BYTE:
3581 case Instruction::SPUT_CHAR:
3582 case Instruction::SPUT_SHORT:
3583 case Instruction::SPUT_WIDE:
3584 case Instruction::SPUT_OBJECT:
3585 ref_type = VERIFY_ERROR_REF_FIELD;
3586 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003588 case Instruction::INVOKE_VIRTUAL_RANGE:
3589 case Instruction::INVOKE_SUPER:
3590 case Instruction::INVOKE_SUPER_RANGE:
3591 case Instruction::INVOKE_DIRECT:
3592 case Instruction::INVOKE_DIRECT_RANGE:
3593 case Instruction::INVOKE_STATIC:
3594 case Instruction::INVOKE_STATIC_RANGE:
3595 case Instruction::INVOKE_INTERFACE:
3596 case Instruction::INVOKE_INTERFACE_RANGE:
3597 ref_type = VERIFY_ERROR_REF_METHOD;
3598 break;
jeffhaobdb76512011-09-07 11:43:16 -07003599 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003600 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003601 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003602 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3604 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3605 // instruction, so assert it.
3606 size_t width = inst->SizeInCodeUnits();
3607 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003608 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 // NOPs
3610 for (size_t i = 2; i < width; i++) {
3611 insns[work_insn_idx_ + i] = Instruction::NOP;
3612 }
3613 // Encode the opcode, with the failure code in the high byte
3614 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3615 (failure_ << 8) | // AA - component
3616 (ref_type << (8 + kVerifyErrorRefTypeShift));
3617 insns[work_insn_idx_] = new_instruction;
3618 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3619 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003620 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3621 << fail_messages_.str();
3622 if (gDebugVerify) {
3623 std::cout << std::endl << info_messages_.str();
3624 Dump(std::cout);
3625 }
jeffhaobdb76512011-09-07 11:43:16 -07003626}
jeffhaoba5ebb92011-08-25 17:24:37 -07003627
Ian Rogersd81871c2011-10-03 13:57:23 -07003628bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3629 const bool merge_debug = true;
3630 bool changed = true;
3631 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3632 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003633 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 * We haven't processed this instruction before, and we haven't touched the registers here, so
3635 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3636 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003637 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003639 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3641 copy->CopyFromLine(target_line);
3642 changed = target_line->MergeRegisters(merge_line);
3643 if (failure_ != VERIFY_ERROR_NONE) {
3644 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003645 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003646 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3648 << *copy.get() << " MERGE" << std::endl
3649 << *merge_line << " ==" << std::endl
3650 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003651 }
3652 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 if (changed) {
3654 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003655 }
3656 return true;
3657}
3658
Ian Rogersd81871c2011-10-03 13:57:23 -07003659void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3660 size_t* log2_max_gc_pc) {
3661 size_t local_gc_points = 0;
3662 size_t max_insn = 0;
3663 size_t max_ref_reg = -1;
3664 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3665 if (insn_flags_[i].IsGcPoint()) {
3666 local_gc_points++;
3667 max_insn = i;
3668 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003669 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003670 }
3671 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 *gc_points = local_gc_points;
3673 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3674 size_t i = 0;
3675 while ((1U << i) < max_insn) {
3676 i++;
3677 }
3678 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003679}
3680
Ian Rogersd81871c2011-10-03 13:57:23 -07003681ByteArray* DexVerifier::GenerateGcMap() {
3682 size_t num_entries, ref_bitmap_bits, pc_bits;
3683 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3684 // There's a single byte to encode the size of each bitmap
3685 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3686 // TODO: either a better GC map format or per method failures
3687 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3688 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003689 return NULL;
3690 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003691 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3692 // There are 2 bytes to encode the number of entries
3693 if (num_entries >= 65536) {
3694 // TODO: either a better GC map format or per method failures
3695 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3696 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003697 return NULL;
3698 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003700 RegisterMapFormat format;
Ian Rogersd81871c2011-10-03 13:57:23 -07003701 if (pc_bits < 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003702 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 pc_bytes = 1;
3704 } else if (pc_bits < 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003705 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003706 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003707 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003708 // TODO: either a better GC map format or per method failures
3709 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3710 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3711 return NULL;
3712 }
3713 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
3714 ByteArray* table = ByteArray::Alloc(table_size);
3715 if (table == NULL) {
3716 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3717 return NULL;
3718 }
3719 // Write table header
3720 table->Set(0, format);
3721 table->Set(1, ref_bitmap_bytes);
3722 table->Set(2, num_entries & 0xFF);
3723 table->Set(3, (num_entries >> 8) & 0xFF);
3724 // Write table data
3725 size_t offset = 4;
3726 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3727 if (insn_flags_[i].IsGcPoint()) {
3728 table->Set(offset, i & 0xFF);
3729 offset++;
3730 if (pc_bytes == 2) {
3731 table->Set(offset, (i >> 8) & 0xFF);
3732 offset++;
3733 }
3734 RegisterLine* line = reg_table_.GetLine(i);
3735 line->WriteReferenceBitMap(table->GetData() + offset, ref_bitmap_bytes);
3736 offset += ref_bitmap_bytes;
3737 }
3738 }
3739 DCHECK(offset == table_size);
3740 return table;
3741}
jeffhaoa0a764a2011-09-16 10:43:38 -07003742
Ian Rogersd81871c2011-10-03 13:57:23 -07003743void DexVerifier::VerifyGcMap() {
3744 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3745 // that the table data is well formed and all references are marked (or not) in the bitmap
3746 PcToReferenceMap map(method_);
3747 size_t map_index = 0;
3748 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3749 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3750 if (insn_flags_[i].IsGcPoint()) {
3751 CHECK_LT(map_index, map.NumEntries());
3752 CHECK_EQ(map.GetPC(map_index), i);
3753 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3754 map_index++;
3755 RegisterLine* line = reg_table_.GetLine(i);
3756 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003757 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 CHECK_LT(j / 8, map.RegWidth());
3759 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3760 } else if ((j / 8) < map.RegWidth()) {
3761 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3762 } else {
3763 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3764 }
3765 }
3766 } else {
3767 CHECK(reg_bitmap == NULL);
3768 }
3769 }
3770}
jeffhaoa0a764a2011-09-16 10:43:38 -07003771
Ian Rogersd81871c2011-10-03 13:57:23 -07003772const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3773 size_t num_entries = NumEntries();
3774 // Do linear or binary search?
3775 static const size_t kSearchThreshold = 8;
3776 if (num_entries < kSearchThreshold) {
3777 for (size_t i = 0; i < num_entries; i++) {
3778 if (GetPC(i) == dex_pc) {
3779 return GetBitMap(i);
3780 }
3781 }
3782 } else {
3783 int lo = 0;
3784 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003785 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 int mid = (hi + lo) / 2;
3787 int mid_pc = GetPC(mid);
3788 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003789 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003791 hi = mid - 1;
3792 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003794 }
3795 }
3796 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 if (error_if_not_present) {
3798 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3799 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003800 return NULL;
3801}
3802
Ian Rogersd81871c2011-10-03 13:57:23 -07003803} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003804} // namespace art