blob: deffdc29b43cc16b0aee29493f5d3d6ae002bbb0 [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"
Brian Carlstrome7d856b2012-01-11 18:10:55 -08008#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -07009#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070010#include "dex_file.h"
11#include "dex_instruction.h"
12#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070013#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070014#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070015#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070016#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080017#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070022namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070023
Ian Rogers2c8a8572011-10-24 17:11:36 -070024static const bool gDebugVerify = false;
25
Ian Rogersd81871c2011-10-03 13:57:23 -070026std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
Brian Carlstrom75412882012-01-18 01:26:54 -080027 switch (rhs) {
28 case VERIFY_ERROR_NONE: os << "VERIFY_ERROR_NONE"; break;
29 case VERIFY_ERROR_GENERIC: os << "VERIFY_ERROR_GENERIC"; break;
30 case VERIFY_ERROR_NO_CLASS: os << "VERIFY_ERROR_NO_CLASS"; break;
31 case VERIFY_ERROR_NO_FIELD: os << "VERIFY_ERROR_NO_FIELD"; break;
32 case VERIFY_ERROR_NO_METHOD: os << "VERIFY_ERROR_NO_METHOD"; break;
33 case VERIFY_ERROR_ACCESS_CLASS: os << "VERIFY_ERROR_ACCESS_CLASS"; break;
34 case VERIFY_ERROR_ACCESS_FIELD: os << "VERIFY_ERROR_ACCESS_FIELD"; break;
35 case VERIFY_ERROR_ACCESS_METHOD: os << "VERIFY_ERROR_ACCESS_METHOD"; break;
36 case VERIFY_ERROR_CLASS_CHANGE: os << "VERIFY_ERROR_CLASS_CHANGE"; break;
37 case VERIFY_ERROR_INSTANTIATION: os << "VERIFY_ERROR_INSTANTIATION"; break;
38 default:
39 os << "VerifyError[" << static_cast<int>(rhs) << "]";
40 break;
41 }
42 return os;
Ian Rogersd81871c2011-10-03 13:57:23 -070043}
jeffhaobdb76512011-09-07 11:43:16 -070044
Ian Rogers84fa0742011-10-25 18:13:30 -070045static const char* type_strings[] = {
46 "Unknown",
47 "Conflict",
48 "Boolean",
49 "Byte",
50 "Short",
51 "Char",
52 "Integer",
53 "Float",
54 "Long (Low Half)",
55 "Long (High Half)",
56 "Double (Low Half)",
57 "Double (High Half)",
58 "64-bit Constant (Low Half)",
59 "64-bit Constant (High Half)",
60 "32-bit Constant",
61 "Unresolved Reference",
62 "Uninitialized Reference",
63 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070064 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070065 "Reference",
66};
Ian Rogersd81871c2011-10-03 13:57:23 -070067
Ian Rogers2c8a8572011-10-24 17:11:36 -070068std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070069 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
70 std::string result;
71 if (IsConstant()) {
72 uint32_t val = ConstantValue();
73 if (val == 0) {
74 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070075 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070076 if(IsConstantShort()) {
77 result = StringPrintf("32-bit Constant: %d", val);
78 } else {
79 result = StringPrintf("32-bit Constant: 0x%x", val);
80 }
81 }
82 } else {
83 result = type_strings[type_];
84 if (IsReferenceTypes()) {
85 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -070086 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070087 result += PrettyDescriptor(GetDescriptor());
88 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080089 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -070090 }
Ian Rogersd81871c2011-10-03 13:57:23 -070091 }
92 }
Ian Rogers84fa0742011-10-25 18:13:30 -070093 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070094}
95
96const RegType& RegType::HighHalf(RegTypeCache* cache) const {
97 CHECK(IsLowHalf());
98 if (type_ == kRegTypeLongLo) {
99 return cache->FromType(kRegTypeLongHi);
100 } else if (type_ == kRegTypeDoubleLo) {
101 return cache->FromType(kRegTypeDoubleHi);
102 } else {
103 return cache->FromType(kRegTypeConstHi);
104 }
105}
106
107/*
108 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
109 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
110 * 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
111 * is the deepest (lowest upper bound) parent of S and T).
112 *
113 * This operation applies for regular classes and arrays, however, for interface types there needn't
114 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
115 * introducing sets of types, however, the only operation permissible on an interface is
116 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
117 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700118 * the perversion of any Object being assignable to an interface type (note, however, that we don't
119 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
120 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700121 */
122Class* RegType::ClassJoin(Class* s, Class* t) {
123 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
124 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
125 if (s == t) {
126 return s;
127 } else if (s->IsAssignableFrom(t)) {
128 return s;
129 } else if (t->IsAssignableFrom(s)) {
130 return t;
131 } else if (s->IsArrayClass() && t->IsArrayClass()) {
132 Class* s_ct = s->GetComponentType();
133 Class* t_ct = t->GetComponentType();
134 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
135 // Given the types aren't the same, if either array is of primitive types then the only
136 // common parent is java.lang.Object
137 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
138 DCHECK(result->IsObjectClass());
139 return result;
140 }
141 Class* common_elem = ClassJoin(s_ct, t_ct);
142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
143 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800144 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700146 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
147 DCHECK(array_class != NULL);
148 return array_class;
149 } else {
150 size_t s_depth = s->Depth();
151 size_t t_depth = t->Depth();
152 // Get s and t to the same depth in the hierarchy
153 if (s_depth > t_depth) {
154 while (s_depth > t_depth) {
155 s = s->GetSuperClass();
156 s_depth--;
157 }
158 } else {
159 while (t_depth > s_depth) {
160 t = t->GetSuperClass();
161 t_depth--;
162 }
163 }
164 // Go up the hierarchy until we get to the common parent
165 while (s != t) {
166 s = s->GetSuperClass();
167 t = t->GetSuperClass();
168 }
169 return s;
170 }
171}
172
Ian Rogersb5e95b92011-10-25 23:28:55 -0700173bool RegType::IsAssignableFrom(const RegType& src) const {
174 if (Equals(src)) {
175 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700176 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700177 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700178 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
179 case RegType::kRegTypeByte: return src.IsByteTypes();
180 case RegType::kRegTypeShort: return src.IsShortTypes();
181 case RegType::kRegTypeChar: return src.IsCharTypes();
182 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
183 case RegType::kRegTypeFloat: return src.IsFloatTypes();
184 case RegType::kRegTypeLongLo: return src.IsLongTypes();
185 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700186 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700187 if (!IsReferenceTypes()) {
188 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700189 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700190 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700191 return true; // all reference types can be assigned null
192 } else if (!src.IsReferenceTypes()) {
193 return false; // expect src to be a reference type
194 } else if (IsJavaLangObject()) {
195 return true; // all reference types can be assigned to Object
196 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700197 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700198 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700199 GetClass()->IsAssignableFrom(src.GetClass())) {
200 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700201 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700202 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700203 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700205 }
206 }
207}
208
Ian Rogers84fa0742011-10-25 18:13:30 -0700209static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
210 return a.IsConstant() ? b : a;
211}
jeffhaobdb76512011-09-07 11:43:16 -0700212
Ian Rogersd81871c2011-10-03 13:57:23 -0700213const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
214 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700215 if (IsUnknown() && incoming_type.IsUnknown()) {
216 return *this; // Unknown MERGE Unknown => Unknown
217 } else if (IsConflict()) {
218 return *this; // Conflict MERGE * => Conflict
219 } else if (incoming_type.IsConflict()) {
220 return incoming_type; // * MERGE Conflict => Conflict
221 } else if (IsUnknown() || incoming_type.IsUnknown()) {
222 return reg_types->Conflict(); // Unknown MERGE * => Conflict
223 } else if(IsConstant() && incoming_type.IsConstant()) {
224 int32_t val1 = ConstantValue();
225 int32_t val2 = incoming_type.ConstantValue();
226 if (val1 >= 0 && val2 >= 0) {
227 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
228 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700229 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700230 } else {
231 return incoming_type;
232 }
233 } else if (val1 < 0 && val2 < 0) {
234 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
235 if (val1 <= val2) {
236 return *this;
237 } else {
238 return incoming_type;
239 }
240 } else {
241 // Values are +ve and -ve, choose smallest signed type in which they both fit
242 if (IsConstantByte()) {
243 if (incoming_type.IsConstantByte()) {
244 return reg_types->ByteConstant();
245 } else if (incoming_type.IsConstantShort()) {
246 return reg_types->ShortConstant();
247 } else {
248 return reg_types->IntConstant();
249 }
250 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700251 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700252 return reg_types->ShortConstant();
253 } else {
254 return reg_types->IntConstant();
255 }
256 } else {
257 return reg_types->IntConstant();
258 }
259 }
260 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
261 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
262 return reg_types->Boolean(); // boolean MERGE boolean => boolean
263 }
264 if (IsByteTypes() && incoming_type.IsByteTypes()) {
265 return reg_types->Byte(); // byte MERGE byte => byte
266 }
267 if (IsShortTypes() && incoming_type.IsShortTypes()) {
268 return reg_types->Short(); // short MERGE short => short
269 }
270 if (IsCharTypes() && incoming_type.IsCharTypes()) {
271 return reg_types->Char(); // char MERGE char => char
272 }
273 return reg_types->Integer(); // int MERGE * => int
274 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
275 (IsLongTypes() && incoming_type.IsLongTypes()) ||
276 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
277 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
278 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
279 // check constant case was handled prior to entry
280 DCHECK(!IsConstant() || !incoming_type.IsConstant());
281 // float/long/double MERGE float/long/double_constant => float/long/double
282 return SelectNonConstant(*this, incoming_type);
283 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700284 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700285 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700286 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
287 return reg_types->JavaLangObject(); // Object MERGE ref => Object
288 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
289 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
290 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
291 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700292 return reg_types->Conflict();
293 } else { // Two reference types, compute Join
294 Class* c1 = GetClass();
295 Class* c2 = incoming_type.GetClass();
296 DCHECK(c1 != NULL && !c1->IsPrimitive());
297 DCHECK(c2 != NULL && !c2->IsPrimitive());
298 Class* join_class = ClassJoin(c1, c2);
299 if (c1 == join_class) {
300 return *this;
301 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700302 return incoming_type;
303 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700304 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700305 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700306 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700307 } else {
308 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 }
310}
311
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700312static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700313 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700314 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
315 case Primitive::kPrimByte: return RegType::kRegTypeByte;
316 case Primitive::kPrimShort: return RegType::kRegTypeShort;
317 case Primitive::kPrimChar: return RegType::kRegTypeChar;
318 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
319 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
320 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
321 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
322 case Primitive::kPrimVoid:
323 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 }
325}
326
327static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
328 if (descriptor.length() == 1) {
329 switch (descriptor[0]) {
330 case 'Z': return RegType::kRegTypeBoolean;
331 case 'B': return RegType::kRegTypeByte;
332 case 'S': return RegType::kRegTypeShort;
333 case 'C': return RegType::kRegTypeChar;
334 case 'I': return RegType::kRegTypeInteger;
335 case 'J': return RegType::kRegTypeLongLo;
336 case 'F': return RegType::kRegTypeFloat;
337 case 'D': return RegType::kRegTypeDoubleLo;
338 case 'V':
339 default: return RegType::kRegTypeUnknown;
340 }
341 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
342 return RegType::kRegTypeReference;
343 } else {
344 return RegType::kRegTypeUnknown;
345 }
346}
347
348std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700349 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 return os;
351}
352
353const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800354 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700355 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
356}
357
358const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800359 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700360 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700361 // entries should be sized greater than primitive types
362 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
363 RegType* entry = entries_[type];
364 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700365 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800366 if (strlen(descriptor) != 0) {
367 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700368 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700369 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 entries_[type] = entry;
371 }
372 return *entry;
373 } else {
374 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800375 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700376 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700378 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800379 if (cur_entry->IsReference()) {
380 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800381 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 return *cur_entry;
383 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700384 } else if (cur_entry->IsUnresolvedReference() &&
385 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700386 return *cur_entry;
387 }
388 }
Ian Rogers672297c2012-01-10 14:50:55 -0800389 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700390 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700391 // Able to resolve so create resolved register type
392 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700393 entries_.push_back(entry);
394 return *entry;
395 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700396 // TODO: we assume unresolved, but we may be able to do better by validating whether the
397 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700398 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700399 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700400 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800401 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700402 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800403 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700404 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
405 entries_.size());
406 entries_.push_back(entry);
407 return *entry;
408 } else {
409 // The descriptor is broken return the unknown type as there's nothing sensible that
410 // could be done at runtime
411 return Unknown();
412 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700413 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700414 }
415}
416
417const RegType& RegTypeCache::FromClass(Class* klass) {
418 if (klass->IsPrimitive()) {
419 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
420 // entries should be sized greater than primitive types
421 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
422 RegType* entry = entries_[type];
423 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700424 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 entries_[type] = entry;
426 }
427 return *entry;
428 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700429 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700431 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700432 return *cur_entry;
433 }
434 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700435 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 entries_.push_back(entry);
437 return *entry;
438 }
439}
440
Ian Rogers28ad40d2011-10-27 15:19:26 -0700441const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
442 RegType* entry;
443 if (type.IsUnresolvedTypes()) {
444 String* descriptor = type.GetDescriptor();
445 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
446 RegType* cur_entry = entries_[i];
447 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
448 cur_entry->GetAllocationPc() == allocation_pc &&
449 cur_entry->GetDescriptor() == descriptor) {
450 return *cur_entry;
451 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700453 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
454 descriptor, allocation_pc, entries_.size());
455 } else {
456 Class* klass = type.GetClass();
457 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
458 RegType* cur_entry = entries_[i];
459 if (cur_entry->IsUninitializedReference() &&
460 cur_entry->GetAllocationPc() == allocation_pc &&
461 cur_entry->GetClass() == klass) {
462 return *cur_entry;
463 }
464 }
465 entry = new RegType(RegType::kRegTypeUninitializedReference,
466 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700467 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700468 entries_.push_back(entry);
469 return *entry;
470}
471
472const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
473 RegType* entry;
474 if (uninit_type.IsUnresolvedTypes()) {
475 String* descriptor = uninit_type.GetDescriptor();
476 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
477 RegType* cur_entry = entries_[i];
478 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
479 return *cur_entry;
480 }
481 }
482 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
483 } else {
484 Class* klass = uninit_type.GetClass();
485 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
486 RegType* cur_entry = entries_[i];
487 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
488 return *cur_entry;
489 }
490 }
491 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
492 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 entries_.push_back(entry);
494 return *entry;
495}
496
497const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700498 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700499 RegType* cur_entry = entries_[i];
500 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
501 return *cur_entry;
502 }
503 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700504 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 entries_.size());
506 entries_.push_back(entry);
507 return *entry;
508}
509
510const RegType& RegTypeCache::FromType(RegType::Type type) {
511 CHECK(type < RegType::kRegTypeReference);
512 switch (type) {
513 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
514 case RegType::kRegTypeByte: return From(type, NULL, "B");
515 case RegType::kRegTypeShort: return From(type, NULL, "S");
516 case RegType::kRegTypeChar: return From(type, NULL, "C");
517 case RegType::kRegTypeInteger: return From(type, NULL, "I");
518 case RegType::kRegTypeFloat: return From(type, NULL, "F");
519 case RegType::kRegTypeLongLo:
520 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
521 case RegType::kRegTypeDoubleLo:
522 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
523 default: return From(type, NULL, "");
524 }
525}
526
527const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700528 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
529 RegType* cur_entry = entries_[i];
530 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
531 return *cur_entry;
532 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700534 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
535 entries_.push_back(entry);
536 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700537}
538
Ian Rogers28ad40d2011-10-27 15:19:26 -0700539const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
540 CHECK(array.IsArrayClass());
541 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800542 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
543 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800544 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700545 } else {
546 return FromClass(array.GetClass()->GetComponentType());
547 }
548}
549
550
Ian Rogersd81871c2011-10-03 13:57:23 -0700551bool RegisterLine::CheckConstructorReturn() const {
552 for (size_t i = 0; i < num_regs_; i++) {
553 if (GetRegisterType(i).IsUninitializedThisReference()) {
554 verifier_->Fail(VERIFY_ERROR_GENERIC)
555 << "Constructor returning without calling superclass constructor";
556 return false;
557 }
558 }
559 return true;
560}
561
562void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
563 DCHECK(vdst < num_regs_);
564 if (new_type.IsLowHalf()) {
565 line_[vdst] = new_type.GetId();
566 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
567 } else if (new_type.IsHighHalf()) {
568 /* should never set these explicitly */
569 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
570 } else if (new_type.IsConflict()) { // should only be set during a merge
571 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
572 } else {
573 line_[vdst] = new_type.GetId();
574 }
575 // Clear the monitor entry bits for this register.
576 ClearAllRegToLockDepths(vdst);
577}
578
579void RegisterLine::SetResultTypeToUnknown() {
580 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
581 result_[0] = unknown_id;
582 result_[1] = unknown_id;
583}
584
585void RegisterLine::SetResultRegisterType(const RegType& new_type) {
586 result_[0] = new_type.GetId();
587 if(new_type.IsLowHalf()) {
588 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
589 result_[1] = new_type.GetId() + 1;
590 } else {
591 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
592 }
593}
594
595const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
596 // The register index was validated during the static pass, so we don't need to check it here.
597 DCHECK_LT(vsrc, num_regs_);
598 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
599}
600
601const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
602 if (dec_insn.vA_ < 1) {
603 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
604 return verifier_->GetRegTypeCache()->Unknown();
605 }
606 /* get the element type of the array held in vsrc */
607 const RegType& this_type = GetRegisterType(dec_insn.vC_);
608 if (!this_type.IsReferenceTypes()) {
609 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
610 << dec_insn.vC_ << " (type=" << this_type << ")";
611 return verifier_->GetRegTypeCache()->Unknown();
612 }
613 return this_type;
614}
615
616Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
617 /* get the element type of the array held in vsrc */
618 const RegType& type = GetRegisterType(vsrc);
619 /* if "always zero", we allow it to fail at runtime */
620 if (type.IsZero()) {
621 return NULL;
622 } else if (!type.IsReferenceTypes()) {
623 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
624 << " (type=" << type << ")";
625 return NULL;
626 } else if (type.IsUninitializedReference()) {
627 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
628 return NULL;
629 } else {
630 return type.GetClass();
631 }
632}
633
634bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
635 // Verify the src register type against the check type refining the type of the register
636 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700637 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
639 << " but expected " << check_type;
640 return false;
641 }
642 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
643 // precise than the subtype in vsrc so leave it for reference types. For primitive types
644 // if they are a defined type then they are as precise as we can get, however, for constant
645 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
646 return true;
647}
648
649void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700650 DCHECK(uninit_type.IsUninitializedTypes());
651 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
652 size_t changed = 0;
653 for (size_t i = 0; i < num_regs_; i++) {
654 if (GetRegisterType(i).Equals(uninit_type)) {
655 line_[i] = init_type.GetId();
656 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700659 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660}
661
662void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
663 for (size_t i = 0; i < num_regs_; i++) {
664 if (GetRegisterType(i).Equals(uninit_type)) {
665 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
666 ClearAllRegToLockDepths(i);
667 }
668 }
669}
670
671void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
672 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
673 const RegType& type = GetRegisterType(vsrc);
674 SetRegisterType(vdst, type);
675 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
676 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
677 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
678 << " cat=" << static_cast<int>(cat);
679 } else if (cat == kTypeCategoryRef) {
680 CopyRegToLockDepth(vdst, vsrc);
681 }
682}
683
684void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
685 const RegType& type_l = GetRegisterType(vsrc);
686 const RegType& type_h = GetRegisterType(vsrc + 1);
687
688 if (!type_l.CheckWidePair(type_h)) {
689 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
690 << " type=" << type_l << "/" << type_h;
691 } else {
692 SetRegisterType(vdst, type_l); // implicitly sets the second half
693 }
694}
695
696void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
697 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
698 if ((!is_reference && !type.IsCategory1Types()) ||
699 (is_reference && !type.IsReferenceTypes())) {
700 verifier_->Fail(VERIFY_ERROR_GENERIC)
701 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
702 } else {
703 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
704 SetRegisterType(vdst, type);
705 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
706 }
707}
708
709/*
710 * Implement "move-result-wide". Copy the category-2 value from the result
711 * register to another register, and reset the result register.
712 */
713void RegisterLine::CopyResultRegister2(uint32_t vdst) {
714 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
715 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
716 if (!type_l.IsCategory2Types()) {
717 verifier_->Fail(VERIFY_ERROR_GENERIC)
718 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
719 } else {
720 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
721 SetRegisterType(vdst, type_l); // also sets the high
722 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
723 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
724 }
725}
726
727void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
728 const RegType& dst_type, const RegType& src_type) {
729 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
730 SetRegisterType(dec_insn.vA_, dst_type);
731 }
732}
733
734void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
735 const RegType& dst_type,
736 const RegType& src_type1, const RegType& src_type2,
737 bool check_boolean_op) {
738 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
739 VerifyRegisterType(dec_insn.vC_, src_type2)) {
740 if (check_boolean_op) {
741 DCHECK(dst_type.IsInteger());
742 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
743 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
744 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
745 return;
746 }
747 }
748 SetRegisterType(dec_insn.vA_, dst_type);
749 }
750}
751
752void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
753 const RegType& dst_type, const RegType& src_type1,
754 const RegType& src_type2, bool check_boolean_op) {
755 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
756 VerifyRegisterType(dec_insn.vB_, src_type2)) {
757 if (check_boolean_op) {
758 DCHECK(dst_type.IsInteger());
759 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
760 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
761 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
762 return;
763 }
764 }
765 SetRegisterType(dec_insn.vA_, dst_type);
766 }
767}
768
769void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
770 const RegType& dst_type, const RegType& src_type,
771 bool check_boolean_op) {
772 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
773 if (check_boolean_op) {
774 DCHECK(dst_type.IsInteger());
775 /* check vB with the call, then check the constant manually */
776 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
777 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
778 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
779 return;
780 }
781 }
782 SetRegisterType(dec_insn.vA_, dst_type);
783 }
784}
785
786void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
787 const RegType& reg_type = GetRegisterType(reg_idx);
788 if (!reg_type.IsReferenceTypes()) {
789 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800790 } else if (monitors_.size() >= 32) {
791 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 } else {
793 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700794 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 }
796}
797
798void RegisterLine::PopMonitor(uint32_t reg_idx) {
799 const RegType& reg_type = GetRegisterType(reg_idx);
800 if (!reg_type.IsReferenceTypes()) {
801 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
802 } else if (monitors_.empty()) {
803 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
804 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700805 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
807 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
808 // format "036" the constant collector may create unlocks on the same object but referenced
809 // via different registers.
810 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
811 : verifier_->LogVerifyInfo())
812 << "monitor-exit not unlocking the top of the monitor stack";
813 } else {
814 // Record the register was unlocked
815 ClearRegToLockDepth(reg_idx, monitors_.size());
816 }
817 }
818}
819
820bool RegisterLine::VerifyMonitorStackEmpty() {
821 if (MonitorStackDepth() != 0) {
822 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
823 return false;
824 } else {
825 return true;
826 }
827}
828
829bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
830 bool changed = false;
831 for (size_t idx = 0; idx < num_regs_; idx++) {
832 if (line_[idx] != incoming_line->line_[idx]) {
833 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
834 const RegType& cur_type = GetRegisterType(idx);
835 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
836 changed = changed || !cur_type.Equals(new_type);
837 line_[idx] = new_type.GetId();
838 }
839 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700840 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
842 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
843 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
844 for (uint32_t idx = 0; idx < num_regs_; idx++) {
845 size_t depths = reg_to_lock_depths_.count(idx);
846 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
847 if (depths != incoming_depths) {
848 if (depths == 0 || incoming_depths == 0) {
849 reg_to_lock_depths_.erase(idx);
850 } else {
851 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
852 << ": " << depths << " != " << incoming_depths;
853 break;
854 }
855 }
856 }
857 }
858 return changed;
859}
860
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800861void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 for (size_t i = 0; i < num_regs_; i += 8) {
863 uint8_t val = 0;
864 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
865 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700866 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 val |= 1 << j;
868 }
869 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800870 if ((i / 8) >= max_bytes) {
871 DCHECK_EQ(0, val);
872 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800874 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
875 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 }
877}
878
879std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700880 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 return os;
882}
883
884
885void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
886 uint32_t insns_size, uint16_t registers_size,
887 DexVerifier* verifier) {
888 DCHECK_GT(insns_size, 0U);
889
890 for (uint32_t i = 0; i < insns_size; i++) {
891 bool interesting = false;
892 switch (mode) {
893 case kTrackRegsAll:
894 interesting = flags[i].IsOpcode();
895 break;
896 case kTrackRegsGcPoints:
897 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
898 break;
899 case kTrackRegsBranches:
900 interesting = flags[i].IsBranchTarget();
901 break;
902 default:
903 break;
904 }
905 if (interesting) {
906 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
907 }
908 }
909}
910
911bool DexVerifier::VerifyClass(const Class* klass) {
jeffhaobdb76512011-09-07 11:43:16 -0700912 if (klass->IsVerified()) {
913 return true;
914 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800916 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
918 return false;
919 }
920 if (super != NULL) {
Ian Rogers672f5202012-01-12 18:06:40 -0800921 // Acquire lock to prevent races on verifying the super class
922 ObjectLock lock(super);
923
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (!super->IsVerified() && !super->IsErroneous()) {
925 Runtime::Current()->GetClassLinker()->VerifyClass(super);
926 }
927 if (!super->IsVerified()) {
928 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
929 << " that attempts to sub-class corrupt class " << PrettyClass(super);
930 return false;
931 } else if (super->IsFinal()) {
932 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
933 << " that attempts to sub-class final class " << PrettyClass(super);
934 return false;
935 }
936 }
jeffhaobdb76512011-09-07 11:43:16 -0700937 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
938 Method* method = klass->GetDirectMethod(i);
939 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
941 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700942 return false;
943 }
944 }
945 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
946 Method* method = klass->GetVirtualMethod(i);
947 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
949 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700950 return false;
951 }
952 }
953 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700954}
955
jeffhaobdb76512011-09-07 11:43:16 -0700956bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 DexVerifier verifier(method);
958 bool success = verifier.Verify();
Brian Carlstrom75412882012-01-18 01:26:54 -0800959 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
960
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 // We expect either success and no verification error, or failure and a generic failure to
962 // reject the class.
963 if (success) {
964 if (verifier.failure_ != VERIFY_ERROR_NONE) {
965 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
966 << verifier.fail_messages_;
967 }
968 } else {
969 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700970 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700971 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700972 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700973 verifier.Dump(std::cout);
974 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
976 }
977 return success;
978}
979
Shih-wei Liao371814f2011-10-27 16:52:10 -0700980void DexVerifier::VerifyMethodAndDump(Method* method) {
981 DexVerifier verifier(method);
982 verifier.Verify();
983
Elliott Hughese0918552011-10-28 17:18:29 -0700984 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
985 << verifier.fail_messages_.str() << std::endl
986 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700987}
988
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800989DexVerifier::DexVerifier(Method* method)
990 : work_insn_idx_(-1),
991 method_(method),
992 failure_(VERIFY_ERROR_NONE),
993 new_instance_count_(0),
994 monitor_enter_count_(0) {
995 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -0700996 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -0800997 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 dex_file_ = &class_linker->FindDexFile(dex_cache);
999 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001000}
1001
Ian Rogersd81871c2011-10-03 13:57:23 -07001002bool DexVerifier::Verify() {
1003 // If there aren't any instructions, make sure that's expected, then exit successfully.
1004 if (code_item_ == NULL) {
1005 if (!method_->IsNative() && !method_->IsAbstract()) {
1006 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001007 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 } else {
1009 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001010 }
jeffhaobdb76512011-09-07 11:43:16 -07001011 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1013 if (code_item_->ins_size_ > code_item_->registers_size_) {
1014 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1015 << " regs=" << code_item_->registers_size_;
1016 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001017 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 // Allocate and initialize an array to hold instruction data.
1019 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1020 // Run through the instructions and see if the width checks out.
1021 bool result = ComputeWidthsAndCountOps();
1022 // Flag instructions guarded by a "try" block and check exception handlers.
1023 result = result && ScanTryCatchBlocks();
1024 // Perform static instruction verification.
1025 result = result && VerifyInstructions();
1026 // Perform code flow analysis.
1027 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001028 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001029}
1030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031bool DexVerifier::ComputeWidthsAndCountOps() {
1032 const uint16_t* insns = code_item_->insns_;
1033 size_t insns_size = code_item_->insns_size_in_code_units_;
1034 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001035 size_t new_instance_count = 0;
1036 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001038
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001040 Instruction::Code opcode = inst->Opcode();
1041 if (opcode == Instruction::NEW_INSTANCE) {
1042 new_instance_count++;
1043 } else if (opcode == Instruction::MONITOR_ENTER) {
1044 monitor_enter_count++;
1045 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 size_t inst_size = inst->SizeInCodeUnits();
1047 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1048 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001049 inst = inst->Next();
1050 }
1051
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 if (dex_pc != insns_size) {
1053 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1054 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001055 return false;
1056 }
1057
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 new_instance_count_ = new_instance_count;
1059 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001060 return true;
1061}
1062
Ian Rogersd81871c2011-10-03 13:57:23 -07001063bool DexVerifier::ScanTryCatchBlocks() {
1064 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001065 if (tries_size == 0) {
1066 return true;
1067 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001069 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001070
1071 for (uint32_t idx = 0; idx < tries_size; idx++) {
1072 const DexFile::TryItem* try_item = &tries[idx];
1073 uint32_t start = try_item->start_addr_;
1074 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001075 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1077 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001078 return false;
1079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 if (!insn_flags_[start].IsOpcode()) {
1081 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001082 return false;
1083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 for (uint32_t dex_pc = start; dex_pc < end;
1085 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1086 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001087 }
1088 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001089 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001090 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001091 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001092 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001093 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001094 CatchHandlerIterator iterator(handlers_ptr);
1095 for (; iterator.HasNext(); iterator.Next()) {
1096 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 if (!insn_flags_[dex_pc].IsOpcode()) {
1098 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001099 return false;
1100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001102 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1103 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001104 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1105 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001106 if (exception_type == NULL) {
1107 DCHECK(Thread::Current()->IsExceptionPending());
1108 Thread::Current()->ClearException();
1109 }
1110 }
jeffhaobdb76512011-09-07 11:43:16 -07001111 }
Ian Rogers0571d352011-11-03 19:51:38 -07001112 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001113 }
jeffhaobdb76512011-09-07 11:43:16 -07001114 return true;
1115}
1116
Ian Rogersd81871c2011-10-03 13:57:23 -07001117bool DexVerifier::VerifyInstructions() {
1118 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001119
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 /* Flag the start of the method as a branch target. */
1121 insn_flags_[0].SetBranchTarget();
1122
1123 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1124 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1125 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001126 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1127 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 return false;
1129 }
1130 /* Flag instructions that are garbage collection points */
1131 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1132 insn_flags_[dex_pc].SetGcPoint();
1133 }
1134 dex_pc += inst->SizeInCodeUnits();
1135 inst = inst->Next();
1136 }
1137 return true;
1138}
1139
1140bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1141 Instruction::DecodedInstruction dec_insn(inst);
1142 bool result = true;
1143 switch (inst->GetVerifyTypeArgumentA()) {
1144 case Instruction::kVerifyRegA:
1145 result = result && CheckRegisterIndex(dec_insn.vA_);
1146 break;
1147 case Instruction::kVerifyRegAWide:
1148 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1149 break;
1150 }
1151 switch (inst->GetVerifyTypeArgumentB()) {
1152 case Instruction::kVerifyRegB:
1153 result = result && CheckRegisterIndex(dec_insn.vB_);
1154 break;
1155 case Instruction::kVerifyRegBField:
1156 result = result && CheckFieldIndex(dec_insn.vB_);
1157 break;
1158 case Instruction::kVerifyRegBMethod:
1159 result = result && CheckMethodIndex(dec_insn.vB_);
1160 break;
1161 case Instruction::kVerifyRegBNewInstance:
1162 result = result && CheckNewInstance(dec_insn.vB_);
1163 break;
1164 case Instruction::kVerifyRegBString:
1165 result = result && CheckStringIndex(dec_insn.vB_);
1166 break;
1167 case Instruction::kVerifyRegBType:
1168 result = result && CheckTypeIndex(dec_insn.vB_);
1169 break;
1170 case Instruction::kVerifyRegBWide:
1171 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1172 break;
1173 }
1174 switch (inst->GetVerifyTypeArgumentC()) {
1175 case Instruction::kVerifyRegC:
1176 result = result && CheckRegisterIndex(dec_insn.vC_);
1177 break;
1178 case Instruction::kVerifyRegCField:
1179 result = result && CheckFieldIndex(dec_insn.vC_);
1180 break;
1181 case Instruction::kVerifyRegCNewArray:
1182 result = result && CheckNewArray(dec_insn.vC_);
1183 break;
1184 case Instruction::kVerifyRegCType:
1185 result = result && CheckTypeIndex(dec_insn.vC_);
1186 break;
1187 case Instruction::kVerifyRegCWide:
1188 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1189 break;
1190 }
1191 switch (inst->GetVerifyExtraFlags()) {
1192 case Instruction::kVerifyArrayData:
1193 result = result && CheckArrayData(code_offset);
1194 break;
1195 case Instruction::kVerifyBranchTarget:
1196 result = result && CheckBranchTarget(code_offset);
1197 break;
1198 case Instruction::kVerifySwitchTargets:
1199 result = result && CheckSwitchTargets(code_offset);
1200 break;
1201 case Instruction::kVerifyVarArg:
1202 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1203 break;
1204 case Instruction::kVerifyVarArgRange:
1205 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1206 break;
1207 case Instruction::kVerifyError:
1208 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1209 result = false;
1210 break;
1211 }
1212 return result;
1213}
1214
1215bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1216 if (idx >= code_item_->registers_size_) {
1217 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1218 << code_item_->registers_size_ << ")";
1219 return false;
1220 }
1221 return true;
1222}
1223
1224bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1225 if (idx + 1 >= code_item_->registers_size_) {
1226 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1227 << "+1 >= " << code_item_->registers_size_ << ")";
1228 return false;
1229 }
1230 return true;
1231}
1232
1233bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1234 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1235 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1236 << dex_file_->GetHeader().field_ids_size_ << ")";
1237 return false;
1238 }
1239 return true;
1240}
1241
1242bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1243 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1244 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1245 << dex_file_->GetHeader().method_ids_size_ << ")";
1246 return false;
1247 }
1248 return true;
1249}
1250
1251bool DexVerifier::CheckNewInstance(uint32_t idx) {
1252 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1253 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1254 << dex_file_->GetHeader().type_ids_size_ << ")";
1255 return false;
1256 }
1257 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001258 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 if (descriptor[0] != 'L') {
1260 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1261 return false;
1262 }
1263 return true;
1264}
1265
1266bool DexVerifier::CheckStringIndex(uint32_t idx) {
1267 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1268 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1269 << dex_file_->GetHeader().string_ids_size_ << ")";
1270 return false;
1271 }
1272 return true;
1273}
1274
1275bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1276 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1277 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1278 << dex_file_->GetHeader().type_ids_size_ << ")";
1279 return false;
1280 }
1281 return true;
1282}
1283
1284bool DexVerifier::CheckNewArray(uint32_t idx) {
1285 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1286 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1287 << dex_file_->GetHeader().type_ids_size_ << ")";
1288 return false;
1289 }
1290 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001291 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 const char* cp = descriptor;
1293 while (*cp++ == '[') {
1294 bracket_count++;
1295 }
1296 if (bracket_count == 0) {
1297 /* The given class must be an array type. */
1298 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1299 return false;
1300 } else if (bracket_count > 255) {
1301 /* It is illegal to create an array of more than 255 dimensions. */
1302 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1303 return false;
1304 }
1305 return true;
1306}
1307
1308bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1309 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1310 const uint16_t* insns = code_item_->insns_ + cur_offset;
1311 const uint16_t* array_data;
1312 int32_t array_data_offset;
1313
1314 DCHECK_LT(cur_offset, insn_count);
1315 /* make sure the start of the array data table is in range */
1316 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1317 if ((int32_t) cur_offset + array_data_offset < 0 ||
1318 cur_offset + array_data_offset + 2 >= insn_count) {
1319 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1320 << ", data offset " << array_data_offset << ", count " << insn_count;
1321 return false;
1322 }
1323 /* offset to array data table is a relative branch-style offset */
1324 array_data = insns + array_data_offset;
1325 /* make sure the table is 32-bit aligned */
1326 if ((((uint32_t) array_data) & 0x03) != 0) {
1327 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1328 << ", data offset " << array_data_offset;
1329 return false;
1330 }
1331 uint32_t value_width = array_data[1];
1332 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1333 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1334 /* make sure the end of the switch is in range */
1335 if (cur_offset + array_data_offset + table_size > insn_count) {
1336 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1337 << ", data offset " << array_data_offset << ", end "
1338 << cur_offset + array_data_offset + table_size
1339 << ", count " << insn_count;
1340 return false;
1341 }
1342 return true;
1343}
1344
1345bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1346 int32_t offset;
1347 bool isConditional, selfOkay;
1348 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1349 return false;
1350 }
1351 if (!selfOkay && offset == 0) {
1352 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1353 return false;
1354 }
1355 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1356 // identical "wrap-around" behavior, but it's unwise to depend on that.
1357 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1358 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1359 return false;
1360 }
1361 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1362 int32_t abs_offset = cur_offset + offset;
1363 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1364 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1365 << (void*) abs_offset << ") at " << (void*) cur_offset;
1366 return false;
1367 }
1368 insn_flags_[abs_offset].SetBranchTarget();
1369 return true;
1370}
1371
1372bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1373 bool* selfOkay) {
1374 const uint16_t* insns = code_item_->insns_ + cur_offset;
1375 *pConditional = false;
1376 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001377 switch (*insns & 0xff) {
1378 case Instruction::GOTO:
1379 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 break;
1381 case Instruction::GOTO_32:
1382 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 *selfOkay = true;
1384 break;
1385 case Instruction::GOTO_16:
1386 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 break;
1388 case Instruction::IF_EQ:
1389 case Instruction::IF_NE:
1390 case Instruction::IF_LT:
1391 case Instruction::IF_GE:
1392 case Instruction::IF_GT:
1393 case Instruction::IF_LE:
1394 case Instruction::IF_EQZ:
1395 case Instruction::IF_NEZ:
1396 case Instruction::IF_LTZ:
1397 case Instruction::IF_GEZ:
1398 case Instruction::IF_GTZ:
1399 case Instruction::IF_LEZ:
1400 *pOffset = (int16_t) insns[1];
1401 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 break;
1403 default:
1404 return false;
1405 break;
1406 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001407 return true;
1408}
1409
Ian Rogersd81871c2011-10-03 13:57:23 -07001410bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1411 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001412 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1416 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1417 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1418 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001419 return false;
1420 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 /* make sure the table is 32-bit aligned */
1424 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1426 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001427 return false;
1428 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 uint32_t switch_count = switch_insns[1];
1430 int32_t keys_offset, targets_offset;
1431 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1433 /* 0=sig, 1=count, 2/3=firstKey */
1434 targets_offset = 4;
1435 keys_offset = -1;
1436 expected_signature = Instruction::kPackedSwitchSignature;
1437 } else {
1438 /* 0=sig, 1=count, 2..count*2 = keys */
1439 keys_offset = 2;
1440 targets_offset = 2 + 2 * switch_count;
1441 expected_signature = Instruction::kSparseSwitchSignature;
1442 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001444 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001445 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1446 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001447 return false;
1448 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001449 /* make sure the end of the switch is in range */
1450 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1452 << switch_offset << ", end "
1453 << (cur_offset + switch_offset + table_size)
1454 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 return false;
1456 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 /* for a sparse switch, verify the keys are in ascending order */
1458 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1460 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1462 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1463 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1465 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001466 return false;
1467 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001468 last_key = key;
1469 }
1470 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 for (uint32_t targ = 0; targ < switch_count; targ++) {
1473 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1474 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1475 int32_t abs_offset = cur_offset + offset;
1476 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1477 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1478 << (void*) abs_offset << ") at "
1479 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 return false;
1481 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 insn_flags_[abs_offset].SetBranchTarget();
1483 }
1484 return true;
1485}
1486
1487bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1488 if (vA > 5) {
1489 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1490 return false;
1491 }
1492 uint16_t registers_size = code_item_->registers_size_;
1493 for (uint32_t idx = 0; idx < vA; idx++) {
1494 if (arg[idx] > registers_size) {
1495 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1496 << ") in non-range invoke (> " << registers_size << ")";
1497 return false;
1498 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 }
1500
1501 return true;
1502}
1503
Ian Rogersd81871c2011-10-03 13:57:23 -07001504bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1505 uint16_t registers_size = code_item_->registers_size_;
1506 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1507 // integer overflow when adding them here.
1508 if (vA + vC > registers_size) {
1509 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1510 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001511 return false;
1512 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001513 return true;
1514}
1515
Brian Carlstrom75412882012-01-18 01:26:54 -08001516const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1517 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1518 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1519 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1520 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1521 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1522 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1523 gc_map.begin(),
1524 gc_map.end());
1525 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1526 DCHECK_EQ(gc_map.size(),
1527 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1528 (length_prefixed_gc_map->at(1) << 16) |
1529 (length_prefixed_gc_map->at(2) << 8) |
1530 (length_prefixed_gc_map->at(3) << 0)));
1531 return length_prefixed_gc_map;
1532}
1533
Ian Rogersd81871c2011-10-03 13:57:23 -07001534bool DexVerifier::VerifyCodeFlow() {
1535 uint16_t registers_size = code_item_->registers_size_;
1536 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001537
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 if (registers_size * insns_size > 4*1024*1024) {
1539 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1540 << " insns_size=" << insns_size << ")";
1541 }
1542 /* Create and initialize table holding register status */
1543 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1544 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001545
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 work_line_.reset(new RegisterLine(registers_size, this));
1547 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001548
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 /* Initialize register types of method arguments. */
1550 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001551 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1552 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 return false;
1554 }
1555 /* Perform code flow verification. */
1556 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001557 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
1560
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001562 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1563 if (map.get() == NULL) {
1564 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 return false; // Not a real failure, but a failure to encode
1566 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001567#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001568 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001569#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001570 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1571 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1572 verifier::DexVerifier::SetGcMap(ref, *gc_map);
1573 method_->SetGcMap(&gc_map->at(0));
jeffhaobdb76512011-09-07 11:43:16 -07001574 return true;
1575}
1576
Ian Rogersd81871c2011-10-03 13:57:23 -07001577void DexVerifier::Dump(std::ostream& os) {
1578 if (method_->IsNative()) {
1579 os << "Native method" << std::endl;
1580 return;
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 DCHECK(code_item_ != NULL);
1583 const Instruction* inst = Instruction::At(code_item_->insns_);
1584 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1585 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001586 os << StringPrintf("0x%04x", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
1587 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1589 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001590 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001592 inst = inst->Next();
1593 }
jeffhaobdb76512011-09-07 11:43:16 -07001594}
1595
Ian Rogersd81871c2011-10-03 13:57:23 -07001596static bool IsPrimitiveDescriptor(char descriptor) {
1597 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001598 case 'I':
1599 case 'C':
1600 case 'S':
1601 case 'B':
1602 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001603 case 'F':
1604 case 'D':
1605 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001607 default:
1608 return false;
1609 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001610}
1611
Ian Rogersd81871c2011-10-03 13:57:23 -07001612bool DexVerifier::SetTypesFromSignature() {
1613 RegisterLine* reg_line = reg_table_.GetLine(0);
1614 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1615 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001616
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1618 //Include the "this" pointer.
1619 size_t cur_arg = 0;
1620 if (!method_->IsStatic()) {
1621 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1622 // argument as uninitialized. This restricts field access until the superclass constructor is
1623 // called.
1624 Class* declaring_class = method_->GetDeclaringClass();
1625 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1626 reg_line->SetRegisterType(arg_start + cur_arg,
1627 reg_types_.UninitializedThisArgument(declaring_class));
1628 } else {
1629 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001630 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001632 }
1633
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001634 const DexFile::ProtoId& proto_id =
1635 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001636 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001637
1638 for (; iterator.HasNext(); iterator.Next()) {
1639 const char* descriptor = iterator.GetDescriptor();
1640 if (descriptor == NULL) {
1641 LOG(FATAL) << "Null descriptor";
1642 }
1643 if (cur_arg >= expected_args) {
1644 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1645 << " args, found more (" << descriptor << ")";
1646 return false;
1647 }
1648 switch (descriptor[0]) {
1649 case 'L':
1650 case '[':
1651 // We assume that reference arguments are initialized. The only way it could be otherwise
1652 // (assuming the caller was verified) is if the current method is <init>, but in that case
1653 // it's effectively considered initialized the instant we reach here (in the sense that we
1654 // can return without doing anything or call virtual methods).
1655 {
1656 const RegType& reg_type =
1657 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001658 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
1660 break;
1661 case 'Z':
1662 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1663 break;
1664 case 'C':
1665 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1666 break;
1667 case 'B':
1668 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1669 break;
1670 case 'I':
1671 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1672 break;
1673 case 'S':
1674 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1675 break;
1676 case 'F':
1677 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1678 break;
1679 case 'J':
1680 case 'D': {
1681 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1682 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1683 cur_arg++;
1684 break;
1685 }
1686 default:
1687 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1688 return false;
1689 }
1690 cur_arg++;
1691 }
1692 if (cur_arg != expected_args) {
1693 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1694 return false;
1695 }
1696 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1697 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1698 // format. Only major difference from the method argument format is that 'V' is supported.
1699 bool result;
1700 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1701 result = descriptor[1] == '\0';
1702 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1703 size_t i = 0;
1704 do {
1705 i++;
1706 } while (descriptor[i] == '['); // process leading [
1707 if (descriptor[i] == 'L') { // object array
1708 do {
1709 i++; // find closing ;
1710 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1711 result = descriptor[i] == ';';
1712 } else { // primitive array
1713 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1714 }
1715 } else if (descriptor[0] == 'L') {
1716 // could be more thorough here, but shouldn't be required
1717 size_t i = 0;
1718 do {
1719 i++;
1720 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1721 result = descriptor[i] == ';';
1722 } else {
1723 result = false;
1724 }
1725 if (!result) {
1726 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1727 << descriptor << "'";
1728 }
1729 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001730}
1731
Ian Rogersd81871c2011-10-03 13:57:23 -07001732bool DexVerifier::CodeFlowVerifyMethod() {
1733 const uint16_t* insns = code_item_->insns_;
1734 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001735
jeffhaobdb76512011-09-07 11:43:16 -07001736 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 insn_flags_[0].SetChanged();
1738 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001739
jeffhaobdb76512011-09-07 11:43:16 -07001740 /* Continue until no instructions are marked "changed". */
1741 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1743 uint32_t insn_idx = start_guess;
1744 for (; insn_idx < insns_size; insn_idx++) {
1745 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001746 break;
1747 }
jeffhaobdb76512011-09-07 11:43:16 -07001748 if (insn_idx == insns_size) {
1749 if (start_guess != 0) {
1750 /* try again, starting from the top */
1751 start_guess = 0;
1752 continue;
1753 } else {
1754 /* all flags are clear */
1755 break;
1756 }
1757 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 // We carry the working set of registers from instruction to instruction. If this address can
1759 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1760 // "changed" flags, we need to load the set of registers from the table.
1761 // Because we always prefer to continue on to the next instruction, we should never have a
1762 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1763 // target.
1764 work_insn_idx_ = insn_idx;
1765 if (insn_flags_[insn_idx].IsBranchTarget()) {
1766 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001767 } else {
1768#ifndef NDEBUG
1769 /*
1770 * Sanity check: retrieve the stored register line (assuming
1771 * a full table) and make sure it actually matches.
1772 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1774 if (register_line != NULL) {
1775 if (work_line_->CompareLine(register_line) != 0) {
1776 Dump(std::cout);
1777 std::cout << info_messages_.str();
1778 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1779 << "@" << (void*)work_insn_idx_ << std::endl
1780 << " work_line=" << *work_line_ << std::endl
1781 << " expected=" << *register_line;
1782 }
jeffhaobdb76512011-09-07 11:43:16 -07001783 }
1784#endif
1785 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 if (!CodeFlowVerifyInstruction(&start_guess)) {
1787 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001788 return false;
1789 }
jeffhaobdb76512011-09-07 11:43:16 -07001790 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 insn_flags_[insn_idx].SetVisited();
1792 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001793 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001794
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001796 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001797 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001798 * (besides the wasted space), but it indicates a flaw somewhere
1799 * down the line, possibly in the verifier.
1800 *
1801 * If we've substituted "always throw" instructions into the stream,
1802 * we are almost certainly going to have some dead code.
1803 */
1804 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 uint32_t insn_idx = 0;
1806 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001807 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001808 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001809 * may or may not be preceded by a padding NOP (for alignment).
1810 */
1811 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1812 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1813 insns[insn_idx] == Instruction::kArrayDataSignature ||
1814 (insns[insn_idx] == Instruction::NOP &&
1815 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1816 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1817 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001819 }
1820
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001822 if (dead_start < 0)
1823 dead_start = insn_idx;
1824 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001826 dead_start = -1;
1827 }
1828 }
1829 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001831 }
1832 }
jeffhaobdb76512011-09-07 11:43:16 -07001833 return true;
1834}
1835
Ian Rogersd81871c2011-10-03 13:57:23 -07001836bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001837#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001839 gDvm.verifierStats.instrsReexamined++;
1840 } else {
1841 gDvm.verifierStats.instrsExamined++;
1842 }
1843#endif
1844
1845 /*
1846 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001847 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001848 * control to another statement:
1849 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001850 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001851 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001852 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001853 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001854 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001855 * throw an exception that is handled by an encompassing "try"
1856 * block.
1857 *
1858 * We can also return, in which case there is no successor instruction
1859 * from this point.
1860 *
1861 * The behavior can be determined from the OpcodeFlags.
1862 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1864 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001865 Instruction::DecodedInstruction dec_insn(inst);
1866 int opcode_flag = inst->Flag();
1867
jeffhaobdb76512011-09-07 11:43:16 -07001868 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001869 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001870 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001872 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1873 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 }
jeffhaobdb76512011-09-07 11:43:16 -07001875
1876 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001877 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001878 * can throw an exception, we will copy/merge this into the "catch"
1879 * address rather than work_line, because we don't want the result
1880 * from the "successful" code path (e.g. a check-cast that "improves"
1881 * a type) to be visible to the exception handler.
1882 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1884 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001885 } else {
1886#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001888#endif
1889 }
1890
1891 switch (dec_insn.opcode_) {
1892 case Instruction::NOP:
1893 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001894 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001895 * a signature that looks like a NOP; if we see one of these in
1896 * the course of executing code then we have a problem.
1897 */
1898 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001900 }
1901 break;
1902
1903 case Instruction::MOVE:
1904 case Instruction::MOVE_FROM16:
1905 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001907 break;
1908 case Instruction::MOVE_WIDE:
1909 case Instruction::MOVE_WIDE_FROM16:
1910 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
1913 case Instruction::MOVE_OBJECT:
1914 case Instruction::MOVE_OBJECT_FROM16:
1915 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918
1919 /*
1920 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001921 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001922 * might want to hold the result in an actual CPU register, so the
1923 * Dalvik spec requires that these only appear immediately after an
1924 * invoke or filled-new-array.
1925 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001926 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001927 * redundant with the reset done below, but it can make the debug info
1928 * easier to read in some cases.)
1929 */
1930 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001932 break;
1933 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001935 break;
1936 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
1939
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001941 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 * This statement can only appear as the first instruction in an exception handler (though not
1943 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001944 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001945 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001946 const RegType& res_type = GetCaughtExceptionType();
1947 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1952 if (!GetMethodReturnType().IsUnknown()) {
1953 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1954 }
jeffhaobdb76512011-09-07 11:43:16 -07001955 }
1956 break;
1957 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001959 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 const RegType& return_type = GetMethodReturnType();
1961 if (!return_type.IsCategory1Types()) {
1962 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1963 } else {
1964 // Compilers may generate synthetic functions that write byte values into boolean fields.
1965 // Also, it may use integer values for boolean, byte, short, and character return types.
1966 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1967 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1968 ((return_type.IsBoolean() || return_type.IsByte() ||
1969 return_type.IsShort() || return_type.IsChar()) &&
1970 src_type.IsInteger()));
1971 /* check the register contents */
1972 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1973 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001974 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 }
1977 }
1978 break;
1979 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001981 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 const RegType& return_type = GetMethodReturnType();
1983 if (!return_type.IsCategory2Types()) {
1984 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1985 } else {
1986 /* check the register contents */
1987 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1988 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001989 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
jeffhaobdb76512011-09-07 11:43:16 -07001991 }
1992 }
1993 break;
1994 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1996 const RegType& return_type = GetMethodReturnType();
1997 if (!return_type.IsReferenceTypes()) {
1998 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
1999 } else {
2000 /* return_type is the *expected* return type, not register value */
2001 DCHECK(!return_type.IsZero());
2002 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002003 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2004 // Disallow returning uninitialized values and verify that the reference in vAA is an
2005 // instance of the "return_type"
2006 if (reg_type.IsUninitializedTypes()) {
2007 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2008 } else if (!return_type.IsAssignableFrom(reg_type)) {
2009 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2010 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002011 }
2012 }
2013 }
2014 break;
2015
2016 case Instruction::CONST_4:
2017 case Instruction::CONST_16:
2018 case Instruction::CONST:
2019 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::CONST_HIGH16:
2023 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 work_line_->SetRegisterType(dec_insn.vA_,
2025 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::CONST_WIDE_16:
2028 case Instruction::CONST_WIDE_32:
2029 case Instruction::CONST_WIDE:
2030 case Instruction::CONST_WIDE_HIGH16:
2031 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::CONST_STRING:
2035 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002039 // Get type from instruction if unresolved then we need an access check
2040 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2041 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2042 // Register holds class, ie its type is class, but on error we keep it Unknown
2043 work_line_->SetRegisterType(dec_insn.vA_,
2044 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 }
jeffhaobdb76512011-09-07 11:43:16 -07002047 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::MONITOR_EXIT:
2051 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002052 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002053 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002054 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002055 * to the need to handle asynchronous exceptions, a now-deprecated
2056 * feature that Dalvik doesn't support.)
2057 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002058 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002059 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002060 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002061 * structured locking checks are working, the former would have
2062 * failed on the -enter instruction, and the latter is impossible.
2063 *
2064 * This is fortunate, because issue 3221411 prevents us from
2065 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002067 * some catch blocks (which will show up as "dead" code when
2068 * we skip them here); if we can't, then the code path could be
2069 * "live" so we still need to check it.
2070 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 opcode_flag &= ~Instruction::kThrow;
2072 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074
Ian Rogers28ad40d2011-10-27 15:19:26 -07002075 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002077 /*
2078 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2079 * could be a "upcast" -- not expected, so we don't try to address it.)
2080 *
2081 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2082 * dec_insn.vA_ when branching to a handler.
2083 */
2084 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2085 const RegType& res_type =
2086 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002087 if (res_type.IsUnknown()) {
2088 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2089 break; // couldn't resolve class
2090 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002091 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2092 const RegType& orig_type =
2093 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2094 if (!res_type.IsNonZeroReferenceTypes()) {
2095 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2096 } else if (!orig_type.IsReferenceTypes()) {
2097 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002098 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002099 if (is_checkcast) {
2100 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002103 }
jeffhaobdb76512011-09-07 11:43:16 -07002104 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002105 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 }
2107 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002108 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2109 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002110 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002111 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 } else {
2113 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2114 }
2115 }
2116 break;
2117 }
2118 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002119 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2120 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2121 // can't create an instance of an interface or abstract class */
2122 if (!res_type.IsInstantiableTypes()) {
2123 Fail(VERIFY_ERROR_INSTANTIATION)
2124 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002126 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2127 // Any registers holding previous allocations from this address that have not yet been
2128 // initialized must be marked invalid.
2129 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2130 // add the new uninitialized reference to the register state
2131 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 }
2133 break;
2134 }
2135 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002136 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2137 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2138 if (!res_type.IsArrayClass()) {
2139 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002140 } else {
2141 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002143 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002144 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002145 }
2146 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002150 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2151 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2152 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002154 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002155 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002156 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002157 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002158 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002159 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002160 just_set_result = true;
2161 }
2162 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 }
jeffhaobdb76512011-09-07 11:43:16 -07002164 case Instruction::CMPL_FLOAT:
2165 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2167 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2168 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::CMPL_DOUBLE:
2171 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2173 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2174 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2178 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2179 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002182 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2183 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2184 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002185 }
2186 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 case Instruction::GOTO:
2189 case Instruction::GOTO_16:
2190 case Instruction::GOTO_32:
2191 /* no effect on or use of registers */
2192 break;
2193
2194 case Instruction::PACKED_SWITCH:
2195 case Instruction::SPARSE_SWITCH:
2196 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002198 break;
2199
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 case Instruction::FILL_ARRAY_DATA: {
2201 /* Similar to the verification done for APUT */
2202 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2203 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002204 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 if (res_class != NULL) {
2206 Class* component_type = res_class->GetComponentType();
2207 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2208 component_type->IsPrimitiveVoid()) {
2209 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002210 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 } else {
2212 const RegType& value_type = reg_types_.FromClass(component_type);
2213 DCHECK(!value_type.IsUnknown());
2214 // Now verify if the element width in the table matches the element width declared in
2215 // the array
2216 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2217 if (array_data[0] != Instruction::kArrayDataSignature) {
2218 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2219 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002220 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 // Since we don't compress the data in Dex, expect to see equal width of data stored
2222 // in the table and expected from the array class.
2223 if (array_data[1] != elem_width) {
2224 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2225 << " vs " << elem_width << ")";
2226 }
2227 }
2228 }
jeffhaobdb76512011-09-07 11:43:16 -07002229 }
2230 }
2231 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 }
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 case Instruction::IF_NE: {
2235 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2236 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2237 bool mismatch = false;
2238 if (reg_type1.IsZero()) { // zero then integral or reference expected
2239 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2240 } else if (reg_type1.IsReferenceTypes()) { // both references?
2241 mismatch = !reg_type2.IsReferenceTypes();
2242 } else { // both integral?
2243 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2244 }
2245 if (mismatch) {
2246 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2247 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002248 }
2249 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 }
jeffhaobdb76512011-09-07 11:43:16 -07002251 case Instruction::IF_LT:
2252 case Instruction::IF_GE:
2253 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 case Instruction::IF_LE: {
2255 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2256 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2257 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2258 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2259 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002260 }
2261 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 }
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 case Instruction::IF_NEZ: {
2265 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2266 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2267 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2268 }
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 }
jeffhaobdb76512011-09-07 11:43:16 -07002271 case Instruction::IF_LTZ:
2272 case Instruction::IF_GEZ:
2273 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 case Instruction::IF_LEZ: {
2275 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2276 if (!reg_type.IsIntegralTypes()) {
2277 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2278 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2279 }
jeffhaobdb76512011-09-07 11:43:16 -07002280 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 }
jeffhaobdb76512011-09-07 11:43:16 -07002282 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2284 break;
jeffhaobdb76512011-09-07 11:43:16 -07002285 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2287 break;
jeffhaobdb76512011-09-07 11:43:16 -07002288 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 VerifyAGet(dec_insn, reg_types_.Char(), true);
2290 break;
jeffhaobdb76512011-09-07 11:43:16 -07002291 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 case Instruction::AGET:
2295 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2296 break;
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 VerifyAGet(dec_insn, reg_types_.Long(), true);
2299 break;
2300 case Instruction::AGET_OBJECT:
2301 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 case Instruction::APUT_BOOLEAN:
2305 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2306 break;
2307 case Instruction::APUT_BYTE:
2308 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2309 break;
2310 case Instruction::APUT_CHAR:
2311 VerifyAPut(dec_insn, reg_types_.Char(), true);
2312 break;
2313 case Instruction::APUT_SHORT:
2314 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325
jeffhaobdb76512011-09-07 11:43:16 -07002326 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002327 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 break;
jeffhaobdb76512011-09-07 11:43:16 -07002329 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002330 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 break;
jeffhaobdb76512011-09-07 11:43:16 -07002332 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002333 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
jeffhaobdb76512011-09-07 11:43:16 -07002335 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002336 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 break;
2338 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002339 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002342 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002345 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002349 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 break;
2351 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002352 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 break;
2354 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002355 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 break;
2357 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002358 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002359 break;
2360 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002361 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002364 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002367 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369
jeffhaobdb76512011-09-07 11:43:16 -07002370 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002371 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 break;
jeffhaobdb76512011-09-07 11:43:16 -07002373 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002374 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 break;
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002377 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 break;
jeffhaobdb76512011-09-07 11:43:16 -07002379 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002380 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 break;
2382 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002383 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002386 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002389 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 break;
2391
2392 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002393 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 break;
2395 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002396 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 break;
2398 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002399 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
2401 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002402 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002405 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002408 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002411 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413
2414 case Instruction::INVOKE_VIRTUAL:
2415 case Instruction::INVOKE_VIRTUAL_RANGE:
2416 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 case Instruction::INVOKE_SUPER_RANGE: {
2418 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2419 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2420 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2421 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2422 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2423 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002424 const char* descriptor;
2425 if (called_method == NULL) {
2426 uint32_t method_idx = dec_insn.vB_;
2427 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2428 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002429 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002430 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002431 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002432 }
Ian Rogers9074b992011-10-26 17:41:55 -07002433 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002434 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002436 just_set_result = true;
2437 }
2438 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 }
jeffhaobdb76512011-09-07 11:43:16 -07002440 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 case Instruction::INVOKE_DIRECT_RANGE: {
2442 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2443 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2444 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002445 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 * Some additional checks when calling a constructor. We know from the invocation arg check
2447 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2448 * that to require that called_method->klass is the same as this->klass or this->super,
2449 * allowing the latter only if the "this" argument is the same as the "this" argument to
2450 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002451 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002452 bool is_constructor;
2453 if (called_method != NULL) {
2454 is_constructor = called_method->IsConstructor();
2455 } else {
2456 uint32_t method_idx = dec_insn.vB_;
2457 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2458 const char* name = dex_file_->GetMethodName(method_id);
2459 is_constructor = strcmp(name, "<init>") == 0;
2460 }
2461 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2463 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465
2466 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 if (this_type.IsZero()) {
2468 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 if (called_method != NULL) {
2472 Class* this_class = this_type.GetClass();
2473 DCHECK(this_class != NULL);
2474 /* must be in same class or in superclass */
2475 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2476 if (this_class != method_->GetDeclaringClass()) {
2477 Fail(VERIFY_ERROR_GENERIC)
2478 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2479 break;
2480 }
2481 } else if (called_method->GetDeclaringClass() != this_class) {
2482 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 }
jeffhaobdb76512011-09-07 11:43:16 -07002485 }
2486
2487 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002488 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2490 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 }
2493
2494 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002495 * Replace the uninitialized reference with an initialized one. We need to do this for all
2496 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002497 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 work_line_->MarkRefsAsInitialized(this_type);
2499 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002500 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002501 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002502 const char* descriptor;
2503 if (called_method == NULL) {
2504 uint32_t method_idx = dec_insn.vB_;
2505 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2506 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002507 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002508 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002509 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002510 }
Ian Rogers9074b992011-10-26 17:41:55 -07002511 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002512 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002514 just_set_result = true;
2515 }
2516 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 }
jeffhaobdb76512011-09-07 11:43:16 -07002518 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 case Instruction::INVOKE_STATIC_RANGE: {
2520 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2521 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2522 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002523 const char* descriptor;
2524 if (called_method == NULL) {
2525 uint32_t method_idx = dec_insn.vB_;
2526 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2527 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002528 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002529 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002530 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002531 }
Ian Rogers9074b992011-10-26 17:41:55 -07002532 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002533 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 work_line_->SetResultRegisterType(return_type);
2535 just_set_result = true;
2536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 }
2538 break;
2539 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 case Instruction::INVOKE_INTERFACE_RANGE: {
2541 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2542 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2543 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002544 if (abs_method != NULL) {
2545 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002546 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002547 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2548 << PrettyMethod(abs_method) << "'";
2549 break;
2550 }
2551 }
2552 /* Get the type of the "this" arg, which should either be a sub-interface of called
2553 * interface or Object (see comments in RegType::JoinClass).
2554 */
2555 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2556 if (failure_ == VERIFY_ERROR_NONE) {
2557 if (this_type.IsZero()) {
2558 /* null pointer always passes (and always fails at runtime) */
2559 } else {
2560 if (this_type.IsUninitializedTypes()) {
2561 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2562 << this_type;
2563 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002565 // In the past we have tried to assert that "called_interface" is assignable
2566 // from "this_type.GetClass()", however, as we do an imprecise Join
2567 // (RegType::JoinClass) we don't have full information on what interfaces are
2568 // implemented by "this_type". For example, two classes may implement the same
2569 // interfaces and have a common parent that doesn't implement the interface. The
2570 // join will set "this_type" to the parent class and a test that this implements
2571 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002572 }
2573 }
jeffhaobdb76512011-09-07 11:43:16 -07002574 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 * We don't have an object instance, so we can't find the concrete method. However, all of
2576 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002577 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002578 const char* descriptor;
2579 if (abs_method == NULL) {
2580 uint32_t method_idx = dec_insn.vB_;
2581 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2582 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002583 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002584 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002585 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002586 }
Ian Rogers9074b992011-10-26 17:41:55 -07002587 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002588 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2589 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002591 just_set_result = true;
2592 }
2593 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 }
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::NEG_INT:
2596 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::NEG_LONG:
2600 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654
2655 case Instruction::ADD_INT:
2656 case Instruction::SUB_INT:
2657 case Instruction::MUL_INT:
2658 case Instruction::REM_INT:
2659 case Instruction::DIV_INT:
2660 case Instruction::SHL_INT:
2661 case Instruction::SHR_INT:
2662 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::AND_INT:
2666 case Instruction::OR_INT:
2667 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::ADD_LONG:
2671 case Instruction::SUB_LONG:
2672 case Instruction::MUL_LONG:
2673 case Instruction::DIV_LONG:
2674 case Instruction::REM_LONG:
2675 case Instruction::AND_LONG:
2676 case Instruction::OR_LONG:
2677 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::SHL_LONG:
2681 case Instruction::SHR_LONG:
2682 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 /* shift distance is Int, making these different from other binary operations */
2684 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::ADD_FLOAT:
2687 case Instruction::SUB_FLOAT:
2688 case Instruction::MUL_FLOAT:
2689 case Instruction::DIV_FLOAT:
2690 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
2693 case Instruction::ADD_DOUBLE:
2694 case Instruction::SUB_DOUBLE:
2695 case Instruction::MUL_DOUBLE:
2696 case Instruction::DIV_DOUBLE:
2697 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::ADD_INT_2ADDR:
2701 case Instruction::SUB_INT_2ADDR:
2702 case Instruction::MUL_INT_2ADDR:
2703 case Instruction::REM_INT_2ADDR:
2704 case Instruction::SHL_INT_2ADDR:
2705 case Instruction::SHR_INT_2ADDR:
2706 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709 case Instruction::AND_INT_2ADDR:
2710 case Instruction::OR_INT_2ADDR:
2711 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002713 break;
2714 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717 case Instruction::ADD_LONG_2ADDR:
2718 case Instruction::SUB_LONG_2ADDR:
2719 case Instruction::MUL_LONG_2ADDR:
2720 case Instruction::DIV_LONG_2ADDR:
2721 case Instruction::REM_LONG_2ADDR:
2722 case Instruction::AND_LONG_2ADDR:
2723 case Instruction::OR_LONG_2ADDR:
2724 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
2727 case Instruction::SHL_LONG_2ADDR:
2728 case Instruction::SHR_LONG_2ADDR:
2729 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::ADD_FLOAT_2ADDR:
2733 case Instruction::SUB_FLOAT_2ADDR:
2734 case Instruction::MUL_FLOAT_2ADDR:
2735 case Instruction::DIV_FLOAT_2ADDR:
2736 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::ADD_DOUBLE_2ADDR:
2740 case Instruction::SUB_DOUBLE_2ADDR:
2741 case Instruction::MUL_DOUBLE_2ADDR:
2742 case Instruction::DIV_DOUBLE_2ADDR:
2743 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::ADD_INT_LIT16:
2747 case Instruction::RSUB_INT:
2748 case Instruction::MUL_INT_LIT16:
2749 case Instruction::DIV_INT_LIT16:
2750 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::AND_INT_LIT16:
2754 case Instruction::OR_INT_LIT16:
2755 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::ADD_INT_LIT8:
2759 case Instruction::RSUB_INT_LIT8:
2760 case Instruction::MUL_INT_LIT8:
2761 case Instruction::DIV_INT_LIT8:
2762 case Instruction::REM_INT_LIT8:
2763 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002764 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002765 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::AND_INT_LIT8:
2769 case Instruction::OR_INT_LIT8:
2770 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773
2774 /*
2775 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002776 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002777 * inserted in the course of verification, we can expect to see it here.
2778 */
jeffhaob4df5142011-09-19 20:25:32 -07002779 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002780 break;
2781
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002783 case Instruction::UNUSED_EE:
2784 case Instruction::UNUSED_EF:
2785 case Instruction::UNUSED_F2:
2786 case Instruction::UNUSED_F3:
2787 case Instruction::UNUSED_F4:
2788 case Instruction::UNUSED_F5:
2789 case Instruction::UNUSED_F6:
2790 case Instruction::UNUSED_F7:
2791 case Instruction::UNUSED_F8:
2792 case Instruction::UNUSED_F9:
2793 case Instruction::UNUSED_FA:
2794 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::UNUSED_F0:
2796 case Instruction::UNUSED_F1:
2797 case Instruction::UNUSED_E3:
2798 case Instruction::UNUSED_E8:
2799 case Instruction::UNUSED_E7:
2800 case Instruction::UNUSED_E4:
2801 case Instruction::UNUSED_E9:
2802 case Instruction::UNUSED_FC:
2803 case Instruction::UNUSED_E5:
2804 case Instruction::UNUSED_EA:
2805 case Instruction::UNUSED_FD:
2806 case Instruction::UNUSED_E6:
2807 case Instruction::UNUSED_EB:
2808 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002809 case Instruction::UNUSED_3E:
2810 case Instruction::UNUSED_3F:
2811 case Instruction::UNUSED_40:
2812 case Instruction::UNUSED_41:
2813 case Instruction::UNUSED_42:
2814 case Instruction::UNUSED_43:
2815 case Instruction::UNUSED_73:
2816 case Instruction::UNUSED_79:
2817 case Instruction::UNUSED_7A:
2818 case Instruction::UNUSED_EC:
2819 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002820 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002821 break;
2822
2823 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002824 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002825 * complain if an instruction is missing (which is desirable).
2826 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002828
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 if (failure_ != VERIFY_ERROR_NONE) {
2830 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002831 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002832 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002833 return false;
2834 } else {
2835 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002836 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002838 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002840 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002842 opcode_flag = Instruction::kThrow;
2843 }
2844 }
jeffhaobdb76512011-09-07 11:43:16 -07002845 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 * If we didn't just set the result register, clear it out. This ensures that you can only use
2847 * "move-result" immediately after the result is set. (We could check this statically, but it's
2848 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002849 */
2850 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002852 }
2853
jeffhaoa0a764a2011-09-16 10:43:38 -07002854 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002855 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2857 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2858 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002859 return false;
2860 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2862 // next instruction isn't one.
2863 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002864 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 }
2866 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2867 if (next_line != NULL) {
2868 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2869 // needed.
2870 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002871 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 }
jeffhaobdb76512011-09-07 11:43:16 -07002873 } else {
2874 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 * We're not recording register data for the next instruction, so we don't know what the prior
2876 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002877 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002879 }
2880 }
2881
2882 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002883 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002884 *
2885 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002886 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002887 * somebody could get a reference field, check it for zero, and if the
2888 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002889 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002890 * that, and will reject the code.
2891 *
2892 * TODO: avoid re-fetching the branch target
2893 */
2894 if ((opcode_flag & Instruction::kBranch) != 0) {
2895 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002897 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002899 return false;
2900 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002901 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002903 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 }
jeffhaobdb76512011-09-07 11:43:16 -07002905 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002907 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 }
jeffhaobdb76512011-09-07 11:43:16 -07002909 }
2910
2911 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002912 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002913 *
2914 * We've already verified that the table is structurally sound, so we
2915 * just need to walk through and tag the targets.
2916 */
2917 if ((opcode_flag & Instruction::kSwitch) != 0) {
2918 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2919 const uint16_t* switch_insns = insns + offset_to_switch;
2920 int switch_count = switch_insns[1];
2921 int offset_to_targets, targ;
2922
2923 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2924 /* 0 = sig, 1 = count, 2/3 = first key */
2925 offset_to_targets = 4;
2926 } else {
2927 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002928 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002929 offset_to_targets = 2 + 2 * switch_count;
2930 }
2931
2932 /* verify each switch target */
2933 for (targ = 0; targ < switch_count; targ++) {
2934 int offset;
2935 uint32_t abs_offset;
2936
2937 /* offsets are 32-bit, and only partly endian-swapped */
2938 offset = switch_insns[offset_to_targets + targ * 2] |
2939 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 abs_offset = work_insn_idx_ + offset;
2941 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2942 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002943 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 }
2945 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002946 return false;
2947 }
2948 }
2949
2950 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2952 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002953 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2955 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002956 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002957
Ian Rogers0571d352011-11-03 19:51:38 -07002958 for (; iterator.HasNext(); iterator.Next()) {
2959 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 within_catch_all = true;
2961 }
jeffhaobdb76512011-09-07 11:43:16 -07002962 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2964 * "work_regs", because at runtime the exception will be thrown before the instruction
2965 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002966 */
Ian Rogers0571d352011-11-03 19:51:38 -07002967 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002968 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 }
jeffhaobdb76512011-09-07 11:43:16 -07002970 }
2971
2972 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2974 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002975 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002977 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002978 * The state in work_line reflects the post-execution state. If the current instruction is a
2979 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002980 * it will do so before grabbing the lock).
2981 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2983 Fail(VERIFY_ERROR_GENERIC)
2984 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002985 return false;
2986 }
2987 }
2988 }
2989
jeffhaod1f0fde2011-09-08 17:25:33 -07002990 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 if ((opcode_flag & Instruction::kReturn) != 0) {
2992 if(!work_line_->VerifyMonitorStackEmpty()) {
2993 return false;
2994 }
jeffhaobdb76512011-09-07 11:43:16 -07002995 }
2996
2997 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002998 * Update start_guess. Advance to the next instruction of that's
2999 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003000 * neither of those exists we're in a return or throw; leave start_guess
3001 * alone and let the caller sort it out.
3002 */
3003 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003005 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3006 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003008 }
3009
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3011 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003012
3013 return true;
3014}
3015
Ian Rogers28ad40d2011-10-27 15:19:26 -07003016const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003017 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003018 Class* referrer = method_->GetDeclaringClass();
3019 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3020 const RegType& result =
3021 klass != NULL ? reg_types_.FromClass(klass)
3022 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3023 if (klass == NULL && !result.IsUnresolvedTypes()) {
3024 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003026 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3027 // check at runtime if access is allowed and so pass here.
3028 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3029 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003030 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003031 << result << "'";
3032 return reg_types_.Unknown();
3033 } else {
3034 return result;
3035 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003036}
3037
Ian Rogers28ad40d2011-10-27 15:19:26 -07003038const RegType& DexVerifier::GetCaughtExceptionType() {
3039 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003041 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3043 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003044 CatchHandlerIterator iterator(handlers_ptr);
3045 for (; iterator.HasNext(); iterator.Next()) {
3046 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3047 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003048 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003050 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3052 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3053 * test, so is essentially harmless.
3054 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003055 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3056 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3057 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003059 common_super = &exception;
3060 } else if (common_super->Equals(exception)) {
3061 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003063 common_super = &common_super->Merge(exception, &reg_types_);
3064 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 }
3066 }
3067 }
3068 }
Ian Rogers0571d352011-11-03 19:51:38 -07003069 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 }
3071 }
3072 if (common_super == NULL) {
3073 /* no catch blocks, or no catches with classes we can find */
3074 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3075 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003076 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003077}
3078
3079Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003080 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3081 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3082 if (failure_ != VERIFY_ERROR_NONE) {
3083 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3084 return NULL;
3085 }
3086 if(klass_type.IsUnresolvedTypes()) {
3087 return NULL; // Can't resolve Class so no more to do here
3088 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 Class* referrer = method_->GetDeclaringClass();
3090 DexCache* dex_cache = referrer->GetDexCache();
3091 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3092 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003093 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003094 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003095 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003096 if (is_direct) {
3097 res_method = klass->FindDirectMethod(name, signature);
3098 } else if (klass->IsInterface()) {
3099 res_method = klass->FindInterfaceMethod(name, signature);
3100 } else {
3101 res_method = klass->FindVirtualMethod(name, signature);
3102 }
3103 if (res_method != NULL) {
3104 dex_cache->SetResolvedMethod(method_idx, res_method);
3105 } else {
3106 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003107 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 << " " << signature;
3109 return NULL;
3110 }
3111 }
3112 /* Check if access is allowed. */
3113 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3114 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003115 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 return NULL;
3117 }
3118 return res_method;
3119}
3120
3121Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3122 MethodType method_type, bool is_range, bool is_super) {
3123 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3124 // we're making.
3125 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3126 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003127 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 return NULL;
3129 }
3130 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3131 // enforce them here.
3132 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3133 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3134 << PrettyMethod(res_method);
3135 return NULL;
3136 }
3137 // See if the method type implied by the invoke instruction matches the access flags for the
3138 // target method.
3139 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3140 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3141 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3142 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003143 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3144 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003145 return NULL;
3146 }
3147 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3148 // has a vtable entry for the target method.
3149 if (is_super) {
3150 DCHECK(method_type == METHOD_VIRTUAL);
3151 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3152 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3153 if (super == NULL) { // Only Object has no super class
3154 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3155 << " to super " << PrettyMethod(res_method);
3156 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003157 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003159 << " to super " << PrettyDescriptor(super)
3160 << "." << mh.GetName()
3161 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 }
3163 return NULL;
3164 }
3165 }
3166 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3167 // match the call to the signature. Also, we might might be calling through an abstract method
3168 // definition (which doesn't have register count values).
3169 int expected_args = dec_insn.vA_;
3170 /* caught by static verifier */
3171 DCHECK(is_range || expected_args <= 5);
3172 if (expected_args > code_item_->outs_size_) {
3173 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3174 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3175 return NULL;
3176 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003177 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 if (sig[0] != '(') {
3179 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3180 << " as descriptor doesn't start with '(': " << sig;
3181 return NULL;
3182 }
jeffhaobdb76512011-09-07 11:43:16 -07003183 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 * Check the "this" argument, which must be an instance of the class
3185 * that declared the method. For an interface class, we don't do the
3186 * full interface merge, so we can't do a rigorous check here (which
3187 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003188 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 int actual_args = 0;
3190 if (!res_method->IsStatic()) {
3191 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3192 if (failure_ != VERIFY_ERROR_NONE) {
3193 return NULL;
3194 }
3195 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3196 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3197 return NULL;
3198 }
3199 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003200 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3201 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3202 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3203 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 return NULL;
3205 }
3206 }
3207 actual_args++;
3208 }
3209 /*
3210 * Process the target method's signature. This signature may or may not
3211 * have been verified, so we can't assume it's properly formed.
3212 */
3213 size_t sig_offset = 0;
3214 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3215 if (actual_args >= expected_args) {
3216 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3217 << "'. Expected " << expected_args << " args, found more ("
3218 << sig.substr(sig_offset) << ")";
3219 return NULL;
3220 }
3221 std::string descriptor;
3222 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3223 size_t end;
3224 if (sig[sig_offset] == 'L') {
3225 end = sig.find(';', sig_offset);
3226 } else {
3227 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3228 if (sig[end] == 'L') {
3229 end = sig.find(';', end);
3230 }
3231 }
3232 if (end == std::string::npos) {
3233 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3234 << "bad signature component '" << sig << "' (missing ';')";
3235 return NULL;
3236 }
3237 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3238 sig_offset = end;
3239 } else {
3240 descriptor = sig[sig_offset];
3241 }
3242 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003243 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3244 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003245 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3246 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3247 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 }
3249 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3250 }
3251 if (sig[sig_offset] != ')') {
3252 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3253 return NULL;
3254 }
3255 if (actual_args != expected_args) {
3256 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3257 << " expected " << expected_args << " args, found " << actual_args;
3258 return NULL;
3259 } else {
3260 return res_method;
3261 }
3262}
3263
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003264const RegType& DexVerifier::GetMethodReturnType() {
3265 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3266 MethodHelper(method_).GetReturnTypeDescriptor());
3267}
3268
Ian Rogersd81871c2011-10-03 13:57:23 -07003269void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3270 const RegType& insn_type, bool is_primitive) {
3271 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3272 if (!index_type.IsArrayIndexTypes()) {
3273 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3274 } else {
3275 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3276 if (failure_ == VERIFY_ERROR_NONE) {
3277 if (array_class == NULL) {
3278 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3279 // instruction type. TODO: have a proper notion of bottom here.
3280 if (!is_primitive || insn_type.IsCategory1Types()) {
3281 // Reference or category 1
3282 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3283 } else {
3284 // Category 2
3285 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3286 }
3287 } else {
3288 /* verify the class */
3289 Class* component_class = array_class->GetComponentType();
3290 const RegType& component_type = reg_types_.FromClass(component_class);
3291 if (!array_class->IsArrayClass()) {
3292 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003293 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 } else if (component_class->IsPrimitive() && !is_primitive) {
3295 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003296 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 << " source for aget-object";
3298 } else if (!component_class->IsPrimitive() && is_primitive) {
3299 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003300 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 << " source for category 1 aget";
3302 } else if (is_primitive && !insn_type.Equals(component_type) &&
3303 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3304 (insn_type.IsLong() && component_type.IsDouble()))) {
3305 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003306 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 << " incompatible with aget of type " << insn_type;
3308 } else {
3309 // Use knowledge of the field type which is stronger than the type inferred from the
3310 // instruction, which can't differentiate object types and ints from floats, longs from
3311 // doubles.
3312 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3313 }
3314 }
3315 }
3316 }
3317}
3318
3319void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3320 const RegType& insn_type, bool is_primitive) {
3321 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3322 if (!index_type.IsArrayIndexTypes()) {
3323 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3324 } else {
3325 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3326 if (failure_ == VERIFY_ERROR_NONE) {
3327 if (array_class == NULL) {
3328 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3329 // instruction type.
3330 } else {
3331 /* verify the class */
3332 Class* component_class = array_class->GetComponentType();
3333 const RegType& component_type = reg_types_.FromClass(component_class);
3334 if (!array_class->IsArrayClass()) {
3335 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003336 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 } else if (component_class->IsPrimitive() && !is_primitive) {
3338 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003339 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 << " source for aput-object";
3341 } else if (!component_class->IsPrimitive() && is_primitive) {
3342 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003343 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 << " source for category 1 aput";
3345 } else if (is_primitive && !insn_type.Equals(component_type) &&
3346 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3347 (insn_type.IsLong() && component_type.IsDouble()))) {
3348 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003349 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 << " incompatible with aput of type " << insn_type;
3351 } else {
3352 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003353 // Note: we use the instruction type (rather than the component type) for aput-object as
3354 // incompatible classes will be caught at runtime as an array store exception
3355 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 }
3357 }
3358 }
3359 }
3360}
3361
3362Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003363 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3364 // Check access to class
3365 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3366 if (failure_ != VERIFY_ERROR_NONE) {
3367 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3368 << dex_file_->GetFieldName(field_id) << ") in "
3369 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3370 return NULL;
3371 }
3372 if(klass_type.IsUnresolvedTypes()) {
3373 return NULL; // Can't resolve Class so no more to do here
3374 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003375 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003377 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3378 << dex_file_->GetFieldName(field_id) << ") in "
3379 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 DCHECK(Thread::Current()->IsExceptionPending());
3381 Thread::Current()->ClearException();
3382 return NULL;
3383 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3384 field->GetAccessFlags())) {
3385 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3386 << " from " << PrettyClass(method_->GetDeclaringClass());
3387 return NULL;
3388 } else if (!field->IsStatic()) {
3389 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3390 return NULL;
3391 } else {
3392 return field;
3393 }
3394}
3395
Ian Rogersd81871c2011-10-03 13:57:23 -07003396Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003397 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3398 // Check access to class
3399 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3400 if (failure_ != VERIFY_ERROR_NONE) {
3401 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3402 << dex_file_->GetFieldName(field_id) << ") in "
3403 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3404 return NULL;
3405 }
3406 if(klass_type.IsUnresolvedTypes()) {
3407 return NULL; // Can't resolve Class so no more to do here
3408 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003409 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003411 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3412 << dex_file_->GetFieldName(field_id) << ") in "
3413 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 DCHECK(Thread::Current()->IsExceptionPending());
3415 Thread::Current()->ClearException();
3416 return NULL;
3417 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3418 field->GetAccessFlags())) {
3419 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3420 << " from " << PrettyClass(method_->GetDeclaringClass());
3421 return NULL;
3422 } else if (field->IsStatic()) {
3423 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3424 << " to not be static";
3425 return NULL;
3426 } else if (obj_type.IsZero()) {
3427 // Cannot infer and check type, however, access will cause null pointer exception
3428 return field;
3429 } else if(obj_type.IsUninitializedReference() &&
3430 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3431 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3432 // Field accesses through uninitialized references are only allowable for constructors where
3433 // the field is declared in this class
3434 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3435 << " of a not fully initialized object within the context of "
3436 << PrettyMethod(method_);
3437 return NULL;
3438 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3439 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3440 // of C1. For resolution to occur the declared class of the field must be compatible with
3441 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3442 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3443 << " from object of type " << PrettyClass(obj_type.GetClass());
3444 return NULL;
3445 } else {
3446 return field;
3447 }
3448}
3449
Ian Rogersb94a27b2011-10-26 00:33:41 -07003450void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3451 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003452 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003453 Field* field;
3454 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003455 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003456 } else {
3457 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003458 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003459 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003460 if (failure_ != VERIFY_ERROR_NONE) {
3461 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3462 } else {
3463 const char* descriptor;
3464 const ClassLoader* loader;
3465 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003466 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003467 loader = field->GetDeclaringClass()->GetClassLoader();
3468 } else {
3469 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3470 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3471 loader = method_->GetDeclaringClass()->GetClassLoader();
3472 }
3473 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003475 if (field_type.Equals(insn_type) ||
3476 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3477 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 // expected that read is of the correct primitive type or that int reads are reading
3479 // floats or long reads are reading doubles
3480 } else {
3481 // This is a global failure rather than a class change failure as the instructions and
3482 // the descriptors for the type should have been consistent within the same file at
3483 // compile time
3484 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003485 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003486 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 return;
3488 }
3489 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003490 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003492 << " to be compatible with type '" << insn_type
3493 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003494 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 return;
3496 }
3497 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003498 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 }
3500}
3501
Ian Rogersb94a27b2011-10-26 00:33:41 -07003502void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3503 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003504 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 Field* field;
3506 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003507 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003508 } else {
3509 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003510 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003511 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003512 if (failure_ != VERIFY_ERROR_NONE) {
3513 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3514 } else {
3515 const char* descriptor;
3516 const ClassLoader* loader;
3517 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003518 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003519 loader = field->GetDeclaringClass()->GetClassLoader();
3520 } else {
3521 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3522 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3523 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003525 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3526 if (field != NULL) {
3527 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3528 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3529 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3530 return;
3531 }
3532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003533 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003534 // Primitive field assignability rules are weaker than regular assignability rules
3535 bool instruction_compatible;
3536 bool value_compatible;
3537 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3538 if (field_type.IsIntegralTypes()) {
3539 instruction_compatible = insn_type.IsIntegralTypes();
3540 value_compatible = value_type.IsIntegralTypes();
3541 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003542 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003543 value_compatible = value_type.IsFloatTypes();
3544 } else if (field_type.IsLong()) {
3545 instruction_compatible = insn_type.IsLong();
3546 value_compatible = value_type.IsLongTypes();
3547 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003548 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003549 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003551 instruction_compatible = false; // reference field with primitive store
3552 value_compatible = false; // unused
3553 }
3554 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 // This is a global failure rather than a class change failure as the instructions and
3556 // the descriptors for the type should have been consistent within the same file at
3557 // compile time
3558 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003559 << " to be of type '" << insn_type
3560 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003561 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 return;
3563 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003564 if (!value_compatible) {
3565 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3566 << " of type " << value_type
3567 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003568 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003569 return;
3570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003572 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003574 << " to be compatible with type '" << insn_type
3575 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003576 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003577 return;
3578 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003579 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 }
3582}
3583
3584bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3585 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3586 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3587 return false;
3588 }
3589 return true;
3590}
3591
3592void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003593 const RegType& res_type, bool is_range) {
3594 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003595 /*
3596 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3597 * list and fail. It's legal, if silly, for arg_count to be zero.
3598 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003599 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3600 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 uint32_t arg_count = dec_insn.vA_;
3602 for (size_t ui = 0; ui < arg_count; ui++) {
3603 uint32_t get_reg;
3604
3605 if (is_range)
3606 get_reg = dec_insn.vC_ + ui;
3607 else
3608 get_reg = dec_insn.arg_[ui];
3609
3610 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3611 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3612 << ") not valid";
3613 return;
3614 }
3615 }
3616}
3617
3618void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003619 if (Runtime::Current()->IsStarted()) {
3620 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3621 << " " << fail_messages_.str();
3622 return;
3623 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003624 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3625 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3626 VerifyErrorRefType ref_type;
3627 switch (inst->Opcode()) {
3628 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003629 case Instruction::CHECK_CAST:
3630 case Instruction::INSTANCE_OF:
3631 case Instruction::NEW_INSTANCE:
3632 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003633 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003634 case Instruction::FILLED_NEW_ARRAY_RANGE:
3635 ref_type = VERIFY_ERROR_REF_CLASS;
3636 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003637 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003638 case Instruction::IGET_BOOLEAN:
3639 case Instruction::IGET_BYTE:
3640 case Instruction::IGET_CHAR:
3641 case Instruction::IGET_SHORT:
3642 case Instruction::IGET_WIDE:
3643 case Instruction::IGET_OBJECT:
3644 case Instruction::IPUT:
3645 case Instruction::IPUT_BOOLEAN:
3646 case Instruction::IPUT_BYTE:
3647 case Instruction::IPUT_CHAR:
3648 case Instruction::IPUT_SHORT:
3649 case Instruction::IPUT_WIDE:
3650 case Instruction::IPUT_OBJECT:
3651 case Instruction::SGET:
3652 case Instruction::SGET_BOOLEAN:
3653 case Instruction::SGET_BYTE:
3654 case Instruction::SGET_CHAR:
3655 case Instruction::SGET_SHORT:
3656 case Instruction::SGET_WIDE:
3657 case Instruction::SGET_OBJECT:
3658 case Instruction::SPUT:
3659 case Instruction::SPUT_BOOLEAN:
3660 case Instruction::SPUT_BYTE:
3661 case Instruction::SPUT_CHAR:
3662 case Instruction::SPUT_SHORT:
3663 case Instruction::SPUT_WIDE:
3664 case Instruction::SPUT_OBJECT:
3665 ref_type = VERIFY_ERROR_REF_FIELD;
3666 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003667 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003668 case Instruction::INVOKE_VIRTUAL_RANGE:
3669 case Instruction::INVOKE_SUPER:
3670 case Instruction::INVOKE_SUPER_RANGE:
3671 case Instruction::INVOKE_DIRECT:
3672 case Instruction::INVOKE_DIRECT_RANGE:
3673 case Instruction::INVOKE_STATIC:
3674 case Instruction::INVOKE_STATIC_RANGE:
3675 case Instruction::INVOKE_INTERFACE:
3676 case Instruction::INVOKE_INTERFACE_RANGE:
3677 ref_type = VERIFY_ERROR_REF_METHOD;
3678 break;
jeffhaobdb76512011-09-07 11:43:16 -07003679 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003680 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003681 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003682 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3684 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3685 // instruction, so assert it.
3686 size_t width = inst->SizeInCodeUnits();
3687 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003688 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003689 // NOPs
3690 for (size_t i = 2; i < width; i++) {
3691 insns[work_insn_idx_ + i] = Instruction::NOP;
3692 }
3693 // Encode the opcode, with the failure code in the high byte
3694 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3695 (failure_ << 8) | // AA - component
3696 (ref_type << (8 + kVerifyErrorRefTypeShift));
3697 insns[work_insn_idx_] = new_instruction;
3698 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3699 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003700 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3701 << fail_messages_.str();
3702 if (gDebugVerify) {
3703 std::cout << std::endl << info_messages_.str();
3704 Dump(std::cout);
3705 }
jeffhaobdb76512011-09-07 11:43:16 -07003706}
jeffhaoba5ebb92011-08-25 17:24:37 -07003707
Ian Rogersd81871c2011-10-03 13:57:23 -07003708bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3709 const bool merge_debug = true;
3710 bool changed = true;
3711 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3712 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003713 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 * We haven't processed this instruction before, and we haven't touched the registers here, so
3715 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3716 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003717 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003718 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003719 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003720 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3721 copy->CopyFromLine(target_line);
3722 changed = target_line->MergeRegisters(merge_line);
3723 if (failure_ != VERIFY_ERROR_NONE) {
3724 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003725 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003726 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3728 << *copy.get() << " MERGE" << std::endl
3729 << *merge_line << " ==" << std::endl
3730 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003731 }
3732 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 if (changed) {
3734 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003735 }
3736 return true;
3737}
3738
Ian Rogersd81871c2011-10-03 13:57:23 -07003739void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3740 size_t* log2_max_gc_pc) {
3741 size_t local_gc_points = 0;
3742 size_t max_insn = 0;
3743 size_t max_ref_reg = -1;
3744 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3745 if (insn_flags_[i].IsGcPoint()) {
3746 local_gc_points++;
3747 max_insn = i;
3748 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003749 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003750 }
3751 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003752 *gc_points = local_gc_points;
3753 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3754 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003755 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 i++;
3757 }
3758 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003759}
3760
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003761const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 size_t num_entries, ref_bitmap_bits, pc_bits;
3763 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3764 // There's a single byte to encode the size of each bitmap
3765 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3766 // TODO: either a better GC map format or per method failures
3767 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3768 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003769 return NULL;
3770 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003771 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3772 // There are 2 bytes to encode the number of entries
3773 if (num_entries >= 65536) {
3774 // TODO: either a better GC map format or per method failures
3775 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3776 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003777 return NULL;
3778 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003780 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003781 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003782 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003784 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003785 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003787 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 // TODO: either a better GC map format or per method failures
3789 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3790 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3791 return NULL;
3792 }
3793 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003794 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 if (table == NULL) {
3796 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3797 return NULL;
3798 }
3799 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003800 table->push_back(format);
3801 table->push_back(ref_bitmap_bytes);
3802 table->push_back(num_entries & 0xFF);
3803 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003804 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3806 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003807 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003809 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003810 }
3811 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003812 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003813 }
3814 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003815 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 return table;
3817}
jeffhaoa0a764a2011-09-16 10:43:38 -07003818
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003819void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3821 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003822 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 size_t map_index = 0;
3824 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3825 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3826 if (insn_flags_[i].IsGcPoint()) {
3827 CHECK_LT(map_index, map.NumEntries());
3828 CHECK_EQ(map.GetPC(map_index), i);
3829 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3830 map_index++;
3831 RegisterLine* line = reg_table_.GetLine(i);
3832 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003833 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 CHECK_LT(j / 8, map.RegWidth());
3835 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3836 } else if ((j / 8) < map.RegWidth()) {
3837 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3838 } else {
3839 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3840 }
3841 }
3842 } else {
3843 CHECK(reg_bitmap == NULL);
3844 }
3845 }
3846}
jeffhaoa0a764a2011-09-16 10:43:38 -07003847
Ian Rogersd81871c2011-10-03 13:57:23 -07003848const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3849 size_t num_entries = NumEntries();
3850 // Do linear or binary search?
3851 static const size_t kSearchThreshold = 8;
3852 if (num_entries < kSearchThreshold) {
3853 for (size_t i = 0; i < num_entries; i++) {
3854 if (GetPC(i) == dex_pc) {
3855 return GetBitMap(i);
3856 }
3857 }
3858 } else {
3859 int lo = 0;
3860 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003861 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 int mid = (hi + lo) / 2;
3863 int mid_pc = GetPC(mid);
3864 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003865 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003866 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003867 hi = mid - 1;
3868 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003870 }
3871 }
3872 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 if (error_if_not_present) {
3874 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3875 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003876 return NULL;
3877}
3878
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003879DexVerifier::GcMapTable DexVerifier::gc_maps_;
3880
3881void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003882 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3883 if (existing_gc_map != NULL) {
3884 CHECK(*existing_gc_map == gc_map);
3885 delete existing_gc_map;
3886 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003887 gc_maps_[ref] = &gc_map;
3888 CHECK(GetGcMap(ref) != NULL);
3889}
3890
3891const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3892 GcMapTable::const_iterator it = gc_maps_.find(ref);
3893 if (it == gc_maps_.end()) {
3894 return NULL;
3895 }
3896 CHECK(it->second != NULL);
3897 return it->second;
3898}
3899
3900void DexVerifier::DeleteGcMaps() {
3901 STLDeleteValues(&gc_maps_);
3902}
3903
Ian Rogersd81871c2011-10-03 13:57:23 -07003904} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003905} // namespace art