blob: a5ab8cdefce21dfab473c8ff0e40a71e03931197 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070027#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070028#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070030#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Logan Chienfca7e872011-12-20 20:08:22 +080035#if defined(ART_USE_LLVM_COMPILER)
36#include "compiler_llvm/backend_types.h"
37#include "compiler_llvm/inferred_reg_category_map.h"
38using namespace art::compiler_llvm;
39#endif
40
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070042namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Ian Rogers2c8a8572011-10-24 17:11:36 -070044static const bool gDebugVerify = false;
45
Ian Rogersd81871c2011-10-03 13:57:23 -070046std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
Brian Carlstrom75412882012-01-18 01:26:54 -080047 switch (rhs) {
48 case VERIFY_ERROR_NONE: os << "VERIFY_ERROR_NONE"; break;
jeffhaod5347e02012-03-22 17:25:05 -070049 case VERIFY_ERROR_BAD_CLASS_HARD: os << "VERIFY_ERROR_BAD_CLASS_HARD"; break;
50 case VERIFY_ERROR_BAD_CLASS_SOFT: os << "VERIFY_ERROR_BAD_CLASS_SOFT"; break;
Brian Carlstrom75412882012-01-18 01:26:54 -080051 case VERIFY_ERROR_NO_CLASS: os << "VERIFY_ERROR_NO_CLASS"; break;
52 case VERIFY_ERROR_NO_FIELD: os << "VERIFY_ERROR_NO_FIELD"; break;
53 case VERIFY_ERROR_NO_METHOD: os << "VERIFY_ERROR_NO_METHOD"; break;
54 case VERIFY_ERROR_ACCESS_CLASS: os << "VERIFY_ERROR_ACCESS_CLASS"; break;
55 case VERIFY_ERROR_ACCESS_FIELD: os << "VERIFY_ERROR_ACCESS_FIELD"; break;
56 case VERIFY_ERROR_ACCESS_METHOD: os << "VERIFY_ERROR_ACCESS_METHOD"; break;
57 case VERIFY_ERROR_CLASS_CHANGE: os << "VERIFY_ERROR_CLASS_CHANGE"; break;
58 case VERIFY_ERROR_INSTANTIATION: os << "VERIFY_ERROR_INSTANTIATION"; break;
59 default:
60 os << "VerifyError[" << static_cast<int>(rhs) << "]";
61 break;
62 }
63 return os;
Ian Rogersd81871c2011-10-03 13:57:23 -070064}
jeffhaobdb76512011-09-07 11:43:16 -070065
Ian Rogers84fa0742011-10-25 18:13:30 -070066static const char* type_strings[] = {
67 "Unknown",
68 "Conflict",
69 "Boolean",
70 "Byte",
71 "Short",
72 "Char",
73 "Integer",
74 "Float",
75 "Long (Low Half)",
76 "Long (High Half)",
77 "Double (Low Half)",
78 "Double (High Half)",
79 "64-bit Constant (Low Half)",
80 "64-bit Constant (High Half)",
81 "32-bit Constant",
82 "Unresolved Reference",
83 "Uninitialized Reference",
84 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070085 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070086 "Reference",
87};
Ian Rogersd81871c2011-10-03 13:57:23 -070088
Ian Rogers2c8a8572011-10-24 17:11:36 -070089std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070090 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
91 std::string result;
92 if (IsConstant()) {
93 uint32_t val = ConstantValue();
94 if (val == 0) {
95 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070096 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070097 if(IsConstantShort()) {
98 result = StringPrintf("32-bit Constant: %d", val);
99 } else {
100 result = StringPrintf("32-bit Constant: 0x%x", val);
101 }
102 }
103 } else {
104 result = type_strings[type_];
105 if (IsReferenceTypes()) {
106 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -0700107 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700108 result += PrettyDescriptor(GetDescriptor());
109 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -0700111 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700112 }
113 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700114 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -0700115}
116
117const RegType& RegType::HighHalf(RegTypeCache* cache) const {
118 CHECK(IsLowHalf());
119 if (type_ == kRegTypeLongLo) {
120 return cache->FromType(kRegTypeLongHi);
121 } else if (type_ == kRegTypeDoubleLo) {
122 return cache->FromType(kRegTypeDoubleHi);
123 } else {
124 return cache->FromType(kRegTypeConstHi);
125 }
126}
127
128/*
129 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
130 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
131 * 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
132 * is the deepest (lowest upper bound) parent of S and T).
133 *
134 * This operation applies for regular classes and arrays, however, for interface types there needn't
135 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
136 * introducing sets of types, however, the only operation permissible on an interface is
137 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
138 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700139 * the perversion of any Object being assignable to an interface type (note, however, that we don't
140 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
141 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700142 */
143Class* RegType::ClassJoin(Class* s, Class* t) {
144 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
145 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
146 if (s == t) {
147 return s;
148 } else if (s->IsAssignableFrom(t)) {
149 return s;
150 } else if (t->IsAssignableFrom(s)) {
151 return t;
152 } else if (s->IsArrayClass() && t->IsArrayClass()) {
153 Class* s_ct = s->GetComponentType();
154 Class* t_ct = t->GetComponentType();
155 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
156 // Given the types aren't the same, if either array is of primitive types then the only
157 // common parent is java.lang.Object
158 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
159 DCHECK(result->IsObjectClass());
160 return result;
161 }
162 Class* common_elem = ClassJoin(s_ct, t_ct);
163 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
164 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800165 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700167 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
168 DCHECK(array_class != NULL);
169 return array_class;
170 } else {
171 size_t s_depth = s->Depth();
172 size_t t_depth = t->Depth();
173 // Get s and t to the same depth in the hierarchy
174 if (s_depth > t_depth) {
175 while (s_depth > t_depth) {
176 s = s->GetSuperClass();
177 s_depth--;
178 }
179 } else {
180 while (t_depth > s_depth) {
181 t = t->GetSuperClass();
182 t_depth--;
183 }
184 }
185 // Go up the hierarchy until we get to the common parent
186 while (s != t) {
187 s = s->GetSuperClass();
188 t = t->GetSuperClass();
189 }
190 return s;
191 }
192}
193
Ian Rogersb5e95b92011-10-25 23:28:55 -0700194bool RegType::IsAssignableFrom(const RegType& src) const {
195 if (Equals(src)) {
196 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700197 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700198 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700199 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
200 case RegType::kRegTypeByte: return src.IsByteTypes();
201 case RegType::kRegTypeShort: return src.IsShortTypes();
202 case RegType::kRegTypeChar: return src.IsCharTypes();
203 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
204 case RegType::kRegTypeFloat: return src.IsFloatTypes();
205 case RegType::kRegTypeLongLo: return src.IsLongTypes();
206 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700207 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700208 if (!IsReferenceTypes()) {
209 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700210 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700211 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700212 return true; // all reference types can be assigned null
213 } else if (!src.IsReferenceTypes()) {
214 return false; // expect src to be a reference type
215 } else if (IsJavaLangObject()) {
216 return true; // all reference types can be assigned to Object
217 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700218 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers5d86e522012-02-01 11:45:24 -0800219 } else if (IsJavaLangObjectArray()) {
Ian Rogers89310de2012-02-01 13:47:30 -0800220 return src.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
Ian Rogers9074b992011-10-26 17:41:55 -0700221 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700222 GetClass()->IsAssignableFrom(src.GetClass())) {
223 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700224 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700225 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700226 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700227 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700228 }
229 }
230}
231
Ian Rogers84fa0742011-10-25 18:13:30 -0700232static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
233 return a.IsConstant() ? b : a;
234}
jeffhaobdb76512011-09-07 11:43:16 -0700235
Ian Rogersd81871c2011-10-03 13:57:23 -0700236const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
237 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700238 if (IsUnknown() && incoming_type.IsUnknown()) {
239 return *this; // Unknown MERGE Unknown => Unknown
240 } else if (IsConflict()) {
241 return *this; // Conflict MERGE * => Conflict
242 } else if (incoming_type.IsConflict()) {
243 return incoming_type; // * MERGE Conflict => Conflict
244 } else if (IsUnknown() || incoming_type.IsUnknown()) {
245 return reg_types->Conflict(); // Unknown MERGE * => Conflict
246 } else if(IsConstant() && incoming_type.IsConstant()) {
247 int32_t val1 = ConstantValue();
248 int32_t val2 = incoming_type.ConstantValue();
249 if (val1 >= 0 && val2 >= 0) {
250 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
251 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700252 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700253 } else {
254 return incoming_type;
255 }
256 } else if (val1 < 0 && val2 < 0) {
257 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
258 if (val1 <= val2) {
259 return *this;
260 } else {
261 return incoming_type;
262 }
263 } else {
264 // Values are +ve and -ve, choose smallest signed type in which they both fit
265 if (IsConstantByte()) {
266 if (incoming_type.IsConstantByte()) {
267 return reg_types->ByteConstant();
268 } else if (incoming_type.IsConstantShort()) {
269 return reg_types->ShortConstant();
270 } else {
271 return reg_types->IntConstant();
272 }
273 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700274 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700275 return reg_types->ShortConstant();
276 } else {
277 return reg_types->IntConstant();
278 }
279 } else {
280 return reg_types->IntConstant();
281 }
282 }
283 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
284 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
285 return reg_types->Boolean(); // boolean MERGE boolean => boolean
286 }
287 if (IsByteTypes() && incoming_type.IsByteTypes()) {
288 return reg_types->Byte(); // byte MERGE byte => byte
289 }
290 if (IsShortTypes() && incoming_type.IsShortTypes()) {
291 return reg_types->Short(); // short MERGE short => short
292 }
293 if (IsCharTypes() && incoming_type.IsCharTypes()) {
294 return reg_types->Char(); // char MERGE char => char
295 }
296 return reg_types->Integer(); // int MERGE * => int
297 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
298 (IsLongTypes() && incoming_type.IsLongTypes()) ||
299 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
300 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
301 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
302 // check constant case was handled prior to entry
303 DCHECK(!IsConstant() || !incoming_type.IsConstant());
304 // float/long/double MERGE float/long/double_constant => float/long/double
305 return SelectNonConstant(*this, incoming_type);
306 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700307 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700308 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700309 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
310 return reg_types->JavaLangObject(); // Object MERGE ref => Object
311 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
312 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
313 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
314 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700315 return reg_types->Conflict();
316 } else { // Two reference types, compute Join
317 Class* c1 = GetClass();
318 Class* c2 = incoming_type.GetClass();
319 DCHECK(c1 != NULL && !c1->IsPrimitive());
320 DCHECK(c2 != NULL && !c2->IsPrimitive());
321 Class* join_class = ClassJoin(c1, c2);
322 if (c1 == join_class) {
323 return *this;
324 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 return incoming_type;
326 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700327 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700328 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700330 } else {
331 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700332 }
333}
334
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700335static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700336 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700337 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
338 case Primitive::kPrimByte: return RegType::kRegTypeByte;
339 case Primitive::kPrimShort: return RegType::kRegTypeShort;
340 case Primitive::kPrimChar: return RegType::kRegTypeChar;
341 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
342 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
343 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
344 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
345 case Primitive::kPrimVoid:
346 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700347 }
348}
349
350static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
351 if (descriptor.length() == 1) {
352 switch (descriptor[0]) {
353 case 'Z': return RegType::kRegTypeBoolean;
354 case 'B': return RegType::kRegTypeByte;
355 case 'S': return RegType::kRegTypeShort;
356 case 'C': return RegType::kRegTypeChar;
357 case 'I': return RegType::kRegTypeInteger;
358 case 'J': return RegType::kRegTypeLongLo;
359 case 'F': return RegType::kRegTypeFloat;
360 case 'D': return RegType::kRegTypeDoubleLo;
361 case 'V':
362 default: return RegType::kRegTypeUnknown;
363 }
364 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
365 return RegType::kRegTypeReference;
366 } else {
367 return RegType::kRegTypeUnknown;
368 }
369}
370
371std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700372 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 return os;
374}
375
376const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800377 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700378 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
379}
380
381const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800382 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700383 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700384 // entries should be sized greater than primitive types
385 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
386 RegType* entry = entries_[type];
387 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700388 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800389 if (strlen(descriptor) != 0) {
390 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700391 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700392 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 entries_[type] = entry;
394 }
395 return *entry;
396 } else {
397 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700399 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700401 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800402 if (cur_entry->IsReference()) {
403 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800404 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405 return *cur_entry;
406 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700407 } else if (cur_entry->IsUnresolvedReference() &&
408 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 return *cur_entry;
410 }
411 }
Ian Rogers672297c2012-01-10 14:50:55 -0800412 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700413 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700414 // Able to resolve so create resolved register type
415 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700416 entries_.push_back(entry);
417 return *entry;
418 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700419 // TODO: we assume unresolved, but we may be able to do better by validating whether the
420 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700421 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700422 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700423 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800424 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700425 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800426 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700427 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
428 entries_.size());
429 entries_.push_back(entry);
430 return *entry;
431 } else {
432 // The descriptor is broken return the unknown type as there's nothing sensible that
433 // could be done at runtime
434 return Unknown();
435 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 }
438}
439
440const RegType& RegTypeCache::FromClass(Class* klass) {
441 if (klass->IsPrimitive()) {
442 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
443 // entries should be sized greater than primitive types
444 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
445 RegType* entry = entries_[type];
446 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700447 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700448 entries_[type] = entry;
449 }
450 return *entry;
451 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700452 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700453 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700454 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700455 return *cur_entry;
456 }
457 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700458 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 entries_.push_back(entry);
460 return *entry;
461 }
462}
463
Ian Rogers28ad40d2011-10-27 15:19:26 -0700464const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
465 RegType* entry;
466 if (type.IsUnresolvedTypes()) {
467 String* descriptor = type.GetDescriptor();
468 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
469 RegType* cur_entry = entries_[i];
470 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
471 cur_entry->GetAllocationPc() == allocation_pc &&
472 cur_entry->GetDescriptor() == descriptor) {
473 return *cur_entry;
474 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700475 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700476 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
477 descriptor, allocation_pc, entries_.size());
478 } else {
479 Class* klass = type.GetClass();
480 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
481 RegType* cur_entry = entries_[i];
482 if (cur_entry->IsUninitializedReference() &&
483 cur_entry->GetAllocationPc() == allocation_pc &&
484 cur_entry->GetClass() == klass) {
485 return *cur_entry;
486 }
487 }
488 entry = new RegType(RegType::kRegTypeUninitializedReference,
489 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700491 entries_.push_back(entry);
492 return *entry;
493}
494
495const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
496 RegType* entry;
497 if (uninit_type.IsUnresolvedTypes()) {
498 String* descriptor = uninit_type.GetDescriptor();
499 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
500 RegType* cur_entry = entries_[i];
501 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
502 return *cur_entry;
503 }
504 }
505 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
506 } else {
507 Class* klass = uninit_type.GetClass();
508 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
509 RegType* cur_entry = entries_[i];
510 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
511 return *cur_entry;
512 }
513 }
514 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
515 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 entries_.push_back(entry);
517 return *entry;
518}
519
520const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700521 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 RegType* cur_entry = entries_[i];
523 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
524 return *cur_entry;
525 }
526 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700527 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 entries_.size());
529 entries_.push_back(entry);
530 return *entry;
531}
532
533const RegType& RegTypeCache::FromType(RegType::Type type) {
534 CHECK(type < RegType::kRegTypeReference);
535 switch (type) {
536 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
537 case RegType::kRegTypeByte: return From(type, NULL, "B");
538 case RegType::kRegTypeShort: return From(type, NULL, "S");
539 case RegType::kRegTypeChar: return From(type, NULL, "C");
540 case RegType::kRegTypeInteger: return From(type, NULL, "I");
541 case RegType::kRegTypeFloat: return From(type, NULL, "F");
542 case RegType::kRegTypeLongLo:
543 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
544 case RegType::kRegTypeDoubleLo:
545 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
546 default: return From(type, NULL, "");
547 }
548}
549
550const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700551 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
552 RegType* cur_entry = entries_[i];
553 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
554 return *cur_entry;
555 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700557 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
558 entries_.push_back(entry);
559 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700560}
561
Ian Rogers28ad40d2011-10-27 15:19:26 -0700562const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
Ian Rogers89310de2012-02-01 13:47:30 -0800563 CHECK(array.IsArrayTypes());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700564 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800565 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
566 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800567 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700568 } else {
569 return FromClass(array.GetClass()->GetComponentType());
570 }
571}
572
573
Ian Rogersd81871c2011-10-03 13:57:23 -0700574bool RegisterLine::CheckConstructorReturn() const {
575 for (size_t i = 0; i < num_regs_; i++) {
576 if (GetRegisterType(i).IsUninitializedThisReference()) {
jeffhaod5347e02012-03-22 17:25:05 -0700577 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT)
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 << "Constructor returning without calling superclass constructor";
579 return false;
580 }
581 }
582 return true;
583}
584
jeffhao60f83e32012-02-13 17:16:30 -0800585bool RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 DCHECK(vdst < num_regs_);
587 if (new_type.IsLowHalf()) {
588 line_[vdst] = new_type.GetId();
589 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
590 } else if (new_type.IsHighHalf()) {
591 /* should never set these explicitly */
jeffhaod5347e02012-03-22 17:25:05 -0700592 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Explicit set of high register type";
jeffhao60f83e32012-02-13 17:16:30 -0800593 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 } else if (new_type.IsConflict()) { // should only be set during a merge
jeffhaod5347e02012-03-22 17:25:05 -0700595 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Set register to unknown type " << new_type;
jeffhao60f83e32012-02-13 17:16:30 -0800596 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 } else {
598 line_[vdst] = new_type.GetId();
599 }
600 // Clear the monitor entry bits for this register.
601 ClearAllRegToLockDepths(vdst);
jeffhao60f83e32012-02-13 17:16:30 -0800602 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700603}
604
605void RegisterLine::SetResultTypeToUnknown() {
606 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
607 result_[0] = unknown_id;
608 result_[1] = unknown_id;
609}
610
611void RegisterLine::SetResultRegisterType(const RegType& new_type) {
612 result_[0] = new_type.GetId();
613 if(new_type.IsLowHalf()) {
614 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
615 result_[1] = new_type.GetId() + 1;
616 } else {
617 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
618 }
619}
620
621const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
622 // The register index was validated during the static pass, so we don't need to check it here.
623 DCHECK_LT(vsrc, num_regs_);
624 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
625}
626
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627const RegType& RegisterLine::GetInvocationThis(const DecodedInstruction& dec_insn) {
628 if (dec_insn.vA < 1) {
jeffhaod5347e02012-03-22 17:25:05 -0700629 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke lacks 'this'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 return verifier_->GetRegTypeCache()->Unknown();
631 }
632 /* get the element type of the array held in vsrc */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800633 const RegType& this_type = GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (!this_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700635 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "tried to get class from non-reference register v"
636 << dec_insn.vC << " (type=" << this_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 return verifier_->GetRegTypeCache()->Unknown();
638 }
639 return this_type;
640}
641
Ian Rogersd81871c2011-10-03 13:57:23 -0700642bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
643 // Verify the src register type against the check type refining the type of the register
644 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700645 if (!check_type.IsAssignableFrom(src_type)) {
jeffhaod5347e02012-03-22 17:25:05 -0700646 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "register v" << vsrc << " has type " << src_type
647 << " but expected " << check_type;
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 return false;
649 }
jeffhao457cc512012-02-02 16:55:13 -0800650 if (check_type.IsLowHalf()) {
651 const RegType& src_type_h = GetRegisterType(vsrc + 1);
652 if (!src_type.CheckWidePair(src_type_h)) {
jeffhaod5347e02012-03-22 17:25:05 -0700653 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "wide register v" << vsrc << " has type "
654 << src_type << "/" << src_type_h;
jeffhao457cc512012-02-02 16:55:13 -0800655 return false;
656 }
657 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
659 // precise than the subtype in vsrc so leave it for reference types. For primitive types
660 // if they are a defined type then they are as precise as we can get, however, for constant
661 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
662 return true;
663}
664
665void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700666 DCHECK(uninit_type.IsUninitializedTypes());
667 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
668 size_t changed = 0;
669 for (size_t i = 0; i < num_regs_; i++) {
670 if (GetRegisterType(i).Equals(uninit_type)) {
671 line_[i] = init_type.GetId();
672 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700675 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676}
677
678void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
679 for (size_t i = 0; i < num_regs_; i++) {
680 if (GetRegisterType(i).Equals(uninit_type)) {
681 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
682 ClearAllRegToLockDepths(i);
683 }
684 }
685}
686
687void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
688 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
689 const RegType& type = GetRegisterType(vsrc);
jeffhao60f83e32012-02-13 17:16:30 -0800690 if (!SetRegisterType(vdst, type)) {
691 return;
692 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
694 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
jeffhaod5347e02012-03-22 17:25:05 -0700695 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
696 << " cat=" << static_cast<int>(cat);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 } else if (cat == kTypeCategoryRef) {
698 CopyRegToLockDepth(vdst, vsrc);
699 }
700}
701
702void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
703 const RegType& type_l = GetRegisterType(vsrc);
704 const RegType& type_h = GetRegisterType(vsrc + 1);
705
706 if (!type_l.CheckWidePair(type_h)) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy2 v" << vdst << "<-v" << vsrc
708 << " type=" << type_l << "/" << type_h;
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 } else {
710 SetRegisterType(vdst, type_l); // implicitly sets the second half
711 }
712}
713
714void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
715 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
716 if ((!is_reference && !type.IsCategory1Types()) ||
717 (is_reference && !type.IsReferenceTypes())) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
720 } else {
721 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
722 SetRegisterType(vdst, type);
723 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
724 }
725}
726
727/*
728 * Implement "move-result-wide". Copy the category-2 value from the result
729 * register to another register, and reset the result register.
730 */
731void RegisterLine::CopyResultRegister2(uint32_t vdst) {
732 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
733 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
734 if (!type_l.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -0700735 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
737 } else {
738 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
739 SetRegisterType(vdst, type_l); // also sets the high
740 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
741 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
742 }
743}
744
Elliott Hughesadb8c672012-03-06 16:49:32 -0800745void RegisterLine::CheckUnaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 const RegType& dst_type, const RegType& src_type) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800747 if (VerifyRegisterType(dec_insn.vB, src_type)) {
748 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 }
750}
751
Elliott Hughesadb8c672012-03-06 16:49:32 -0800752void RegisterLine::CheckBinaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 const RegType& dst_type,
754 const RegType& src_type1, const RegType& src_type2,
755 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800756 if (VerifyRegisterType(dec_insn.vB, src_type1) &&
757 VerifyRegisterType(dec_insn.vC, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 if (check_boolean_op) {
759 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800760 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
761 GetRegisterType(dec_insn.vC).IsBooleanTypes()) {
762 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 return;
764 }
765 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800766 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 }
768}
769
Elliott Hughesadb8c672012-03-06 16:49:32 -0800770void RegisterLine::CheckBinaryOp2addr(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 const RegType& dst_type, const RegType& src_type1,
772 const RegType& src_type2, bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800773 if (VerifyRegisterType(dec_insn.vA, src_type1) &&
774 VerifyRegisterType(dec_insn.vB, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 if (check_boolean_op) {
776 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800777 if (GetRegisterType(dec_insn.vA).IsBooleanTypes() &&
778 GetRegisterType(dec_insn.vB).IsBooleanTypes()) {
779 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 return;
781 }
782 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800783 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 }
785}
786
Elliott Hughesadb8c672012-03-06 16:49:32 -0800787void RegisterLine::CheckLiteralOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 const RegType& dst_type, const RegType& src_type,
789 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800790 if (VerifyRegisterType(dec_insn.vB, src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 if (check_boolean_op) {
792 DCHECK(dst_type.IsInteger());
793 /* check vB with the call, then check the constant manually */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800794 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
795 (dec_insn.vC == 0 || dec_insn.vC == 1)) {
796 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return;
798 }
799 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800800 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 }
802}
803
804void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
805 const RegType& reg_type = GetRegisterType(reg_idx);
806 if (!reg_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700807 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800808 } else if (monitors_.size() >= 32) {
jeffhaod5347e02012-03-22 17:25:05 -0700809 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 } else {
811 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700812 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 }
814}
815
816void RegisterLine::PopMonitor(uint32_t reg_idx) {
817 const RegType& reg_type = GetRegisterType(reg_idx);
818 if (!reg_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700819 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit on non-object (" << reg_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 } else if (monitors_.empty()) {
jeffhaod5347e02012-03-22 17:25:05 -0700821 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit stack underflow";
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700823 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
825 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
826 // format "036" the constant collector may create unlocks on the same object but referenced
827 // via different registers.
jeffhaod5347e02012-03-22 17:25:05 -0700828 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT)
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 : verifier_->LogVerifyInfo())
830 << "monitor-exit not unlocking the top of the monitor stack";
831 } else {
832 // Record the register was unlocked
833 ClearRegToLockDepth(reg_idx, monitors_.size());
834 }
835 }
836}
837
838bool RegisterLine::VerifyMonitorStackEmpty() {
839 if (MonitorStackDepth() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700840 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected empty monitor stack";
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 return false;
842 } else {
843 return true;
844 }
845}
846
847bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
848 bool changed = false;
849 for (size_t idx = 0; idx < num_regs_; idx++) {
850 if (line_[idx] != incoming_line->line_[idx]) {
851 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
852 const RegType& cur_type = GetRegisterType(idx);
853 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
854 changed = changed || !cur_type.Equals(new_type);
855 line_[idx] = new_type.GetId();
856 }
857 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700858 if(monitors_.size() != incoming_line->monitors_.size()) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths (depth="
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
861 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
862 for (uint32_t idx = 0; idx < num_regs_; idx++) {
863 size_t depths = reg_to_lock_depths_.count(idx);
864 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
865 if (depths != incoming_depths) {
866 if (depths == 0 || incoming_depths == 0) {
867 reg_to_lock_depths_.erase(idx);
868 } else {
jeffhaod5347e02012-03-22 17:25:05 -0700869 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths for register v" << idx
870 << ": " << depths << " != " << incoming_depths;
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 break;
872 }
873 }
874 }
875 }
876 return changed;
877}
878
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800879void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 for (size_t i = 0; i < num_regs_; i += 8) {
881 uint8_t val = 0;
882 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
883 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700884 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 val |= 1 << j;
886 }
887 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800888 if ((i / 8) >= max_bytes) {
889 DCHECK_EQ(0, val);
890 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800892 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
893 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 }
895}
896
897std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700898 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 return os;
900}
901
902
903void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
904 uint32_t insns_size, uint16_t registers_size,
905 DexVerifier* verifier) {
906 DCHECK_GT(insns_size, 0U);
907
908 for (uint32_t i = 0; i < insns_size; i++) {
909 bool interesting = false;
910 switch (mode) {
911 case kTrackRegsAll:
912 interesting = flags[i].IsOpcode();
913 break;
914 case kTrackRegsGcPoints:
915 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
916 break;
917 case kTrackRegsBranches:
918 interesting = flags[i].IsBranchTarget();
919 break;
920 default:
921 break;
922 }
923 if (interesting) {
924 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
925 }
926 }
927}
928
Ian Rogers1c5eb702012-02-01 09:18:34 -0800929bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700930 if (klass->IsVerified()) {
931 return true;
932 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800934 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800935 error = "Verifier rejected class ";
936 error += PrettyDescriptor(klass);
937 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 return false;
939 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800940 if (super != NULL && super->IsFinal()) {
941 error = "Verifier rejected class ";
942 error += PrettyDescriptor(klass);
943 error += " that attempts to sub-class final class ";
944 error += PrettyDescriptor(super);
945 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 }
jeffhaobdb76512011-09-07 11:43:16 -0700947 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
948 Method* method = klass->GetDirectMethod(i);
949 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800950 error = "Verifier rejected class ";
951 error += PrettyDescriptor(klass);
952 error += " due to bad method ";
953 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700954 return false;
955 }
956 }
957 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
958 Method* method = klass->GetVirtualMethod(i);
959 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800960 error = "Verifier rejected class ";
961 error += PrettyDescriptor(klass);
962 error += " due to bad method ";
963 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700964 return false;
965 }
966 }
967 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700968}
969
jeffhaobdb76512011-09-07 11:43:16 -0700970bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800972 bool success = verifier.VerifyAll();
Brian Carlstrom75412882012-01-18 01:26:54 -0800973 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
974
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 // We expect either success and no verification error, or failure and a generic failure to
976 // reject the class.
977 if (success) {
978 if (verifier.failure_ != VERIFY_ERROR_NONE) {
979 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
980 << verifier.fail_messages_;
981 }
982 } else {
983 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700984 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700985 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700986 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700987 verifier.Dump(std::cout);
988 }
jeffhaod5347e02012-03-22 17:25:05 -0700989 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
990 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 }
992 return success;
993}
994
Shih-wei Liao371814f2011-10-27 16:52:10 -0700995void DexVerifier::VerifyMethodAndDump(Method* method) {
996 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800997 verifier.VerifyAll();
Shih-wei Liao371814f2011-10-27 16:52:10 -0700998
Elliott Hughese0918552011-10-28 17:18:29 -0700999 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
1000 << verifier.fail_messages_.str() << std::endl
1001 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -07001002}
1003
jeffhaof56197c2012-03-05 18:01:54 -08001004bool DexVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
1005 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
1006 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
1007 const byte* class_data = dex_file->GetClassData(class_def);
1008 ClassDataItemIterator it(*dex_file, class_data);
1009 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
1010 it.Next();
1011 }
1012 while (it.HasNextDirectMethod()) {
1013 uint32_t method_idx = it.GetMemberIndex();
1014 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
1015 it.GetMethodCodeItem())) {
1016 error = "Verifier rejected class";
1017 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
1018 error += " due to bad method ";
1019 error += PrettyMethod(method_idx, *dex_file);
1020 return false;
1021 }
1022 it.Next();
1023 }
1024 while (it.HasNextVirtualMethod()) {
1025 uint32_t method_idx = it.GetMemberIndex();
1026 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
1027 it.GetMethodCodeItem())) {
1028 error = "Verifier rejected class";
1029 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
1030 error += " due to bad method ";
1031 error += PrettyMethod(method_idx, *dex_file);
1032 return false;
1033 }
1034 it.Next();
1035 }
1036 return true;
1037}
1038
1039bool DexVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
1040 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) {
1041 DexVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item);
1042
1043 // Without a method*, we can only verify the struture.
1044 bool success = verifier.VerifyStructure();
1045 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
1046
1047 // We expect either success and no verification error, or failure and a generic failure to
1048 // reject the class.
1049 if (success) {
1050 if (verifier.failure_ != VERIFY_ERROR_NONE) {
1051 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method_idx, *dex_file)
1052 << std::endl << verifier.fail_messages_;
1053 }
1054 } else {
1055 LOG(INFO) << "Verification error in " << PrettyMethod(method_idx, *dex_file) << " "
1056 << verifier.fail_messages_.str();
1057 if (gDebugVerify) {
1058 std::cout << std::endl << verifier.info_messages_.str();
1059 verifier.Dump(std::cout);
1060 }
jeffhaod5347e02012-03-22 17:25:05 -07001061 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
1062 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
jeffhaof56197c2012-03-05 18:01:54 -08001063 }
1064 return success;
1065}
1066
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001067DexVerifier::DexVerifier(Method* method)
1068 : work_insn_idx_(-1),
1069 method_(method),
1070 failure_(VERIFY_ERROR_NONE),
1071 new_instance_count_(0),
1072 monitor_enter_count_(0) {
1073 CHECK(method != NULL);
jeffhaof56197c2012-03-05 18:01:54 -08001074 dex_cache_ = method->GetDeclaringClass()->GetDexCache();
1075 class_loader_ = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001076 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -08001077 dex_file_ = &class_linker->FindDexFile(dex_cache_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaof56197c2012-03-05 18:01:54 -08001079 const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef();
1080 class_def_idx_ = dex_file_->GetIndexForClassDef(*class_def);
jeffhaoba5ebb92011-08-25 17:24:37 -07001081}
1082
jeffhaof56197c2012-03-05 18:01:54 -08001083DexVerifier::DexVerifier(const DexFile* dex_file, DexCache* dex_cache,
1084 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item)
1085 : work_insn_idx_(-1),
1086 method_(NULL),
1087 dex_file_(dex_file),
1088 dex_cache_(dex_cache),
1089 class_loader_(class_loader),
1090 class_def_idx_(class_def_idx),
1091 code_item_(code_item),
1092 failure_(VERIFY_ERROR_NONE),
1093 new_instance_count_(0),
1094 monitor_enter_count_(0) {
1095}
1096
1097bool DexVerifier::VerifyAll() {
1098 CHECK(method_ != NULL);
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 // If there aren't any instructions, make sure that's expected, then exit successfully.
1100 if (code_item_ == NULL) {
1101 if (!method_->IsNative() && !method_->IsAbstract()) {
jeffhaod5347e02012-03-22 17:25:05 -07001102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001103 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 } else {
1105 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001106 }
jeffhaobdb76512011-09-07 11:43:16 -07001107 }
jeffhaof56197c2012-03-05 18:01:54 -08001108 return VerifyStructure() && VerifyCodeFlow();
1109}
1110
1111bool DexVerifier::VerifyStructure() {
1112 if (code_item_ == NULL) {
1113 return true;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1116 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
1118 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001120 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 // Allocate and initialize an array to hold instruction data.
1122 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1123 // Run through the instructions and see if the width checks out.
1124 bool result = ComputeWidthsAndCountOps();
1125 // Flag instructions guarded by a "try" block and check exception handlers.
1126 result = result && ScanTryCatchBlocks();
1127 // Perform static instruction verification.
1128 result = result && VerifyInstructions();
jeffhaobdb76512011-09-07 11:43:16 -07001129 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130}
1131
Ian Rogers47a05882012-02-03 12:23:33 -08001132std::ostream& DexVerifier::Fail(VerifyError error) {
1133 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers47a05882012-02-03 12:23:33 -08001134 if (Runtime::Current()->IsCompiler()) {
jeffhaod1224c72012-02-29 13:43:08 -08001135 switch (error) {
1136 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
jeffhaod5347e02012-03-22 17:25:05 -07001137 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
1138 // or to agree on access because of not yet available class loaders, or class loaders that
1139 // will differ at runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001140 case VERIFY_ERROR_NO_CLASS:
1141 case VERIFY_ERROR_NO_FIELD:
1142 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -08001143 case VERIFY_ERROR_ACCESS_CLASS:
1144 case VERIFY_ERROR_ACCESS_FIELD:
1145 case VERIFY_ERROR_ACCESS_METHOD:
jeffhaod5347e02012-03-22 17:25:05 -07001146 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogers47a05882012-02-03 12:23:33 -08001147 break;
jeffhaod5347e02012-03-22 17:25:05 -07001148 // Hard verification failures at compile time will still fail at runtime, so the class is
1149 // marked as rejected to prevent it from being compiled.
1150 case VERIFY_ERROR_BAD_CLASS_HARD: {
jeffhaof56197c2012-03-05 18:01:54 -08001151 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -08001152 AddRejectedClass(ref);
1153 break;
1154 }
Ian Rogers47a05882012-02-03 12:23:33 -08001155 default:
1156 break;
1157 }
1158 }
1159 failure_ = error;
1160 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1161 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1162}
1163
Ian Rogersd81871c2011-10-03 13:57:23 -07001164bool DexVerifier::ComputeWidthsAndCountOps() {
1165 const uint16_t* insns = code_item_->insns_;
1166 size_t insns_size = code_item_->insns_size_in_code_units_;
1167 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001168 size_t new_instance_count = 0;
1169 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001171
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001173 Instruction::Code opcode = inst->Opcode();
1174 if (opcode == Instruction::NEW_INSTANCE) {
1175 new_instance_count++;
1176 } else if (opcode == Instruction::MONITOR_ENTER) {
1177 monitor_enter_count++;
1178 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 size_t inst_size = inst->SizeInCodeUnits();
1180 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1181 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001182 inst = inst->Next();
1183 }
1184
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1187 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001188 return false;
1189 }
1190
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 new_instance_count_ = new_instance_count;
1192 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001193 return true;
1194}
1195
Ian Rogersd81871c2011-10-03 13:57:23 -07001196bool DexVerifier::ScanTryCatchBlocks() {
1197 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001198 if (tries_size == 0) {
1199 return true;
1200 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001202 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001203
1204 for (uint32_t idx = 0; idx < tries_size; idx++) {
1205 const DexFile::TryItem* try_item = &tries[idx];
1206 uint32_t start = try_item->start_addr_;
1207 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001208 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1210 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001211 return false;
1212 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001215 return false;
1216 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 for (uint32_t dex_pc = start; dex_pc < end;
1218 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1219 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001220 }
1221 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001222 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001223 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001224 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001225 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001226 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001227 CatchHandlerIterator iterator(handlers_ptr);
1228 for (; iterator.HasNext(); iterator.Next()) {
1229 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001232 return false;
1233 }
jeffhao60f83e32012-02-13 17:16:30 -08001234 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1235 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07001236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
jeffhao60f83e32012-02-13 17:16:30 -08001237 << dex_pc << ")";
1238 return false;
1239 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001241 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1242 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001243 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -08001244 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
1245 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001246 if (exception_type == NULL) {
1247 DCHECK(Thread::Current()->IsExceptionPending());
1248 Thread::Current()->ClearException();
1249 }
1250 }
jeffhaobdb76512011-09-07 11:43:16 -07001251 }
Ian Rogers0571d352011-11-03 19:51:38 -07001252 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001253 }
jeffhaobdb76512011-09-07 11:43:16 -07001254 return true;
1255}
1256
Ian Rogersd81871c2011-10-03 13:57:23 -07001257bool DexVerifier::VerifyInstructions() {
1258 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001259
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 /* Flag the start of the method as a branch target. */
1261 insn_flags_[0].SetBranchTarget();
1262
1263 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1264 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1265 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001266 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
jeffhaod1224c72012-02-29 13:43:08 -08001267 fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 return false;
1269 }
1270 /* Flag instructions that are garbage collection points */
1271 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1272 insn_flags_[dex_pc].SetGcPoint();
1273 }
1274 dex_pc += inst->SizeInCodeUnits();
1275 inst = inst->Next();
1276 }
1277 return true;
1278}
1279
1280bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001281 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 bool result = true;
1283 switch (inst->GetVerifyTypeArgumentA()) {
1284 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001285 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 break;
1287 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001288 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 break;
1290 }
1291 switch (inst->GetVerifyTypeArgumentB()) {
1292 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001293 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 break;
1295 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001296 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 break;
1298 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 break;
1301 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001302 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 break;
1304 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001305 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 break;
1307 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001308 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 break;
1310 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001311 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 break;
1313 }
1314 switch (inst->GetVerifyTypeArgumentC()) {
1315 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001316 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 break;
1318 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001319 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 break;
1321 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 break;
1324 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 break;
1327 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001328 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 break;
1330 }
1331 switch (inst->GetVerifyExtraFlags()) {
1332 case Instruction::kVerifyArrayData:
1333 result = result && CheckArrayData(code_offset);
1334 break;
1335 case Instruction::kVerifyBranchTarget:
1336 result = result && CheckBranchTarget(code_offset);
1337 break;
1338 case Instruction::kVerifySwitchTargets:
1339 result = result && CheckSwitchTargets(code_offset);
1340 break;
1341 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001342 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 break;
1344 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 break;
1347 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 result = false;
1350 break;
1351 }
1352 return result;
1353}
1354
1355bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1356 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1358 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
1360 }
1361 return true;
1362}
1363
1364bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1365 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1367 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 return false;
1369 }
1370 return true;
1371}
1372
1373bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1374 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1376 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 return false;
1378 }
1379 return true;
1380}
1381
1382bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1383 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1385 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return false;
1387 }
1388 return true;
1389}
1390
1391bool DexVerifier::CheckNewInstance(uint32_t idx) {
1392 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1394 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 return false;
1396 }
1397 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001398 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 return false;
1402 }
1403 return true;
1404}
1405
1406bool DexVerifier::CheckStringIndex(uint32_t idx) {
1407 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1409 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 return false;
1411 }
1412 return true;
1413}
1414
1415bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1416 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1418 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 }
1421 return true;
1422}
1423
1424bool DexVerifier::CheckNewArray(uint32_t idx) {
1425 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1427 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 return false;
1429 }
1430 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001431 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 const char* cp = descriptor;
1433 while (*cp++ == '[') {
1434 bracket_count++;
1435 }
1436 if (bracket_count == 0) {
1437 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -07001438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 return false;
1440 } else if (bracket_count > 255) {
1441 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -07001442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 return false;
1444 }
1445 return true;
1446}
1447
1448bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1449 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1450 const uint16_t* insns = code_item_->insns_ + cur_offset;
1451 const uint16_t* array_data;
1452 int32_t array_data_offset;
1453
1454 DCHECK_LT(cur_offset, insn_count);
1455 /* make sure the start of the array data table is in range */
1456 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1457 if ((int32_t) cur_offset + array_data_offset < 0 ||
1458 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
1460 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 return false;
1462 }
1463 /* offset to array data table is a relative branch-style offset */
1464 array_data = insns + array_data_offset;
1465 /* make sure the table is 32-bit aligned */
1466 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1468 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 return false;
1470 }
1471 uint32_t value_width = array_data[1];
1472 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1473 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1474 /* make sure the end of the switch is in range */
1475 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1477 << ", data offset " << array_data_offset << ", end "
1478 << cur_offset + array_data_offset + table_size
1479 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 return false;
1481 }
1482 return true;
1483}
1484
1485bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1486 int32_t offset;
1487 bool isConditional, selfOkay;
1488 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1489 return false;
1490 }
1491 if (!selfOkay && offset == 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << (void*) cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 return false;
1494 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001495 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1496 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
jeffhaod5347e02012-03-22 17:25:05 -07001498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << (void*) cur_offset << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 return false;
1500 }
1501 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1502 int32_t abs_offset = cur_offset + offset;
1503 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
1505 << (void*) abs_offset << ") at " << (void*) cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 return false;
1507 }
1508 insn_flags_[abs_offset].SetBranchTarget();
1509 return true;
1510}
1511
1512bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1513 bool* selfOkay) {
1514 const uint16_t* insns = code_item_->insns_ + cur_offset;
1515 *pConditional = false;
1516 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001517 switch (*insns & 0xff) {
1518 case Instruction::GOTO:
1519 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 break;
1521 case Instruction::GOTO_32:
1522 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 *selfOkay = true;
1524 break;
1525 case Instruction::GOTO_16:
1526 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 break;
1528 case Instruction::IF_EQ:
1529 case Instruction::IF_NE:
1530 case Instruction::IF_LT:
1531 case Instruction::IF_GE:
1532 case Instruction::IF_GT:
1533 case Instruction::IF_LE:
1534 case Instruction::IF_EQZ:
1535 case Instruction::IF_NEZ:
1536 case Instruction::IF_LTZ:
1537 case Instruction::IF_GEZ:
1538 case Instruction::IF_GTZ:
1539 case Instruction::IF_LEZ:
1540 *pOffset = (int16_t) insns[1];
1541 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 break;
1543 default:
1544 return false;
1545 break;
1546 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001547 return true;
1548}
1549
Ian Rogersd81871c2011-10-03 13:57:23 -07001550bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1551 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001552 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001554 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1556 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
1558 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001559 return false;
1560 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001561 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001563 /* make sure the table is 32-bit aligned */
1564 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1566 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001567 return false;
1568 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 uint32_t switch_count = switch_insns[1];
1570 int32_t keys_offset, targets_offset;
1571 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001572 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1573 /* 0=sig, 1=count, 2/3=firstKey */
1574 targets_offset = 4;
1575 keys_offset = -1;
1576 expected_signature = Instruction::kPackedSwitchSignature;
1577 } else {
1578 /* 0=sig, 1=count, 2..count*2 = keys */
1579 keys_offset = 2;
1580 targets_offset = 2 + 2 * switch_count;
1581 expected_signature = Instruction::kSparseSwitchSignature;
1582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001584 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -07001585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1586 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001587 return false;
1588 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001589 /* make sure the end of the switch is in range */
1590 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
1592 << switch_offset << ", end "
1593 << (cur_offset + switch_offset + table_size)
1594 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001595 return false;
1596 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001597 /* for a sparse switch, verify the keys are in ascending order */
1598 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1600 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001601 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1602 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1603 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1605 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001606 return false;
1607 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001608 last_key = key;
1609 }
1610 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001611 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 for (uint32_t targ = 0; targ < switch_count; targ++) {
1613 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1614 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1615 int32_t abs_offset = cur_offset + offset;
1616 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
1618 << (void*) abs_offset << ") at "
1619 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001620 return false;
1621 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 insn_flags_[abs_offset].SetBranchTarget();
1623 }
1624 return true;
1625}
1626
1627bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1628 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 return false;
1631 }
1632 uint16_t registers_size = code_item_->registers_size_;
1633 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001634 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1636 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 return false;
1638 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001639 }
1640
1641 return true;
1642}
1643
Ian Rogersd81871c2011-10-03 13:57:23 -07001644bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1645 uint16_t registers_size = code_item_->registers_size_;
1646 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1647 // integer overflow when adding them here.
1648 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1650 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001651 return false;
1652 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001653 return true;
1654}
1655
Brian Carlstrom75412882012-01-18 01:26:54 -08001656const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1657 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1658 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1659 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1660 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1661 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1662 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1663 gc_map.begin(),
1664 gc_map.end());
1665 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1666 DCHECK_EQ(gc_map.size(),
1667 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1668 (length_prefixed_gc_map->at(1) << 16) |
1669 (length_prefixed_gc_map->at(2) << 8) |
1670 (length_prefixed_gc_map->at(3) << 0)));
1671 return length_prefixed_gc_map;
1672}
1673
Ian Rogersd81871c2011-10-03 13:57:23 -07001674bool DexVerifier::VerifyCodeFlow() {
1675 uint16_t registers_size = code_item_->registers_size_;
1676 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001677
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001679 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1680 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 }
1682 /* Create and initialize table holding register status */
1683 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1684 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001685
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 work_line_.reset(new RegisterLine(registers_size, this));
1687 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001688
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 /* Initialize register types of method arguments. */
1690 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001691 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1692 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 return false;
1694 }
1695 /* Perform code flow verification. */
1696 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001697 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001699 }
1700
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001702 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1703 if (map.get() == NULL) {
1704 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 return false; // Not a real failure, but a failure to encode
1706 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001707#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001708 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001709#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001710 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1711 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1712 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001713
1714#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom75412882012-01-18 01:26:54 -08001715 method_->SetGcMap(&gc_map->at(0));
Logan Chienfca7e872011-12-20 20:08:22 +08001716#else
1717 /* Generate Inferred Register Category for LLVM-based Code Generator */
1718 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
1719 method_->SetInferredRegCategoryMap(table);
1720#endif
1721
jeffhaobdb76512011-09-07 11:43:16 -07001722 return true;
1723}
1724
Ian Rogersd81871c2011-10-03 13:57:23 -07001725void DexVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001726 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 os << "Native method" << std::endl;
1728 return;
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 DCHECK(code_item_ != NULL);
1731 const Instruction* inst = Instruction::At(code_item_->insns_);
1732 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1733 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001734 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001735 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1737 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001738 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001740 inst = inst->Next();
1741 }
jeffhaobdb76512011-09-07 11:43:16 -07001742}
1743
Ian Rogersd81871c2011-10-03 13:57:23 -07001744static bool IsPrimitiveDescriptor(char descriptor) {
1745 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001746 case 'I':
1747 case 'C':
1748 case 'S':
1749 case 'B':
1750 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001751 case 'F':
1752 case 'D':
1753 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001755 default:
1756 return false;
1757 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001758}
1759
Ian Rogersd81871c2011-10-03 13:57:23 -07001760bool DexVerifier::SetTypesFromSignature() {
1761 RegisterLine* reg_line = reg_table_.GetLine(0);
1762 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1763 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001764
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1766 //Include the "this" pointer.
1767 size_t cur_arg = 0;
1768 if (!method_->IsStatic()) {
1769 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1770 // argument as uninitialized. This restricts field access until the superclass constructor is
1771 // called.
1772 Class* declaring_class = method_->GetDeclaringClass();
1773 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1774 reg_line->SetRegisterType(arg_start + cur_arg,
1775 reg_types_.UninitializedThisArgument(declaring_class));
1776 } else {
1777 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001778 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001780 }
1781
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001782 const DexFile::ProtoId& proto_id =
1783 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001784 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785
1786 for (; iterator.HasNext(); iterator.Next()) {
1787 const char* descriptor = iterator.GetDescriptor();
1788 if (descriptor == NULL) {
1789 LOG(FATAL) << "Null descriptor";
1790 }
1791 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1793 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 return false;
1795 }
1796 switch (descriptor[0]) {
1797 case 'L':
1798 case '[':
1799 // We assume that reference arguments are initialized. The only way it could be otherwise
1800 // (assuming the caller was verified) is if the current method is <init>, but in that case
1801 // it's effectively considered initialized the instant we reach here (in the sense that we
1802 // can return without doing anything or call virtual methods).
1803 {
1804 const RegType& reg_type =
1805 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001806 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 }
1808 break;
1809 case 'Z':
1810 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1811 break;
1812 case 'C':
1813 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1814 break;
1815 case 'B':
1816 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1817 break;
1818 case 'I':
1819 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1820 break;
1821 case 'S':
1822 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1823 break;
1824 case 'F':
1825 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1826 break;
1827 case 'J':
1828 case 'D': {
1829 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1830 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1831 cur_arg++;
1832 break;
1833 }
1834 default:
jeffhaod5347e02012-03-22 17:25:05 -07001835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 return false;
1837 }
1838 cur_arg++;
1839 }
1840 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 return false;
1843 }
1844 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1845 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1846 // format. Only major difference from the method argument format is that 'V' is supported.
1847 bool result;
1848 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1849 result = descriptor[1] == '\0';
1850 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1851 size_t i = 0;
1852 do {
1853 i++;
1854 } while (descriptor[i] == '['); // process leading [
1855 if (descriptor[i] == 'L') { // object array
1856 do {
1857 i++; // find closing ;
1858 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1859 result = descriptor[i] == ';';
1860 } else { // primitive array
1861 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1862 }
1863 } else if (descriptor[0] == 'L') {
1864 // could be more thorough here, but shouldn't be required
1865 size_t i = 0;
1866 do {
1867 i++;
1868 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1869 result = descriptor[i] == ';';
1870 } else {
1871 result = false;
1872 }
1873 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1875 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 }
1877 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001878}
1879
Ian Rogersd81871c2011-10-03 13:57:23 -07001880bool DexVerifier::CodeFlowVerifyMethod() {
1881 const uint16_t* insns = code_item_->insns_;
1882 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001883
jeffhaobdb76512011-09-07 11:43:16 -07001884 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 insn_flags_[0].SetChanged();
1886 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001887
jeffhaobdb76512011-09-07 11:43:16 -07001888 /* Continue until no instructions are marked "changed". */
1889 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1891 uint32_t insn_idx = start_guess;
1892 for (; insn_idx < insns_size; insn_idx++) {
1893 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
1895 }
jeffhaobdb76512011-09-07 11:43:16 -07001896 if (insn_idx == insns_size) {
1897 if (start_guess != 0) {
1898 /* try again, starting from the top */
1899 start_guess = 0;
1900 continue;
1901 } else {
1902 /* all flags are clear */
1903 break;
1904 }
1905 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 // We carry the working set of registers from instruction to instruction. If this address can
1907 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1908 // "changed" flags, we need to load the set of registers from the table.
1909 // Because we always prefer to continue on to the next instruction, we should never have a
1910 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1911 // target.
1912 work_insn_idx_ = insn_idx;
1913 if (insn_flags_[insn_idx].IsBranchTarget()) {
1914 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001915 } else {
1916#ifndef NDEBUG
1917 /*
1918 * Sanity check: retrieve the stored register line (assuming
1919 * a full table) and make sure it actually matches.
1920 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1922 if (register_line != NULL) {
1923 if (work_line_->CompareLine(register_line) != 0) {
1924 Dump(std::cout);
1925 std::cout << info_messages_.str();
1926 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1927 << "@" << (void*)work_insn_idx_ << std::endl
1928 << " work_line=" << *work_line_ << std::endl
1929 << " expected=" << *register_line;
1930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 }
1932#endif
1933 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 if (!CodeFlowVerifyInstruction(&start_guess)) {
1935 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001936 return false;
1937 }
jeffhaobdb76512011-09-07 11:43:16 -07001938 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 insn_flags_[insn_idx].SetVisited();
1940 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001941 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001942
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001944 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001945 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001946 * (besides the wasted space), but it indicates a flaw somewhere
1947 * down the line, possibly in the verifier.
1948 *
1949 * If we've substituted "always throw" instructions into the stream,
1950 * we are almost certainly going to have some dead code.
1951 */
1952 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 uint32_t insn_idx = 0;
1954 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001955 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001956 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001957 * may or may not be preceded by a padding NOP (for alignment).
1958 */
1959 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1960 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1961 insns[insn_idx] == Instruction::kArrayDataSignature ||
1962 (insns[insn_idx] == Instruction::NOP &&
1963 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1964 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1965 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001967 }
1968
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001970 if (dead_start < 0)
1971 dead_start = insn_idx;
1972 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001974 dead_start = -1;
1975 }
1976 }
1977 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001979 }
1980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 return true;
1982}
1983
Ian Rogersd81871c2011-10-03 13:57:23 -07001984bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001985#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001987 gDvm.verifierStats.instrsReexamined++;
1988 } else {
1989 gDvm.verifierStats.instrsExamined++;
1990 }
1991#endif
1992
1993 /*
1994 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001995 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001996 * control to another statement:
1997 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001999 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002000 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002001 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002002 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002003 * throw an exception that is handled by an encompassing "try"
2004 * block.
2005 *
2006 * We can also return, in which case there is no successor instruction
2007 * from this point.
2008 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002010 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2012 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002013 DecodedInstruction dec_insn(inst);
2014 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002015
jeffhaobdb76512011-09-07 11:43:16 -07002016 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002017 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07002018 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002020 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2021 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023
2024 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002025 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002026 * can throw an exception, we will copy/merge this into the "catch"
2027 * address rather than work_line, because we don't want the result
2028 * from the "successful" code path (e.g. a check-cast that "improves"
2029 * a type) to be visible to the exception handler.
2030 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002031 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07002033 } else {
2034#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002036#endif
2037 }
2038
Elliott Hughesadb8c672012-03-06 16:49:32 -08002039 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::NOP:
2041 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002042 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002043 * a signature that looks like a NOP; if we see one of these in
2044 * the course of executing code then we have a problem.
2045 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002046 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002048 }
2049 break;
2050
2051 case Instruction::MOVE:
2052 case Instruction::MOVE_FROM16:
2053 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002054 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::MOVE_WIDE:
2057 case Instruction::MOVE_WIDE_FROM16:
2058 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::MOVE_OBJECT:
2062 case Instruction::MOVE_OBJECT_FROM16:
2063 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002064 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066
2067 /*
2068 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002069 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002070 * might want to hold the result in an actual CPU register, so the
2071 * Dalvik spec requires that these only appear immediately after an
2072 * invoke or filled-new-array.
2073 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002074 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002075 * redundant with the reset done below, but it can make the debug info
2076 * easier to read in some cases.)
2077 */
2078 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002082 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002085 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07002089 /*
jeffhao60f83e32012-02-13 17:16:30 -08002090 * This statement can only appear as the first instruction in an exception handler. We verify
2091 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002092 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002093 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002094 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2099 if (!GetMethodReturnType().IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07002100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 }
jeffhaobdb76512011-09-07 11:43:16 -07002102 }
2103 break;
2104 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002106 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 const RegType& return_type = GetMethodReturnType();
2108 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 } else {
2111 // Compilers may generate synthetic functions that write byte values into boolean fields.
2112 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2115 ((return_type.IsBoolean() || return_type.IsByte() ||
2116 return_type.IsShort() || return_type.IsChar()) &&
2117 src_type.IsInteger()));
2118 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002119 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002121 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 }
jeffhaobdb76512011-09-07 11:43:16 -07002123 }
2124 }
2125 break;
2126 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002128 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 const RegType& return_type = GetMethodReturnType();
2130 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 } else {
2133 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 work_line_->VerifyRegisterType(dec_insn.vA, return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002136 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 }
2139 }
2140 break;
2141 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2143 const RegType& return_type = GetMethodReturnType();
2144 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 } else {
2147 /* return_type is the *expected* return type, not register value */
2148 DCHECK(!return_type.IsZero());
2149 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08002150 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07002151 // Disallow returning uninitialized values and verify that the reference in vAA is an
2152 // instance of the "return_type"
2153 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002154 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002155 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07002157 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002158 }
2159 }
2160 }
2161 break;
2162
2163 case Instruction::CONST_4:
2164 case Instruction::CONST_16:
2165 case Instruction::CONST:
2166 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002167 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::CONST_HIGH16:
2170 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002171 work_line_->SetRegisterType(dec_insn.vA,
2172 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::CONST_WIDE_16:
2175 case Instruction::CONST_WIDE_32:
2176 case Instruction::CONST_WIDE:
2177 case Instruction::CONST_WIDE_HIGH16:
2178 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002179 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181 case Instruction::CONST_STRING:
2182 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002183 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002186 // Get type from instruction if unresolved then we need an access check
2187 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08002188 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002189 // Register holds class, ie its type is class, but on error we keep it Unknown
Elliott Hughesadb8c672012-03-06 16:49:32 -08002190 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers28ad40d2011-10-27 15:19:26 -07002191 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002195 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197 case Instruction::MONITOR_EXIT:
2198 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002199 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002200 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002201 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002202 * to the need to handle asynchronous exceptions, a now-deprecated
2203 * feature that Dalvik doesn't support.)
2204 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002205 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002206 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002207 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002208 * structured locking checks are working, the former would have
2209 * failed on the -enter instruction, and the latter is impossible.
2210 *
2211 * This is fortunate, because issue 3221411 prevents us from
2212 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002213 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002214 * some catch blocks (which will show up as "dead" code when
2215 * we skip them here); if we can't, then the code path could be
2216 * "live" so we still need to check it.
2217 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002218 opcode_flags &= ~Instruction::kThrow;
2219 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002224 /*
2225 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2226 * could be a "upcast" -- not expected, so we don't try to address it.)
2227 *
2228 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002229 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002231 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002232 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002233 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002234 if (res_type.IsUnknown()) {
2235 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2236 break; // couldn't resolve class
2237 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2239 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002240 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002241 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07002245 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002247 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002249 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002250 }
jeffhaobdb76512011-09-07 11:43:16 -07002251 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002252 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 }
2254 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002255 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002256 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002257 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002260 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 }
2262 }
2263 break;
2264 }
2265 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002266 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
jeffhao8cd6dda2012-02-22 10:15:34 -08002267 if (res_type.IsUnknown()) {
2268 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2269 break; // couldn't resolve class
2270 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002271 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2272 // can't create an instance of an interface or abstract class */
2273 if (!res_type.IsInstantiableTypes()) {
2274 Fail(VERIFY_ERROR_INSTANTIATION)
2275 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002277 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2278 // Any registers holding previous allocations from this address that have not yet been
2279 // initialized must be marked invalid.
2280 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2281 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08002282 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 }
2284 break;
2285 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002286 case Instruction::NEW_ARRAY:
2287 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002290 VerifyNewArray(dec_insn, true, false);
2291 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002293 case Instruction::FILLED_NEW_ARRAY_RANGE:
2294 VerifyNewArray(dec_insn, true, true);
2295 just_set_result = true; // Filled new array range sets result register
2296 break;
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::CMPL_FLOAT:
2298 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002299 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002300 break;
2301 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002302 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002303 break;
2304 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002305 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
2307 case Instruction::CMPL_DOUBLE:
2308 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002309 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002310 break;
2311 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002312 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002313 break;
2314 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002315 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002319 break;
2320 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002321 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002322 break;
2323 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002324 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002328 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002329 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002330 }
2331 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 }
jeffhaobdb76512011-09-07 11:43:16 -07002333 case Instruction::GOTO:
2334 case Instruction::GOTO_16:
2335 case Instruction::GOTO_32:
2336 /* no effect on or use of registers */
2337 break;
2338
2339 case Instruction::PACKED_SWITCH:
2340 case Instruction::SPARSE_SWITCH:
2341 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 case Instruction::FILL_ARRAY_DATA: {
2346 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002347 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08002348 /* array_type can be null if the reg type is Zero */
2349 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002350 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002352 } else {
jeffhao457cc512012-02-02 16:55:13 -08002353 const RegType& component_type = reg_types_.GetComponentType(array_type,
2354 method_->GetDeclaringClass()->GetClassLoader());
2355 DCHECK(!component_type.IsUnknown());
2356 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2358 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 } else {
jeffhao457cc512012-02-02 16:55:13 -08002360 // Now verify if the element width in the table matches the element width declared in
2361 // the array
2362 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2363 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002365 } else {
2366 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2367 // Since we don't compress the data in Dex, expect to see equal width of data stored
2368 // in the table and expected from the array class.
2369 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2371 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002372 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 }
2374 }
jeffhaobdb76512011-09-07 11:43:16 -07002375 }
2376 }
2377 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 }
jeffhaobdb76512011-09-07 11:43:16 -07002379 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002381 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2382 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 bool mismatch = false;
2384 if (reg_type1.IsZero()) { // zero then integral or reference expected
2385 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2386 } else if (reg_type1.IsReferenceTypes()) { // both references?
2387 mismatch = !reg_type2.IsReferenceTypes();
2388 } else { // both integral?
2389 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2390 }
2391 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07002392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2393 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002394 }
2395 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 case Instruction::IF_LT:
2398 case Instruction::IF_GE:
2399 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002401 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2402 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2405 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002406 }
2407 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 }
jeffhaobdb76512011-09-07 11:43:16 -07002409 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002411 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 }
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 case Instruction::IF_LTZ:
2418 case Instruction::IF_GEZ:
2419 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2424 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 }
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 }
jeffhaobdb76512011-09-07 11:43:16 -07002428 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2430 break;
jeffhaobdb76512011-09-07 11:43:16 -07002431 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2433 break;
jeffhaobdb76512011-09-07 11:43:16 -07002434 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 VerifyAGet(dec_insn, reg_types_.Char(), true);
2436 break;
jeffhaobdb76512011-09-07 11:43:16 -07002437 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 case Instruction::AGET:
2441 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2442 break;
jeffhaobdb76512011-09-07 11:43:16 -07002443 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 VerifyAGet(dec_insn, reg_types_.Long(), true);
2445 break;
2446 case Instruction::AGET_OBJECT:
2447 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 case Instruction::APUT_BOOLEAN:
2451 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2452 break;
2453 case Instruction::APUT_BYTE:
2454 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2455 break;
2456 case Instruction::APUT_CHAR:
2457 VerifyAPut(dec_insn, reg_types_.Char(), true);
2458 break;
2459 case Instruction::APUT_SHORT:
2460 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471
jeffhaobdb76512011-09-07 11:43:16 -07002472 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002473 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 break;
jeffhaobdb76512011-09-07 11:43:16 -07002475 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002476 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 break;
jeffhaobdb76512011-09-07 11:43:16 -07002478 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002479 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 break;
jeffhaobdb76512011-09-07 11:43:16 -07002481 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002482 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 break;
2484 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002485 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002488 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002489 break;
2490 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002491 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 break;
jeffhaobdb76512011-09-07 11:43:16 -07002493
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002495 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 break;
2497 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002498 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 break;
2500 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002501 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 break;
2503 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002504 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002507 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002510 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 break;
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002513 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515
jeffhaobdb76512011-09-07 11:43:16 -07002516 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002517 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 break;
jeffhaobdb76512011-09-07 11:43:16 -07002519 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002520 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 break;
jeffhaobdb76512011-09-07 11:43:16 -07002522 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002523 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 break;
jeffhaobdb76512011-09-07 11:43:16 -07002525 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002526 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 break;
2528 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002529 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002532 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002535 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 break;
2537
2538 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002539 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 break;
2541 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002542 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 break;
2544 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002545 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 break;
2547 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002548 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002551 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002554 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002557 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559
2560 case Instruction::INVOKE_VIRTUAL:
2561 case Instruction::INVOKE_VIRTUAL_RANGE:
2562 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002564 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
2565 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2566 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
2567 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2569 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002570 const char* descriptor;
2571 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002572 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2574 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002575 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002576 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002577 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002578 }
Ian Rogers9074b992011-10-26 17:41:55 -07002579 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002580 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002582 just_set_result = true;
2583 }
2584 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 }
jeffhaobdb76512011-09-07 11:43:16 -07002586 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002588 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2590 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002591 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 * Some additional checks when calling a constructor. We know from the invocation arg check
2593 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2594 * that to require that called_method->klass is the same as this->klass or this->super,
2595 * allowing the latter only if the "this" argument is the same as the "this" argument to
2596 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002597 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002598 bool is_constructor;
2599 if (called_method != NULL) {
2600 is_constructor = called_method->IsConstructor();
2601 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002602 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002603 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2604 const char* name = dex_file_->GetMethodName(method_id);
2605 is_constructor = strcmp(name, "<init>") == 0;
2606 }
2607 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2609 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611
2612 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07002614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002617 if (called_method != NULL) {
2618 Class* this_class = this_type.GetClass();
2619 DCHECK(this_class != NULL);
2620 /* must be in same class or in superclass */
2621 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2622 if (this_class != method_->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07002623 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers28ad40d2011-10-27 15:19:26 -07002624 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2625 break;
2626 }
2627 } else if (called_method->GetDeclaringClass() != this_class) {
jeffhaod5347e02012-03-22 17:25:05 -07002628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 }
jeffhaobdb76512011-09-07 11:43:16 -07002631 }
2632
2633 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002634 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 }
2639
2640 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002641 * Replace the uninitialized reference with an initialized one. We need to do this for all
2642 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002643 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 work_line_->MarkRefsAsInitialized(this_type);
2645 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002647 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002648 const char* descriptor;
2649 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002650 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002651 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2652 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002653 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002654 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002655 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002656 }
Ian Rogers9074b992011-10-26 17:41:55 -07002657 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002658 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002660 just_set_result = true;
2661 }
2662 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 }
jeffhaobdb76512011-09-07 11:43:16 -07002664 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002666 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2668 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002669 const char* descriptor;
2670 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002671 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002672 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2673 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002674 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002675 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002676 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002677 }
Ian Rogers9074b992011-10-26 17:41:55 -07002678 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002679 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 work_line_->SetResultRegisterType(return_type);
2681 just_set_result = true;
2682 }
jeffhaobdb76512011-09-07 11:43:16 -07002683 }
2684 break;
2685 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002687 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2689 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002690 if (abs_method != NULL) {
2691 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002692 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002693 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2694 << PrettyMethod(abs_method) << "'";
2695 break;
2696 }
2697 }
2698 /* Get the type of the "this" arg, which should either be a sub-interface of called
2699 * interface or Object (see comments in RegType::JoinClass).
2700 */
2701 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2702 if (failure_ == VERIFY_ERROR_NONE) {
2703 if (this_type.IsZero()) {
2704 /* null pointer always passes (and always fails at runtime) */
2705 } else {
2706 if (this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
Ian Rogers28ad40d2011-10-27 15:19:26 -07002708 << this_type;
2709 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002711 // In the past we have tried to assert that "called_interface" is assignable
2712 // from "this_type.GetClass()", however, as we do an imprecise Join
2713 // (RegType::JoinClass) we don't have full information on what interfaces are
2714 // implemented by "this_type". For example, two classes may implement the same
2715 // interfaces and have a common parent that doesn't implement the interface. The
2716 // join will set "this_type" to the parent class and a test that this implements
2717 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002718 }
2719 }
jeffhaobdb76512011-09-07 11:43:16 -07002720 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 * We don't have an object instance, so we can't find the concrete method. However, all of
2722 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002723 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002724 const char* descriptor;
2725 if (abs_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002726 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002727 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2728 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002729 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002730 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002731 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002732 }
Ian Rogers9074b992011-10-26 17:41:55 -07002733 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002734 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2735 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002737 just_set_result = true;
2738 }
2739 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 }
jeffhaobdb76512011-09-07 11:43:16 -07002741 case Instruction::NEG_INT:
2742 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745 case Instruction::NEG_LONG:
2746 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002787 break;
2788 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
2794 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002796 break;
2797 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002799 break;
2800
2801 case Instruction::ADD_INT:
2802 case Instruction::SUB_INT:
2803 case Instruction::MUL_INT:
2804 case Instruction::REM_INT:
2805 case Instruction::DIV_INT:
2806 case Instruction::SHL_INT:
2807 case Instruction::SHR_INT:
2808 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002810 break;
2811 case Instruction::AND_INT:
2812 case Instruction::OR_INT:
2813 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::ADD_LONG:
2817 case Instruction::SUB_LONG:
2818 case Instruction::MUL_LONG:
2819 case Instruction::DIV_LONG:
2820 case Instruction::REM_LONG:
2821 case Instruction::AND_LONG:
2822 case Instruction::OR_LONG:
2823 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002825 break;
2826 case Instruction::SHL_LONG:
2827 case Instruction::SHR_LONG:
2828 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 /* shift distance is Int, making these different from other binary operations */
2830 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832 case Instruction::ADD_FLOAT:
2833 case Instruction::SUB_FLOAT:
2834 case Instruction::MUL_FLOAT:
2835 case Instruction::DIV_FLOAT:
2836 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839 case Instruction::ADD_DOUBLE:
2840 case Instruction::SUB_DOUBLE:
2841 case Instruction::MUL_DOUBLE:
2842 case Instruction::DIV_DOUBLE:
2843 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002845 break;
2846 case Instruction::ADD_INT_2ADDR:
2847 case Instruction::SUB_INT_2ADDR:
2848 case Instruction::MUL_INT_2ADDR:
2849 case Instruction::REM_INT_2ADDR:
2850 case Instruction::SHL_INT_2ADDR:
2851 case Instruction::SHR_INT_2ADDR:
2852 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002854 break;
2855 case Instruction::AND_INT_2ADDR:
2856 case Instruction::OR_INT_2ADDR:
2857 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002859 break;
2860 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::ADD_LONG_2ADDR:
2864 case Instruction::SUB_LONG_2ADDR:
2865 case Instruction::MUL_LONG_2ADDR:
2866 case Instruction::DIV_LONG_2ADDR:
2867 case Instruction::REM_LONG_2ADDR:
2868 case Instruction::AND_LONG_2ADDR:
2869 case Instruction::OR_LONG_2ADDR:
2870 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002872 break;
2873 case Instruction::SHL_LONG_2ADDR:
2874 case Instruction::SHR_LONG_2ADDR:
2875 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002877 break;
2878 case Instruction::ADD_FLOAT_2ADDR:
2879 case Instruction::SUB_FLOAT_2ADDR:
2880 case Instruction::MUL_FLOAT_2ADDR:
2881 case Instruction::DIV_FLOAT_2ADDR:
2882 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::ADD_DOUBLE_2ADDR:
2886 case Instruction::SUB_DOUBLE_2ADDR:
2887 case Instruction::MUL_DOUBLE_2ADDR:
2888 case Instruction::DIV_DOUBLE_2ADDR:
2889 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002891 break;
2892 case Instruction::ADD_INT_LIT16:
2893 case Instruction::RSUB_INT:
2894 case Instruction::MUL_INT_LIT16:
2895 case Instruction::DIV_INT_LIT16:
2896 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899 case Instruction::AND_INT_LIT16:
2900 case Instruction::OR_INT_LIT16:
2901 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002903 break;
2904 case Instruction::ADD_INT_LIT8:
2905 case Instruction::RSUB_INT_LIT8:
2906 case Instruction::MUL_INT_LIT8:
2907 case Instruction::DIV_INT_LIT8:
2908 case Instruction::REM_INT_LIT8:
2909 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002910 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002911 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002913 break;
2914 case Instruction::AND_INT_LIT8:
2915 case Instruction::OR_INT_LIT8:
2916 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002918 break;
2919
2920 /*
2921 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002922 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002923 * inserted in the course of verification, we can expect to see it here.
2924 */
jeffhaob4df5142011-09-19 20:25:32 -07002925 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002926 break;
2927
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002929 case Instruction::UNUSED_EE:
2930 case Instruction::UNUSED_EF:
2931 case Instruction::UNUSED_F2:
2932 case Instruction::UNUSED_F3:
2933 case Instruction::UNUSED_F4:
2934 case Instruction::UNUSED_F5:
2935 case Instruction::UNUSED_F6:
2936 case Instruction::UNUSED_F7:
2937 case Instruction::UNUSED_F8:
2938 case Instruction::UNUSED_F9:
2939 case Instruction::UNUSED_FA:
2940 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002941 case Instruction::UNUSED_F0:
2942 case Instruction::UNUSED_F1:
2943 case Instruction::UNUSED_E3:
2944 case Instruction::UNUSED_E8:
2945 case Instruction::UNUSED_E7:
2946 case Instruction::UNUSED_E4:
2947 case Instruction::UNUSED_E9:
2948 case Instruction::UNUSED_FC:
2949 case Instruction::UNUSED_E5:
2950 case Instruction::UNUSED_EA:
2951 case Instruction::UNUSED_FD:
2952 case Instruction::UNUSED_E6:
2953 case Instruction::UNUSED_EB:
2954 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002955 case Instruction::UNUSED_3E:
2956 case Instruction::UNUSED_3F:
2957 case Instruction::UNUSED_40:
2958 case Instruction::UNUSED_41:
2959 case Instruction::UNUSED_42:
2960 case Instruction::UNUSED_43:
2961 case Instruction::UNUSED_73:
2962 case Instruction::UNUSED_79:
2963 case Instruction::UNUSED_7A:
2964 case Instruction::UNUSED_EC:
2965 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002967 break;
2968
2969 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002970 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002971 * complain if an instruction is missing (which is desirable).
2972 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002973 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002974
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 if (failure_ != VERIFY_ERROR_NONE) {
jeffhaod5347e02012-03-22 17:25:05 -07002976 if (failure_ == VERIFY_ERROR_BAD_CLASS_HARD || failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) {
jeffhaobdb76512011-09-07 11:43:16 -07002977 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002978 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002979 return false;
2980 } else {
2981 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002982 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002984 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002986 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 failure_ = VERIFY_ERROR_NONE;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002988 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002989 }
2990 }
jeffhaobdb76512011-09-07 11:43:16 -07002991 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 * If we didn't just set the result register, clear it out. This ensures that you can only use
2993 * "move-result" immediately after the result is set. (We could check this statically, but it's
2994 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002995 */
2996 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002998 }
2999
jeffhaoa0a764a2011-09-16 10:43:38 -07003000 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003001 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
3003 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07003004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07003005 return false;
3006 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3008 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07003009 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07003010 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003011 }
3012 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
3013 if (next_line != NULL) {
3014 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3015 // needed.
3016 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003017 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 }
jeffhaobdb76512011-09-07 11:43:16 -07003019 } else {
3020 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 * We're not recording register data for the next instruction, so we don't know what the prior
3022 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07003023 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003025 }
3026 }
3027
3028 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003029 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003030 *
3031 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003032 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003033 * somebody could get a reference field, check it for zero, and if the
3034 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003035 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003036 * that, and will reject the code.
3037 *
3038 * TODO: avoid re-fetching the branch target
3039 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003040 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003041 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003043 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003045 return false;
3046 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003047 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07003048 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003049 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 }
jeffhaobdb76512011-09-07 11:43:16 -07003051 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003053 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 }
jeffhaobdb76512011-09-07 11:43:16 -07003055 }
3056
3057 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003058 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003059 *
3060 * We've already verified that the table is structurally sound, so we
3061 * just need to walk through and tag the targets.
3062 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003063 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003064 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3065 const uint16_t* switch_insns = insns + offset_to_switch;
3066 int switch_count = switch_insns[1];
3067 int offset_to_targets, targ;
3068
3069 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3070 /* 0 = sig, 1 = count, 2/3 = first key */
3071 offset_to_targets = 4;
3072 } else {
3073 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003074 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003075 offset_to_targets = 2 + 2 * switch_count;
3076 }
3077
3078 /* verify each switch target */
3079 for (targ = 0; targ < switch_count; targ++) {
3080 int offset;
3081 uint32_t abs_offset;
3082
3083 /* offsets are 32-bit, and only partly endian-swapped */
3084 offset = switch_insns[offset_to_targets + targ * 2] |
3085 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 abs_offset = work_insn_idx_ + offset;
3087 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07003088 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003089 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 }
3091 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07003092 return false;
3093 }
3094 }
3095
3096 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3098 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003099 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003100 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003102 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003103
Ian Rogers0571d352011-11-03 19:51:38 -07003104 for (; iterator.HasNext(); iterator.Next()) {
3105 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 within_catch_all = true;
3107 }
jeffhaobdb76512011-09-07 11:43:16 -07003108 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3110 * "work_regs", because at runtime the exception will be thrown before the instruction
3111 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003112 */
Ian Rogers0571d352011-11-03 19:51:38 -07003113 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003114 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 }
jeffhaobdb76512011-09-07 11:43:16 -07003116 }
3117
3118 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3120 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003121 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003123 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003124 * The state in work_line reflects the post-execution state. If the current instruction is a
3125 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003126 * it will do so before grabbing the lock).
3127 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003129 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003131 return false;
3132 }
3133 }
3134 }
3135
jeffhaod1f0fde2011-09-08 17:25:33 -07003136 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003137 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 if(!work_line_->VerifyMonitorStackEmpty()) {
3139 return false;
3140 }
jeffhaobdb76512011-09-07 11:43:16 -07003141 }
3142
3143 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003144 * Update start_guess. Advance to the next instruction of that's
3145 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003146 * neither of those exists we're in a return or throw; leave start_guess
3147 * alone and let the caller sort it out.
3148 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003149 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003151 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003152 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003154 }
3155
Ian Rogersd81871c2011-10-03 13:57:23 -07003156 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3157 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003158
3159 return true;
3160}
3161
Ian Rogers28ad40d2011-10-27 15:19:26 -07003162const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003163 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003164 Class* referrer = method_->GetDeclaringClass();
3165 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3166 const RegType& result =
3167 klass != NULL ? reg_types_.FromClass(klass)
3168 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3169 if (klass == NULL && !result.IsUnresolvedTypes()) {
3170 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 }
jeffhaod1224c72012-02-29 13:43:08 -08003172 if (result.IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07003173 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing unknown class in " << PrettyDescriptor(referrer);
jeffhaod1224c72012-02-29 13:43:08 -08003174 return result;
3175 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003176 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3177 // check at runtime if access is allowed and so pass here.
3178 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3179 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003180 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003181 << result << "'";
3182 return reg_types_.Unknown();
3183 } else {
3184 return result;
3185 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003186}
3187
Ian Rogers28ad40d2011-10-27 15:19:26 -07003188const RegType& DexVerifier::GetCaughtExceptionType() {
3189 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003191 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3193 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003194 CatchHandlerIterator iterator(handlers_ptr);
3195 for (; iterator.HasNext(); iterator.Next()) {
3196 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3197 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003198 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003200 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003201 if (common_super == NULL) {
3202 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3203 // as that is caught at runtime
3204 common_super = &exception;
3205 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3206 // We don't know enough about the type and the common path merge will result in
3207 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07003208 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003209 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003210 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003211 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003213 common_super = &common_super->Merge(exception, &reg_types_);
3214 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 }
3216 }
3217 }
3218 }
Ian Rogers0571d352011-11-03 19:51:38 -07003219 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 }
3221 }
3222 if (common_super == NULL) {
3223 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003224 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003225 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003227 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003228}
3229
jeffhao8cd6dda2012-02-22 10:15:34 -08003230Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003231 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3232 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3233 if (failure_ != VERIFY_ERROR_NONE) {
3234 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3235 return NULL;
3236 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003237 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003238 return NULL; // Can't resolve Class so no more to do here
3239 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003240 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 Class* referrer = method_->GetDeclaringClass();
3242 DexCache* dex_cache = referrer->GetDexCache();
3243 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3244 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003245 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003246 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003247
3248 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003250 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 res_method = klass->FindInterfaceMethod(name, signature);
3252 } else {
3253 res_method = klass->FindVirtualMethod(name, signature);
3254 }
3255 if (res_method != NULL) {
3256 dex_cache->SetResolvedMethod(method_idx, res_method);
3257 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003258 // If a virtual or interface method wasn't found with the expected type, look in
3259 // the direct methods. This can happen when the wrong invoke type is used or when
3260 // a class has changed, and will be flagged as an error in later checks.
3261 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3262 res_method = klass->FindDirectMethod(name, signature);
3263 }
3264 if (res_method == NULL) {
3265 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3266 << PrettyDescriptor(klass) << "." << name
3267 << " " << signature;
3268 return NULL;
3269 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 }
3271 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3273 // enforce them here.
3274 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3276 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 return NULL;
3278 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003279 // Disallow any calls to class initializers.
3280 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3282 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003283 return NULL;
3284 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003285 // Check if access is allowed.
3286 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3287 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3288 << " from " << PrettyDescriptor(referrer) << ")";
3289 return NULL;
3290 }
jeffhaode0d9c92012-02-27 13:58:13 -08003291 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3292 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3294 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003295 return NULL;
3296 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003297 // Check that interface methods match interface classes.
3298 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3299 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3300 << " is in an interface class " << PrettyClass(klass);
3301 return NULL;
3302 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3303 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3304 << " is in a non-interface class " << PrettyClass(klass);
3305 return NULL;
3306 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 // See if the method type implied by the invoke instruction matches the access flags for the
3308 // target method.
3309 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3310 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3311 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3312 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003313 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3314 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 return NULL;
3316 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003317 return res_method;
3318}
3319
Elliott Hughesadb8c672012-03-06 16:49:32 -08003320Method* DexVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08003321 MethodType method_type, bool is_range, bool is_super) {
3322 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3323 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003324 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003325 if (res_method == NULL) { // error or class is unresolved
3326 return NULL;
3327 }
3328
Ian Rogersd81871c2011-10-03 13:57:23 -07003329 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3330 // has a vtable entry for the target method.
3331 if (is_super) {
3332 DCHECK(method_type == METHOD_VIRTUAL);
3333 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003334 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 if (super == NULL) { // Only Object has no super class
3336 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3337 << " to super " << PrettyMethod(res_method);
3338 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003339 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003341 << " to super " << PrettyDescriptor(super)
3342 << "." << mh.GetName()
3343 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 }
3345 return NULL;
3346 }
3347 }
3348 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3349 // match the call to the signature. Also, we might might be calling through an abstract method
3350 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08003351 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 /* caught by static verifier */
3353 DCHECK(is_range || expected_args <= 5);
3354 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3357 return NULL;
3358 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003359
jeffhaobdb76512011-09-07 11:43:16 -07003360 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 * Check the "this" argument, which must be an instance of the class
3362 * that declared the method. For an interface class, we don't do the
3363 * full interface merge, so we can't do a rigorous check here (which
3364 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003365 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003366 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003367 if (!res_method->IsStatic()) {
3368 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3369 if (failure_ != VERIFY_ERROR_NONE) {
3370 return NULL;
3371 }
3372 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 return NULL;
3375 }
3376 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003377 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3378 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003379 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003380 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003381 return NULL;
3382 }
3383 }
3384 actual_args++;
3385 }
3386 /*
3387 * Process the target method's signature. This signature may or may not
3388 * have been verified, so we can't assume it's properly formed.
3389 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003390 MethodHelper mh(res_method);
3391 const DexFile::TypeList* params = mh.GetParameterTypeList();
3392 size_t params_size = params == NULL ? 0 : params->Size();
3393 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003396 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3397 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 return NULL;
3399 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003400 const char* descriptor =
3401 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3402 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003404 << " missing signature component";
3405 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 }
3407 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003408 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003409 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003410 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3411 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003412 }
3413 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3414 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003417 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 return NULL;
3419 } else {
3420 return res_method;
3421 }
3422}
3423
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003424const RegType& DexVerifier::GetMethodReturnType() {
3425 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3426 MethodHelper(method_).GetReturnTypeDescriptor());
3427}
3428
Elliott Hughesadb8c672012-03-06 16:49:32 -08003429void DexVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08003430 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003431 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003432 if (res_type.IsUnknown()) {
3433 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3434 } else {
3435 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3436 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003438 } else if (!is_filled) {
3439 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003440 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003441 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003442 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003443 } else {
3444 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3445 // the list and fail. It's legal, if silly, for arg_count to be zero.
3446 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3447 method_->GetDeclaringClass()->GetClassLoader());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003449 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003450 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003451 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3452 work_line_->SetResultRegisterType(reg_types_.Unknown());
3453 return;
3454 }
3455 }
3456 // filled-array result goes into "result" register
3457 work_line_->SetResultRegisterType(res_type);
3458 }
3459 }
3460}
3461
Elliott Hughesadb8c672012-03-06 16:49:32 -08003462void DexVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003464 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003468 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003469 if (array_type.IsZero()) {
3470 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3471 // instruction type. TODO: have a proper notion of bottom here.
3472 if (!is_primitive || insn_type.IsCategory1Types()) {
3473 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08003474 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003476 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08003477 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08003478 }
jeffhaofc3144e2012-02-01 17:21:15 -08003479 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003481 } else {
3482 /* verify the class */
3483 const RegType& component_type = reg_types_.GetComponentType(array_type,
3484 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003485 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003487 << " source for aget-object";
3488 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003490 << " source for category 1 aget";
3491 } else if (is_primitive && !insn_type.Equals(component_type) &&
3492 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3493 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003496 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 // Use knowledge of the field type which is stronger than the type inferred from the
3498 // instruction, which can't differentiate object types and ints from floats, longs from
3499 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003500 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 }
3502 }
3503 }
3504}
3505
Elliott Hughesadb8c672012-03-06 16:49:32 -08003506void DexVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003507 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003508 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003512 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003513 if (array_type.IsZero()) {
3514 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3515 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003516 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003518 } else {
3519 /* verify the class */
3520 const RegType& component_type = reg_types_.GetComponentType(array_type,
3521 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003522 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003524 << " source for aput-object";
3525 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003527 << " source for category 1 aput";
3528 } else if (is_primitive && !insn_type.Equals(component_type) &&
3529 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3530 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003532 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003533 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003534 // The instruction agrees with the type of array, confirm the value to be stored does too
3535 // Note: we use the instruction type (rather than the component type) for aput-object as
3536 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08003537 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 }
3539 }
3540 }
3541}
3542
3543Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003544 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3545 // Check access to class
3546 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3547 if (failure_ != VERIFY_ERROR_NONE) {
3548 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3549 << dex_file_->GetFieldName(field_id) << ") in "
3550 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3551 return NULL;
3552 }
3553 if(klass_type.IsUnresolvedTypes()) {
3554 return NULL; // Can't resolve Class so no more to do here
3555 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003556 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003558 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3559 << dex_file_->GetFieldName(field_id) << ") in "
3560 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003561 DCHECK(Thread::Current()->IsExceptionPending());
3562 Thread::Current()->ClearException();
3563 return NULL;
3564 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3565 field->GetAccessFlags())) {
3566 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3567 << " from " << PrettyClass(method_->GetDeclaringClass());
3568 return NULL;
3569 } else if (!field->IsStatic()) {
3570 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3571 return NULL;
3572 } else {
3573 return field;
3574 }
3575}
3576
Ian Rogersd81871c2011-10-03 13:57:23 -07003577Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003578 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3579 // Check access to class
3580 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3581 if (failure_ != VERIFY_ERROR_NONE) {
3582 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3583 << dex_file_->GetFieldName(field_id) << ") in "
3584 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3585 return NULL;
3586 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003587 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003588 return NULL; // Can't resolve Class so no more to do here
3589 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003590 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003592 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3593 << dex_file_->GetFieldName(field_id) << ") in "
3594 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003595 DCHECK(Thread::Current()->IsExceptionPending());
3596 Thread::Current()->ClearException();
3597 return NULL;
3598 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3599 field->GetAccessFlags())) {
3600 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3601 << " from " << PrettyClass(method_->GetDeclaringClass());
3602 return NULL;
3603 } else if (field->IsStatic()) {
3604 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3605 << " to not be static";
3606 return NULL;
3607 } else if (obj_type.IsZero()) {
3608 // Cannot infer and check type, however, access will cause null pointer exception
3609 return field;
jeffhao8cd6dda2012-02-22 10:15:34 -08003610 } else if(obj_type.IsUninitializedTypes() &&
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3612 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3613 // Field accesses through uninitialized references are only allowable for constructors where
3614 // the field is declared in this class
jeffhaod5347e02012-03-22 17:25:05 -07003615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3616 << " of a not fully initialized object within the context of "
3617 << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 return NULL;
3619 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3620 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3621 // of C1. For resolution to occur the declared class of the field must be compatible with
3622 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3623 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3624 << " from object of type " << PrettyClass(obj_type.GetClass());
3625 return NULL;
3626 } else {
3627 return field;
3628 }
3629}
3630
Elliott Hughesadb8c672012-03-06 16:49:32 -08003631void DexVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003632 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003633 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003634 Field* field;
3635 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003636 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003637 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003638 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003639 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003640 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003641 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003642 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003643 } else {
3644 const char* descriptor;
3645 const ClassLoader* loader;
3646 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003647 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003648 loader = field->GetDeclaringClass()->GetClassLoader();
3649 } else {
3650 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3651 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3652 loader = method_->GetDeclaringClass()->GetClassLoader();
3653 }
3654 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003656 if (field_type.Equals(insn_type) ||
3657 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3658 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003659 // expected that read is of the correct primitive type or that int reads are reading
3660 // floats or long reads are reading doubles
3661 } else {
3662 // This is a global failure rather than a class change failure as the instructions and
3663 // the descriptors for the type should have been consistent within the same file at
3664 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3666 << " to be of type '" << insn_type
3667 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 return;
3669 }
3670 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003671 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003672 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3673 << " to be compatible with type '" << insn_type
3674 << "' but found type '" << field_type
3675 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003676 return;
3677 }
3678 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003679 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 }
3681}
3682
Elliott Hughesadb8c672012-03-06 16:49:32 -08003683void DexVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003684 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003685 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003686 Field* field;
3687 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003688 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003689 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003690 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003691 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003692 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003693 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003694 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogers55d249f2011-11-02 16:48:09 -07003695 } else {
3696 const char* descriptor;
3697 const ClassLoader* loader;
3698 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003699 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003700 loader = field->GetDeclaringClass()->GetClassLoader();
3701 } else {
3702 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3703 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3704 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003705 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003706 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3707 if (field != NULL) {
3708 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3709 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3710 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3711 return;
3712 }
3713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003715 // Primitive field assignability rules are weaker than regular assignability rules
3716 bool instruction_compatible;
3717 bool value_compatible;
Elliott Hughesadb8c672012-03-06 16:49:32 -08003718 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers2c8a8572011-10-24 17:11:36 -07003719 if (field_type.IsIntegralTypes()) {
3720 instruction_compatible = insn_type.IsIntegralTypes();
3721 value_compatible = value_type.IsIntegralTypes();
3722 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003723 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003724 value_compatible = value_type.IsFloatTypes();
3725 } else if (field_type.IsLong()) {
3726 instruction_compatible = insn_type.IsLong();
3727 value_compatible = value_type.IsLongTypes();
3728 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003729 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003730 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003731 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003732 instruction_compatible = false; // reference field with primitive store
3733 value_compatible = false; // unused
3734 }
3735 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003736 // This is a global failure rather than a class change failure as the instructions and
3737 // the descriptors for the type should have been consistent within the same file at
3738 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3740 << " to be of type '" << insn_type
3741 << "' but found type '" << field_type
3742 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 return;
3744 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003745 if (!value_compatible) {
jeffhaod5347e02012-03-22 17:25:05 -07003746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3747 << " of type " << value_type
3748 << " but expected " << field_type
3749 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003750 return;
3751 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003752 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003753 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003754 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3755 << " to be compatible with type '" << insn_type
3756 << "' but found type '" << field_type
3757 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 return;
3759 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003760 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 }
3763}
3764
jeffhaod5347e02012-03-22 17:25:05 -07003765bool DexVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 return false;
3769 }
3770 return true;
3771}
3772
Ian Rogersd81871c2011-10-03 13:57:23 -07003773void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003774 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003775 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003776 << " " << fail_messages_.str();
3777 return;
3778 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3780 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3781 VerifyErrorRefType ref_type;
3782 switch (inst->Opcode()) {
3783 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003784 case Instruction::CHECK_CAST:
3785 case Instruction::INSTANCE_OF:
3786 case Instruction::NEW_INSTANCE:
3787 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003789 case Instruction::FILLED_NEW_ARRAY_RANGE:
3790 ref_type = VERIFY_ERROR_REF_CLASS;
3791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003793 case Instruction::IGET_BOOLEAN:
3794 case Instruction::IGET_BYTE:
3795 case Instruction::IGET_CHAR:
3796 case Instruction::IGET_SHORT:
3797 case Instruction::IGET_WIDE:
3798 case Instruction::IGET_OBJECT:
3799 case Instruction::IPUT:
3800 case Instruction::IPUT_BOOLEAN:
3801 case Instruction::IPUT_BYTE:
3802 case Instruction::IPUT_CHAR:
3803 case Instruction::IPUT_SHORT:
3804 case Instruction::IPUT_WIDE:
3805 case Instruction::IPUT_OBJECT:
3806 case Instruction::SGET:
3807 case Instruction::SGET_BOOLEAN:
3808 case Instruction::SGET_BYTE:
3809 case Instruction::SGET_CHAR:
3810 case Instruction::SGET_SHORT:
3811 case Instruction::SGET_WIDE:
3812 case Instruction::SGET_OBJECT:
3813 case Instruction::SPUT:
3814 case Instruction::SPUT_BOOLEAN:
3815 case Instruction::SPUT_BYTE:
3816 case Instruction::SPUT_CHAR:
3817 case Instruction::SPUT_SHORT:
3818 case Instruction::SPUT_WIDE:
3819 case Instruction::SPUT_OBJECT:
3820 ref_type = VERIFY_ERROR_REF_FIELD;
3821 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003823 case Instruction::INVOKE_VIRTUAL_RANGE:
3824 case Instruction::INVOKE_SUPER:
3825 case Instruction::INVOKE_SUPER_RANGE:
3826 case Instruction::INVOKE_DIRECT:
3827 case Instruction::INVOKE_DIRECT_RANGE:
3828 case Instruction::INVOKE_STATIC:
3829 case Instruction::INVOKE_STATIC_RANGE:
3830 case Instruction::INVOKE_INTERFACE:
3831 case Instruction::INVOKE_INTERFACE_RANGE:
3832 ref_type = VERIFY_ERROR_REF_METHOD;
3833 break;
jeffhaobdb76512011-09-07 11:43:16 -07003834 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003835 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003836 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3839 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3840 // instruction, so assert it.
3841 size_t width = inst->SizeInCodeUnits();
3842 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003843 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 // NOPs
3845 for (size_t i = 2; i < width; i++) {
3846 insns[work_insn_idx_ + i] = Instruction::NOP;
3847 }
3848 // Encode the opcode, with the failure code in the high byte
3849 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3850 (failure_ << 8) | // AA - component
3851 (ref_type << (8 + kVerifyErrorRefTypeShift));
3852 insns[work_insn_idx_] = new_instruction;
3853 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3854 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003855 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3856 << fail_messages_.str();
3857 if (gDebugVerify) {
3858 std::cout << std::endl << info_messages_.str();
3859 Dump(std::cout);
3860 }
jeffhaobdb76512011-09-07 11:43:16 -07003861}
jeffhaoba5ebb92011-08-25 17:24:37 -07003862
Ian Rogersd81871c2011-10-03 13:57:23 -07003863bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003864 bool changed = true;
3865 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3866 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003867 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 * We haven't processed this instruction before, and we haven't touched the registers here, so
3869 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3870 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003871 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003873 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003874 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3875 if (gDebugVerify) {
3876 copy->CopyFromLine(target_line);
3877 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003878 changed = target_line->MergeRegisters(merge_line);
3879 if (failure_ != VERIFY_ERROR_NONE) {
3880 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003881 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003882 if (gDebugVerify && changed) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003883 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "]"
3884 " to [" <<(void*)next_insn << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003885 << *copy.get() << " MERGE" << std::endl
3886 << *merge_line << " ==" << std::endl
3887 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003888 }
3889 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 if (changed) {
3891 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003892 }
3893 return true;
3894}
3895
Ian Rogersd81871c2011-10-03 13:57:23 -07003896void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3897 size_t* log2_max_gc_pc) {
3898 size_t local_gc_points = 0;
3899 size_t max_insn = 0;
3900 size_t max_ref_reg = -1;
3901 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3902 if (insn_flags_[i].IsGcPoint()) {
3903 local_gc_points++;
3904 max_insn = i;
3905 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003906 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003907 }
3908 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003909 *gc_points = local_gc_points;
3910 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3911 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003912 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003913 i++;
3914 }
3915 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003916}
3917
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003918const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003919 size_t num_entries, ref_bitmap_bits, pc_bits;
3920 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3921 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003922 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003923 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003926 return NULL;
3927 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003928 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3929 // There are 2 bytes to encode the number of entries
3930 if (num_entries >= 65536) {
3931 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003933 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003934 return NULL;
3935 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003937 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003938 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003939 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003940 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003941 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003942 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003943 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003944 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003947 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3948 return NULL;
3949 }
3950 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003951 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003952 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003954 return NULL;
3955 }
3956 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003957 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3958 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003959 table->push_back(num_entries & 0xFF);
3960 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003961 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003962 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3963 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003964 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003965 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003966 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003967 }
3968 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003969 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003970 }
3971 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003972 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003973 return table;
3974}
jeffhaoa0a764a2011-09-16 10:43:38 -07003975
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003976void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003977 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3978 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003979 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003980 size_t map_index = 0;
3981 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3982 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3983 if (insn_flags_[i].IsGcPoint()) {
3984 CHECK_LT(map_index, map.NumEntries());
3985 CHECK_EQ(map.GetPC(map_index), i);
3986 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3987 map_index++;
3988 RegisterLine* line = reg_table_.GetLine(i);
3989 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003990 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 CHECK_LT(j / 8, map.RegWidth());
3992 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3993 } else if ((j / 8) < map.RegWidth()) {
3994 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3995 } else {
3996 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3997 }
3998 }
3999 } else {
4000 CHECK(reg_bitmap == NULL);
4001 }
4002 }
4003}
jeffhaoa0a764a2011-09-16 10:43:38 -07004004
Ian Rogersd81871c2011-10-03 13:57:23 -07004005const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
4006 size_t num_entries = NumEntries();
4007 // Do linear or binary search?
4008 static const size_t kSearchThreshold = 8;
4009 if (num_entries < kSearchThreshold) {
4010 for (size_t i = 0; i < num_entries; i++) {
4011 if (GetPC(i) == dex_pc) {
4012 return GetBitMap(i);
4013 }
4014 }
4015 } else {
4016 int lo = 0;
4017 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07004018 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 int mid = (hi + lo) / 2;
4020 int mid_pc = GetPC(mid);
4021 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004022 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004024 hi = mid - 1;
4025 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004026 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07004027 }
4028 }
4029 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004030 if (error_if_not_present) {
4031 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
4032 }
jeffhaoa0a764a2011-09-16 10:43:38 -07004033 return NULL;
4034}
4035
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004036Mutex* DexVerifier::gc_maps_lock_ = NULL;
4037DexVerifier::GcMapTable* DexVerifier::gc_maps_ = NULL;
4038
4039void DexVerifier::InitGcMaps() {
4040 gc_maps_lock_ = new Mutex("verifier GC maps lock");
4041 MutexLock mu(*gc_maps_lock_);
4042 gc_maps_ = new DexVerifier::GcMapTable;
4043}
4044
4045void DexVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07004046 {
4047 MutexLock mu(*gc_maps_lock_);
4048 STLDeleteValues(gc_maps_);
4049 delete gc_maps_;
4050 gc_maps_ = NULL;
4051 }
4052 delete gc_maps_lock_;
4053 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004054}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004055
4056void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004057 MutexLock mu(*gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004058 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
4059 if (existing_gc_map != NULL) {
4060 CHECK(*existing_gc_map == gc_map);
4061 delete existing_gc_map;
4062 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004063 (*gc_maps_)[ref] = &gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004064 CHECK(GetGcMap(ref) != NULL);
4065}
4066
4067const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004068 MutexLock mu(*gc_maps_lock_);
4069 GcMapTable::const_iterator it = gc_maps_->find(ref);
4070 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004071 return NULL;
4072 }
4073 CHECK(it->second != NULL);
4074 return it->second;
4075}
4076
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004077static Mutex& GetRejectedClassesLock() {
4078 static Mutex rejected_classes_lock("verifier rejected classes lock");
4079 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004080}
4081
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004082static std::set<Compiler::ClassReference>& GetRejectedClasses() {
4083 static std::set<Compiler::ClassReference> rejected_classes;
4084 return rejected_classes;
4085}
jeffhaod1224c72012-02-29 13:43:08 -08004086
4087void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004088 MutexLock mu(GetRejectedClassesLock());
4089 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08004090 CHECK(IsClassRejected(ref));
4091}
4092
4093bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004094 MutexLock mu(GetRejectedClassesLock());
4095 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
4096 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08004097}
4098
Logan Chienfca7e872011-12-20 20:08:22 +08004099#if defined(ART_USE_LLVM_COMPILER)
4100InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
4101 uint32_t insns_size = code_item_->insns_size_in_code_units_;
4102 uint16_t regs_size = code_item_->registers_size_;
4103
4104 UniquePtr<InferredRegCategoryMap> table(
4105 new InferredRegCategoryMap(insns_size, regs_size));
4106
4107 for (size_t i = 0; i < insns_size; ++i) {
4108 if (RegisterLine* line = reg_table_.GetLine(i)) {
4109 for (size_t r = 0; r < regs_size; ++r) {
4110 RegType const &rt = line->GetRegisterType(r);
4111
4112 if (rt.IsZero()) {
4113 table->SetRegCategory(i, r, kRegZero);
4114 } else if (rt.IsCategory1Types()) {
4115 table->SetRegCategory(i, r, kRegCat1nr);
4116 } else if (rt.IsCategory2Types()) {
4117 table->SetRegCategory(i, r, kRegCat2);
4118 } else if (rt.IsReferenceTypes()) {
4119 table->SetRegCategory(i, r, kRegObject);
4120 } else {
4121 table->SetRegCategory(i, r, kRegUnknown);
4122 }
4123 }
4124 }
4125 }
4126
4127 return table.release();
4128}
4129#endif
4130
Ian Rogersd81871c2011-10-03 13:57:23 -07004131} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004132} // namespace art