blob: 987777ce6ecff858cc40f8b2797fb6bc185d1a1a [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 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -070097 if (IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070098 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
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700246 } else if (IsConstant() && incoming_type.IsConstant()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700247 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 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700364 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
Ian Rogersd81871c2011-10-03 13:57:23 -0700365 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 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700397 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();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700613 if (new_type.IsLowHalf()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 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();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700824 if (!IsSetLockDepth(reg_idx, monitors_.size())) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 // 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 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -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_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001264 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 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];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001472 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 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) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<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)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<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 << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001505 << reinterpret_cast<void*>(abs_offset) << ") at "
1506 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 return false;
1508 }
1509 insn_flags_[abs_offset].SetBranchTarget();
1510 return true;
1511}
1512
1513bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1514 bool* selfOkay) {
1515 const uint16_t* insns = code_item_->insns_ + cur_offset;
1516 *pConditional = false;
1517 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 switch (*insns & 0xff) {
1519 case Instruction::GOTO:
1520 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001521 break;
1522 case Instruction::GOTO_32:
1523 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 *selfOkay = true;
1525 break;
1526 case Instruction::GOTO_16:
1527 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 break;
1529 case Instruction::IF_EQ:
1530 case Instruction::IF_NE:
1531 case Instruction::IF_LT:
1532 case Instruction::IF_GE:
1533 case Instruction::IF_GT:
1534 case Instruction::IF_LE:
1535 case Instruction::IF_EQZ:
1536 case Instruction::IF_NEZ:
1537 case Instruction::IF_LTZ:
1538 case Instruction::IF_GEZ:
1539 case Instruction::IF_GTZ:
1540 case Instruction::IF_LEZ:
1541 *pOffset = (int16_t) insns[1];
1542 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 break;
1544 default:
1545 return false;
1546 break;
1547 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 return true;
1549}
1550
Ian Rogersd81871c2011-10-03 13:57:23 -07001551bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1552 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001553 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1557 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
1559 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001560 return false;
1561 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001562 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 /* make sure the table is 32-bit aligned */
1565 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1567 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001568 return false;
1569 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 uint32_t switch_count = switch_insns[1];
1571 int32_t keys_offset, targets_offset;
1572 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001573 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1574 /* 0=sig, 1=count, 2/3=firstKey */
1575 targets_offset = 4;
1576 keys_offset = -1;
1577 expected_signature = Instruction::kPackedSwitchSignature;
1578 } else {
1579 /* 0=sig, 1=count, 2..count*2 = keys */
1580 keys_offset = 2;
1581 targets_offset = 2 + 2 * switch_count;
1582 expected_signature = Instruction::kSparseSwitchSignature;
1583 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001585 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -07001586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1587 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001588 return false;
1589 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001590 /* make sure the end of the switch is in range */
1591 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
1593 << switch_offset << ", end "
1594 << (cur_offset + switch_offset + table_size)
1595 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001596 return false;
1597 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001598 /* for a sparse switch, verify the keys are in ascending order */
1599 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1601 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001602 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1603 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1604 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1606 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001607 return false;
1608 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001609 last_key = key;
1610 }
1611 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001612 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 for (uint32_t targ = 0; targ < switch_count; targ++) {
1614 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1615 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1616 int32_t abs_offset = cur_offset + offset;
1617 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001619 << reinterpret_cast<void*>(abs_offset) << ") at "
1620 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001621 return false;
1622 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 insn_flags_[abs_offset].SetBranchTarget();
1624 }
1625 return true;
1626}
1627
1628bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1629 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 return false;
1632 }
1633 uint16_t registers_size = code_item_->registers_size_;
1634 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001635 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1637 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 return false;
1639 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001640 }
1641
1642 return true;
1643}
1644
Ian Rogersd81871c2011-10-03 13:57:23 -07001645bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1646 uint16_t registers_size = code_item_->registers_size_;
1647 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1648 // integer overflow when adding them here.
1649 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1651 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001652 return false;
1653 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001654 return true;
1655}
1656
Brian Carlstrom75412882012-01-18 01:26:54 -08001657const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1658 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1659 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1660 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1661 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1662 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1663 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1664 gc_map.begin(),
1665 gc_map.end());
1666 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1667 DCHECK_EQ(gc_map.size(),
1668 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1669 (length_prefixed_gc_map->at(1) << 16) |
1670 (length_prefixed_gc_map->at(2) << 8) |
1671 (length_prefixed_gc_map->at(3) << 0)));
1672 return length_prefixed_gc_map;
1673}
1674
Ian Rogersd81871c2011-10-03 13:57:23 -07001675bool DexVerifier::VerifyCodeFlow() {
1676 uint16_t registers_size = code_item_->registers_size_;
1677 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001678
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001680 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1681 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 }
1683 /* Create and initialize table holding register status */
1684 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1685 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001686
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 work_line_.reset(new RegisterLine(registers_size, this));
1688 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001689
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 /* Initialize register types of method arguments. */
1691 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001692 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1693 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 return false;
1695 }
1696 /* Perform code flow verification. */
1697 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001698 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001700 }
1701
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001703 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1704 if (map.get() == NULL) {
1705 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 return false; // Not a real failure, but a failure to encode
1707 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001708#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001709 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001710#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001711 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1712 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1713 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001714
1715#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom75412882012-01-18 01:26:54 -08001716 method_->SetGcMap(&gc_map->at(0));
Logan Chienfca7e872011-12-20 20:08:22 +08001717#else
1718 /* Generate Inferred Register Category for LLVM-based Code Generator */
1719 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
1720 method_->SetInferredRegCategoryMap(table);
1721#endif
1722
jeffhaobdb76512011-09-07 11:43:16 -07001723 return true;
1724}
1725
Ian Rogersd81871c2011-10-03 13:57:23 -07001726void DexVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001727 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 os << "Native method" << std::endl;
1729 return;
jeffhaobdb76512011-09-07 11:43:16 -07001730 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 DCHECK(code_item_ != NULL);
1732 const Instruction* inst = Instruction::At(code_item_->insns_);
1733 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1734 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001735 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001736 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1738 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001739 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001741 inst = inst->Next();
1742 }
jeffhaobdb76512011-09-07 11:43:16 -07001743}
1744
Ian Rogersd81871c2011-10-03 13:57:23 -07001745static bool IsPrimitiveDescriptor(char descriptor) {
1746 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001747 case 'I':
1748 case 'C':
1749 case 'S':
1750 case 'B':
1751 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001752 case 'F':
1753 case 'D':
1754 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001756 default:
1757 return false;
1758 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001759}
1760
Ian Rogersd81871c2011-10-03 13:57:23 -07001761bool DexVerifier::SetTypesFromSignature() {
1762 RegisterLine* reg_line = reg_table_.GetLine(0);
1763 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1764 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001765
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1767 //Include the "this" pointer.
1768 size_t cur_arg = 0;
1769 if (!method_->IsStatic()) {
1770 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1771 // argument as uninitialized. This restricts field access until the superclass constructor is
1772 // called.
1773 Class* declaring_class = method_->GetDeclaringClass();
1774 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1775 reg_line->SetRegisterType(arg_start + cur_arg,
1776 reg_types_.UninitializedThisArgument(declaring_class));
1777 } else {
1778 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001781 }
1782
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001783 const DexFile::ProtoId& proto_id =
1784 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001785 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001786
1787 for (; iterator.HasNext(); iterator.Next()) {
1788 const char* descriptor = iterator.GetDescriptor();
1789 if (descriptor == NULL) {
1790 LOG(FATAL) << "Null descriptor";
1791 }
1792 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1794 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 return false;
1796 }
1797 switch (descriptor[0]) {
1798 case 'L':
1799 case '[':
1800 // We assume that reference arguments are initialized. The only way it could be otherwise
1801 // (assuming the caller was verified) is if the current method is <init>, but in that case
1802 // it's effectively considered initialized the instant we reach here (in the sense that we
1803 // can return without doing anything or call virtual methods).
1804 {
1805 const RegType& reg_type =
1806 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001807 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 }
1809 break;
1810 case 'Z':
1811 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1812 break;
1813 case 'C':
1814 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1815 break;
1816 case 'B':
1817 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1818 break;
1819 case 'I':
1820 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1821 break;
1822 case 'S':
1823 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1824 break;
1825 case 'F':
1826 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1827 break;
1828 case 'J':
1829 case 'D': {
1830 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1831 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1832 cur_arg++;
1833 break;
1834 }
1835 default:
jeffhaod5347e02012-03-22 17:25:05 -07001836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 return false;
1838 }
1839 cur_arg++;
1840 }
1841 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 return false;
1844 }
1845 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1846 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1847 // format. Only major difference from the method argument format is that 'V' is supported.
1848 bool result;
1849 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1850 result = descriptor[1] == '\0';
1851 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1852 size_t i = 0;
1853 do {
1854 i++;
1855 } while (descriptor[i] == '['); // process leading [
1856 if (descriptor[i] == 'L') { // object array
1857 do {
1858 i++; // find closing ;
1859 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1860 result = descriptor[i] == ';';
1861 } else { // primitive array
1862 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1863 }
1864 } else if (descriptor[0] == 'L') {
1865 // could be more thorough here, but shouldn't be required
1866 size_t i = 0;
1867 do {
1868 i++;
1869 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1870 result = descriptor[i] == ';';
1871 } else {
1872 result = false;
1873 }
1874 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1876 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 }
1878 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001879}
1880
Ian Rogersd81871c2011-10-03 13:57:23 -07001881bool DexVerifier::CodeFlowVerifyMethod() {
1882 const uint16_t* insns = code_item_->insns_;
1883 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001884
jeffhaobdb76512011-09-07 11:43:16 -07001885 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 insn_flags_[0].SetChanged();
1887 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001888
jeffhaobdb76512011-09-07 11:43:16 -07001889 /* Continue until no instructions are marked "changed". */
1890 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1892 uint32_t insn_idx = start_guess;
1893 for (; insn_idx < insns_size; insn_idx++) {
1894 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
1896 }
jeffhaobdb76512011-09-07 11:43:16 -07001897 if (insn_idx == insns_size) {
1898 if (start_guess != 0) {
1899 /* try again, starting from the top */
1900 start_guess = 0;
1901 continue;
1902 } else {
1903 /* all flags are clear */
1904 break;
1905 }
1906 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 // We carry the working set of registers from instruction to instruction. If this address can
1908 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1909 // "changed" flags, we need to load the set of registers from the table.
1910 // Because we always prefer to continue on to the next instruction, we should never have a
1911 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1912 // target.
1913 work_insn_idx_ = insn_idx;
1914 if (insn_flags_[insn_idx].IsBranchTarget()) {
1915 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001916 } else {
1917#ifndef NDEBUG
1918 /*
1919 * Sanity check: retrieve the stored register line (assuming
1920 * a full table) and make sure it actually matches.
1921 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1923 if (register_line != NULL) {
1924 if (work_line_->CompareLine(register_line) != 0) {
1925 Dump(std::cout);
1926 std::cout << info_messages_.str();
1927 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
Elliott Hughes398f64b2012-03-26 18:05:48 -07001928 << "@" << reinterpret_cast<void*>(work_insn_idx_) << std::endl
1929 << " work_line=" << *work_line_ << std::endl
1930 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 }
1933#endif
1934 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 if (!CodeFlowVerifyInstruction(&start_guess)) {
1936 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001937 return false;
1938 }
jeffhaobdb76512011-09-07 11:43:16 -07001939 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 insn_flags_[insn_idx].SetVisited();
1941 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001942 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001943
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001945 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001947 * (besides the wasted space), but it indicates a flaw somewhere
1948 * down the line, possibly in the verifier.
1949 *
1950 * If we've substituted "always throw" instructions into the stream,
1951 * we are almost certainly going to have some dead code.
1952 */
1953 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 uint32_t insn_idx = 0;
1955 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001956 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001957 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001958 * may or may not be preceded by a padding NOP (for alignment).
1959 */
1960 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1961 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1962 insns[insn_idx] == Instruction::kArrayDataSignature ||
1963 (insns[insn_idx] == Instruction::NOP &&
1964 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1965 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1966 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001968 }
1969
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001971 if (dead_start < 0)
1972 dead_start = insn_idx;
1973 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001974 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001975 dead_start = -1;
1976 }
1977 }
1978 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001979 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001980 }
1981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 return true;
1983}
1984
Ian Rogersd81871c2011-10-03 13:57:23 -07001985bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001986#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001988 gDvm.verifierStats.instrsReexamined++;
1989 } else {
1990 gDvm.verifierStats.instrsExamined++;
1991 }
1992#endif
1993
1994 /*
1995 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001996 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001997 * control to another statement:
1998 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001999 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002000 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002001 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002002 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002003 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002004 * throw an exception that is handled by an encompassing "try"
2005 * block.
2006 *
2007 * We can also return, in which case there is no successor instruction
2008 * from this point.
2009 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002010 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002011 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2013 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002014 DecodedInstruction dec_insn(inst);
2015 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002016
jeffhaobdb76512011-09-07 11:43:16 -07002017 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002018 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07002019 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002021 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2022 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 }
jeffhaobdb76512011-09-07 11:43:16 -07002024
2025 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002026 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002027 * can throw an exception, we will copy/merge this into the "catch"
2028 * address rather than work_line, because we don't want the result
2029 * from the "successful" code path (e.g. a check-cast that "improves"
2030 * a type) to be visible to the exception handler.
2031 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002032 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07002034 } else {
2035#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002037#endif
2038 }
2039
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07002041 case Instruction::NOP:
2042 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002043 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002044 * a signature that looks like a NOP; if we see one of these in
2045 * the course of executing code then we have a problem.
2046 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002047 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002049 }
2050 break;
2051
2052 case Instruction::MOVE:
2053 case Instruction::MOVE_FROM16:
2054 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002055 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::MOVE_WIDE:
2058 case Instruction::MOVE_WIDE_FROM16:
2059 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002060 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::MOVE_OBJECT:
2063 case Instruction::MOVE_OBJECT_FROM16:
2064 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002065 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
2067
2068 /*
2069 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002070 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002071 * might want to hold the result in an actual CPU register, so the
2072 * Dalvik spec requires that these only appear immediately after an
2073 * invoke or filled-new-array.
2074 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002075 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002076 * redundant with the reset done below, but it can make the debug info
2077 * easier to read in some cases.)
2078 */
2079 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002080 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002083 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002086 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07002090 /*
jeffhao60f83e32012-02-13 17:16:30 -08002091 * This statement can only appear as the first instruction in an exception handler. We verify
2092 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002093 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002094 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002095 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 }
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2100 if (!GetMethodReturnType().IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07002101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 }
jeffhaobdb76512011-09-07 11:43:16 -07002103 }
2104 break;
2105 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002107 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 const RegType& return_type = GetMethodReturnType();
2109 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 } else {
2112 // Compilers may generate synthetic functions that write byte values into boolean fields.
2113 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002114 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2116 ((return_type.IsBoolean() || return_type.IsByte() ||
2117 return_type.IsShort() || return_type.IsChar()) &&
2118 src_type.IsInteger()));
2119 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002120 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002122 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 }
jeffhaobdb76512011-09-07 11:43:16 -07002124 }
2125 }
2126 break;
2127 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002129 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 const RegType& return_type = GetMethodReturnType();
2131 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 } else {
2134 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002135 work_line_->VerifyRegisterType(dec_insn.vA, return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002137 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 }
jeffhaobdb76512011-09-07 11:43:16 -07002139 }
2140 }
2141 break;
2142 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2144 const RegType& return_type = GetMethodReturnType();
2145 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 } else {
2148 /* return_type is the *expected* return type, not register value */
2149 DCHECK(!return_type.IsZero());
2150 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07002152 // Disallow returning uninitialized values and verify that the reference in vAA is an
2153 // instance of the "return_type"
2154 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002155 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002156 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07002158 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002159 }
2160 }
2161 }
2162 break;
2163
2164 case Instruction::CONST_4:
2165 case Instruction::CONST_16:
2166 case Instruction::CONST:
2167 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002168 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::CONST_HIGH16:
2171 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002172 work_line_->SetRegisterType(dec_insn.vA,
2173 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::CONST_WIDE_16:
2176 case Instruction::CONST_WIDE_32:
2177 case Instruction::CONST_WIDE:
2178 case Instruction::CONST_WIDE_HIGH16:
2179 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002180 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::CONST_STRING:
2183 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002184 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002187 // Get type from instruction if unresolved then we need an access check
2188 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08002189 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002190 // Register holds class, ie its type is class, but on error we keep it Unknown
Elliott Hughesadb8c672012-03-06 16:49:32 -08002191 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers28ad40d2011-10-27 15:19:26 -07002192 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 }
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002196 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198 case Instruction::MONITOR_EXIT:
2199 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002200 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002201 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002202 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002203 * to the need to handle asynchronous exceptions, a now-deprecated
2204 * feature that Dalvik doesn't support.)
2205 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002206 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002207 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002208 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002209 * structured locking checks are working, the former would have
2210 * failed on the -enter instruction, and the latter is impossible.
2211 *
2212 * This is fortunate, because issue 3221411 prevents us from
2213 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002214 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002215 * some catch blocks (which will show up as "dead" code when
2216 * we skip them here); if we can't, then the code path could be
2217 * "live" so we still need to check it.
2218 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002219 opcode_flags &= ~Instruction::kThrow;
2220 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
2222
Ian Rogers28ad40d2011-10-27 15:19:26 -07002223 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002225 /*
2226 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2227 * could be a "upcast" -- not expected, so we don't try to address it.)
2228 *
2229 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002230 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002231 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002232 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002233 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002234 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002235 if (res_type.IsUnknown()) {
2236 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2237 break; // couldn't resolve class
2238 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002239 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2240 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002241 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002242 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002244 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07002246 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002247 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002248 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002250 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002251 }
jeffhaobdb76512011-09-07 11:43:16 -07002252 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002253 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 }
2255 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002256 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002257 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002258 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002261 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 }
2263 }
2264 break;
2265 }
2266 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002267 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
jeffhao8cd6dda2012-02-22 10:15:34 -08002268 if (res_type.IsUnknown()) {
2269 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2270 break; // couldn't resolve class
2271 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002272 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2273 // can't create an instance of an interface or abstract class */
2274 if (!res_type.IsInstantiableTypes()) {
2275 Fail(VERIFY_ERROR_INSTANTIATION)
2276 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2279 // Any registers holding previous allocations from this address that have not yet been
2280 // initialized must be marked invalid.
2281 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2282 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08002283 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
2285 break;
2286 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002287 case Instruction::NEW_ARRAY:
2288 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002291 VerifyNewArray(dec_insn, true, false);
2292 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002294 case Instruction::FILLED_NEW_ARRAY_RANGE:
2295 VerifyNewArray(dec_insn, true, true);
2296 just_set_result = true; // Filled new array range sets result register
2297 break;
jeffhaobdb76512011-09-07 11:43:16 -07002298 case Instruction::CMPL_FLOAT:
2299 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002300 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002301 break;
2302 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002303 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002304 break;
2305 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002306 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::CMPL_DOUBLE:
2309 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002311 break;
2312 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002313 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002314 break;
2315 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002316 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002319 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002320 break;
2321 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002322 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002323 break;
2324 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002325 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002328 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002329 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002330 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002331 }
2332 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 }
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::GOTO:
2335 case Instruction::GOTO_16:
2336 case Instruction::GOTO_32:
2337 /* no effect on or use of registers */
2338 break;
2339
2340 case Instruction::PACKED_SWITCH:
2341 case Instruction::SPARSE_SWITCH:
2342 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 case Instruction::FILL_ARRAY_DATA: {
2347 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002348 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08002349 /* array_type can be null if the reg type is Zero */
2350 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002351 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002353 } else {
jeffhao457cc512012-02-02 16:55:13 -08002354 const RegType& component_type = reg_types_.GetComponentType(array_type,
2355 method_->GetDeclaringClass()->GetClassLoader());
2356 DCHECK(!component_type.IsUnknown());
2357 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2359 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 } else {
jeffhao457cc512012-02-02 16:55:13 -08002361 // Now verify if the element width in the table matches the element width declared in
2362 // the array
2363 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2364 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002366 } else {
2367 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2368 // Since we don't compress the data in Dex, expect to see equal width of data stored
2369 // in the table and expected from the array class.
2370 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2372 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 }
2375 }
jeffhaobdb76512011-09-07 11:43:16 -07002376 }
2377 }
2378 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 }
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002382 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2383 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 bool mismatch = false;
2385 if (reg_type1.IsZero()) { // zero then integral or reference expected
2386 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2387 } else if (reg_type1.IsReferenceTypes()) { // both references?
2388 mismatch = !reg_type2.IsReferenceTypes();
2389 } else { // both integral?
2390 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2391 }
2392 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07002393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2394 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002395 }
2396 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 }
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::IF_LT:
2399 case Instruction::IF_GE:
2400 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002402 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2403 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2406 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002407 }
2408 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 }
jeffhaobdb76512011-09-07 11:43:16 -07002410 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 }
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 }
jeffhaobdb76512011-09-07 11:43:16 -07002418 case Instruction::IF_LTZ:
2419 case Instruction::IF_GEZ:
2420 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002422 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2425 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 }
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 }
jeffhaobdb76512011-09-07 11:43:16 -07002429 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2431 break;
jeffhaobdb76512011-09-07 11:43:16 -07002432 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2434 break;
jeffhaobdb76512011-09-07 11:43:16 -07002435 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002436 VerifyAGet(dec_insn, reg_types_.Char(), true);
2437 break;
jeffhaobdb76512011-09-07 11:43:16 -07002438 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 case Instruction::AGET:
2442 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2443 break;
jeffhaobdb76512011-09-07 11:43:16 -07002444 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 VerifyAGet(dec_insn, reg_types_.Long(), true);
2446 break;
2447 case Instruction::AGET_OBJECT:
2448 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 case Instruction::APUT_BOOLEAN:
2452 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2453 break;
2454 case Instruction::APUT_BYTE:
2455 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2456 break;
2457 case Instruction::APUT_CHAR:
2458 VerifyAPut(dec_insn, reg_types_.Char(), true);
2459 break;
2460 case Instruction::APUT_SHORT:
2461 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472
jeffhaobdb76512011-09-07 11:43:16 -07002473 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002474 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 break;
jeffhaobdb76512011-09-07 11:43:16 -07002476 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002477 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 break;
jeffhaobdb76512011-09-07 11:43:16 -07002479 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002480 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 break;
jeffhaobdb76512011-09-07 11:43:16 -07002482 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002483 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 break;
2485 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002486 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002489 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002492 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 break;
jeffhaobdb76512011-09-07 11:43:16 -07002494
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002496 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 break;
2498 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002499 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 break;
2501 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002502 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 break;
2504 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002505 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002508 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002511 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 break;
jeffhaobdb76512011-09-07 11:43:16 -07002513 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002514 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516
jeffhaobdb76512011-09-07 11:43:16 -07002517 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002518 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 break;
jeffhaobdb76512011-09-07 11:43:16 -07002520 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002521 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 break;
jeffhaobdb76512011-09-07 11:43:16 -07002523 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002524 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 break;
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002527 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 break;
2529 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002530 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002533 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
2535 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002536 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 break;
2538
2539 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002540 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 break;
2542 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002543 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 break;
2545 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002546 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 break;
2548 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002549 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002552 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002555 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002558 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560
2561 case Instruction::INVOKE_VIRTUAL:
2562 case Instruction::INVOKE_VIRTUAL_RANGE:
2563 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002565 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
2566 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2567 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
2568 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2570 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002571 const char* descriptor;
2572 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002573 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002574 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2575 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002576 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002577 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002578 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 }
Ian Rogers9074b992011-10-26 17:41:55 -07002580 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002581 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002583 just_set_result = true;
2584 }
2585 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 }
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002589 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2591 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002592 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 * Some additional checks when calling a constructor. We know from the invocation arg check
2594 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2595 * that to require that called_method->klass is the same as this->klass or this->super,
2596 * allowing the latter only if the "this" argument is the same as the "this" argument to
2597 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002598 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002599 bool is_constructor;
2600 if (called_method != NULL) {
2601 is_constructor = called_method->IsConstructor();
2602 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002603 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002604 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2605 const char* name = dex_file_->GetMethodName(method_id);
2606 is_constructor = strcmp(name, "<init>") == 0;
2607 }
2608 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2610 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612
2613 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07002615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002618 if (called_method != NULL) {
2619 Class* this_class = this_type.GetClass();
2620 DCHECK(this_class != NULL);
2621 /* must be in same class or in superclass */
2622 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2623 if (this_class != method_->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07002624 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers28ad40d2011-10-27 15:19:26 -07002625 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2626 break;
2627 }
2628 } else if (called_method->GetDeclaringClass() != this_class) {
jeffhaod5347e02012-03-22 17:25:05 -07002629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 }
jeffhaobdb76512011-09-07 11:43:16 -07002632 }
2633
2634 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002635 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639 }
2640
2641 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002642 * Replace the uninitialized reference with an initialized one. We need to do this for all
2643 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002644 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 work_line_->MarkRefsAsInitialized(this_type);
2646 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002648 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002649 const char* descriptor;
2650 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002651 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002652 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2653 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002654 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002655 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002656 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002657 }
Ian Rogers9074b992011-10-26 17:41:55 -07002658 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002659 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002661 just_set_result = true;
2662 }
2663 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 }
jeffhaobdb76512011-09-07 11:43:16 -07002665 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002667 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2669 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002670 const char* descriptor;
2671 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002673 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2674 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002675 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002676 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002677 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002678 }
Ian Rogers9074b992011-10-26 17:41:55 -07002679 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002680 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 work_line_->SetResultRegisterType(return_type);
2682 just_set_result = true;
2683 }
jeffhaobdb76512011-09-07 11:43:16 -07002684 }
2685 break;
2686 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2690 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002691 if (abs_method != NULL) {
2692 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002693 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002694 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2695 << PrettyMethod(abs_method) << "'";
2696 break;
2697 }
2698 }
2699 /* Get the type of the "this" arg, which should either be a sub-interface of called
2700 * interface or Object (see comments in RegType::JoinClass).
2701 */
2702 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2703 if (failure_ == VERIFY_ERROR_NONE) {
2704 if (this_type.IsZero()) {
2705 /* null pointer always passes (and always fails at runtime) */
2706 } else {
2707 if (this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
Ian Rogers28ad40d2011-10-27 15:19:26 -07002709 << this_type;
2710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002712 // In the past we have tried to assert that "called_interface" is assignable
2713 // from "this_type.GetClass()", however, as we do an imprecise Join
2714 // (RegType::JoinClass) we don't have full information on what interfaces are
2715 // implemented by "this_type". For example, two classes may implement the same
2716 // interfaces and have a common parent that doesn't implement the interface. The
2717 // join will set "this_type" to the parent class and a test that this implements
2718 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002719 }
2720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 * We don't have an object instance, so we can't find the concrete method. However, all of
2723 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002724 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002725 const char* descriptor;
2726 if (abs_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002727 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002728 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2729 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002730 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002731 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002732 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002733 }
Ian Rogers9074b992011-10-26 17:41:55 -07002734 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002735 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2736 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002738 just_set_result = true;
2739 }
2740 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 }
jeffhaobdb76512011-09-07 11:43:16 -07002742 case Instruction::NEG_INT:
2743 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::NEG_LONG:
2747 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002761 break;
2762 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002764 break;
2765 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002773 break;
2774 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002779 break;
2780 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
2789 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801
2802 case Instruction::ADD_INT:
2803 case Instruction::SUB_INT:
2804 case Instruction::MUL_INT:
2805 case Instruction::REM_INT:
2806 case Instruction::DIV_INT:
2807 case Instruction::SHL_INT:
2808 case Instruction::SHR_INT:
2809 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812 case Instruction::AND_INT:
2813 case Instruction::OR_INT:
2814 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::ADD_LONG:
2818 case Instruction::SUB_LONG:
2819 case Instruction::MUL_LONG:
2820 case Instruction::DIV_LONG:
2821 case Instruction::REM_LONG:
2822 case Instruction::AND_LONG:
2823 case Instruction::OR_LONG:
2824 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::SHL_LONG:
2828 case Instruction::SHR_LONG:
2829 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 /* shift distance is Int, making these different from other binary operations */
2831 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::ADD_FLOAT:
2834 case Instruction::SUB_FLOAT:
2835 case Instruction::MUL_FLOAT:
2836 case Instruction::DIV_FLOAT:
2837 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002839 break;
2840 case Instruction::ADD_DOUBLE:
2841 case Instruction::SUB_DOUBLE:
2842 case Instruction::MUL_DOUBLE:
2843 case Instruction::DIV_DOUBLE:
2844 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002846 break;
2847 case Instruction::ADD_INT_2ADDR:
2848 case Instruction::SUB_INT_2ADDR:
2849 case Instruction::MUL_INT_2ADDR:
2850 case Instruction::REM_INT_2ADDR:
2851 case Instruction::SHL_INT_2ADDR:
2852 case Instruction::SHR_INT_2ADDR:
2853 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002855 break;
2856 case Instruction::AND_INT_2ADDR:
2857 case Instruction::OR_INT_2ADDR:
2858 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002860 break;
2861 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002863 break;
2864 case Instruction::ADD_LONG_2ADDR:
2865 case Instruction::SUB_LONG_2ADDR:
2866 case Instruction::MUL_LONG_2ADDR:
2867 case Instruction::DIV_LONG_2ADDR:
2868 case Instruction::REM_LONG_2ADDR:
2869 case Instruction::AND_LONG_2ADDR:
2870 case Instruction::OR_LONG_2ADDR:
2871 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874 case Instruction::SHL_LONG_2ADDR:
2875 case Instruction::SHR_LONG_2ADDR:
2876 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::ADD_FLOAT_2ADDR:
2880 case Instruction::SUB_FLOAT_2ADDR:
2881 case Instruction::MUL_FLOAT_2ADDR:
2882 case Instruction::DIV_FLOAT_2ADDR:
2883 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002885 break;
2886 case Instruction::ADD_DOUBLE_2ADDR:
2887 case Instruction::SUB_DOUBLE_2ADDR:
2888 case Instruction::MUL_DOUBLE_2ADDR:
2889 case Instruction::DIV_DOUBLE_2ADDR:
2890 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002892 break;
2893 case Instruction::ADD_INT_LIT16:
2894 case Instruction::RSUB_INT:
2895 case Instruction::MUL_INT_LIT16:
2896 case Instruction::DIV_INT_LIT16:
2897 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002899 break;
2900 case Instruction::AND_INT_LIT16:
2901 case Instruction::OR_INT_LIT16:
2902 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
2905 case Instruction::ADD_INT_LIT8:
2906 case Instruction::RSUB_INT_LIT8:
2907 case Instruction::MUL_INT_LIT8:
2908 case Instruction::DIV_INT_LIT8:
2909 case Instruction::REM_INT_LIT8:
2910 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002911 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002912 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::AND_INT_LIT8:
2916 case Instruction::OR_INT_LIT8:
2917 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002919 break;
2920
2921 /*
2922 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002923 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002924 * inserted in the course of verification, we can expect to see it here.
2925 */
jeffhaob4df5142011-09-19 20:25:32 -07002926 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002930 case Instruction::UNUSED_EE:
2931 case Instruction::UNUSED_EF:
2932 case Instruction::UNUSED_F2:
2933 case Instruction::UNUSED_F3:
2934 case Instruction::UNUSED_F4:
2935 case Instruction::UNUSED_F5:
2936 case Instruction::UNUSED_F6:
2937 case Instruction::UNUSED_F7:
2938 case Instruction::UNUSED_F8:
2939 case Instruction::UNUSED_F9:
2940 case Instruction::UNUSED_FA:
2941 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002942 case Instruction::UNUSED_F0:
2943 case Instruction::UNUSED_F1:
2944 case Instruction::UNUSED_E3:
2945 case Instruction::UNUSED_E8:
2946 case Instruction::UNUSED_E7:
2947 case Instruction::UNUSED_E4:
2948 case Instruction::UNUSED_E9:
2949 case Instruction::UNUSED_FC:
2950 case Instruction::UNUSED_E5:
2951 case Instruction::UNUSED_EA:
2952 case Instruction::UNUSED_FD:
2953 case Instruction::UNUSED_E6:
2954 case Instruction::UNUSED_EB:
2955 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002956 case Instruction::UNUSED_3E:
2957 case Instruction::UNUSED_3F:
2958 case Instruction::UNUSED_40:
2959 case Instruction::UNUSED_41:
2960 case Instruction::UNUSED_42:
2961 case Instruction::UNUSED_43:
2962 case Instruction::UNUSED_73:
2963 case Instruction::UNUSED_79:
2964 case Instruction::UNUSED_7A:
2965 case Instruction::UNUSED_EC:
2966 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002968 break;
2969
2970 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002971 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002972 * complain if an instruction is missing (which is desirable).
2973 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002974 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002975
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 if (failure_ != VERIFY_ERROR_NONE) {
jeffhaod5347e02012-03-22 17:25:05 -07002977 if (failure_ == VERIFY_ERROR_BAD_CLASS_HARD || failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) {
jeffhaobdb76512011-09-07 11:43:16 -07002978 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002979 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002980 return false;
2981 } else {
2982 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002983 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002985 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002987 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 failure_ = VERIFY_ERROR_NONE;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002989 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002990 }
2991 }
jeffhaobdb76512011-09-07 11:43:16 -07002992 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 * If we didn't just set the result register, clear it out. This ensures that you can only use
2994 * "move-result" immediately after the result is set. (We could check this statically, but it's
2995 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002996 */
2997 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002999 }
3000
jeffhaoa0a764a2011-09-16 10:43:38 -07003001 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003002 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
3004 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07003005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07003006 return false;
3007 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3009 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07003010 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07003011 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 }
3013 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
3014 if (next_line != NULL) {
3015 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3016 // needed.
3017 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003018 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
jeffhaobdb76512011-09-07 11:43:16 -07003020 } else {
3021 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 * We're not recording register data for the next instruction, so we don't know what the prior
3023 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07003024 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003026 }
3027 }
3028
3029 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003030 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003031 *
3032 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003033 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003034 * somebody could get a reference field, check it for zero, and if the
3035 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003036 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003037 * that, and will reject the code.
3038 *
3039 * TODO: avoid re-fetching the branch target
3040 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003042 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003044 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003046 return false;
3047 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003048 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07003049 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003050 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 }
jeffhaobdb76512011-09-07 11:43:16 -07003052 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003054 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 }
jeffhaobdb76512011-09-07 11:43:16 -07003056 }
3057
3058 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003059 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003060 *
3061 * We've already verified that the table is structurally sound, so we
3062 * just need to walk through and tag the targets.
3063 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003064 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003065 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3066 const uint16_t* switch_insns = insns + offset_to_switch;
3067 int switch_count = switch_insns[1];
3068 int offset_to_targets, targ;
3069
3070 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3071 /* 0 = sig, 1 = count, 2/3 = first key */
3072 offset_to_targets = 4;
3073 } else {
3074 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003075 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003076 offset_to_targets = 2 + 2 * switch_count;
3077 }
3078
3079 /* verify each switch target */
3080 for (targ = 0; targ < switch_count; targ++) {
3081 int offset;
3082 uint32_t abs_offset;
3083
3084 /* offsets are 32-bit, and only partly endian-swapped */
3085 offset = switch_insns[offset_to_targets + targ * 2] |
3086 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 abs_offset = work_insn_idx_ + offset;
3088 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07003089 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003090 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 }
3092 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07003093 return false;
3094 }
3095 }
3096
3097 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3099 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003100 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003103 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003104
Ian Rogers0571d352011-11-03 19:51:38 -07003105 for (; iterator.HasNext(); iterator.Next()) {
3106 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 within_catch_all = true;
3108 }
jeffhaobdb76512011-09-07 11:43:16 -07003109 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3111 * "work_regs", because at runtime the exception will be thrown before the instruction
3112 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003113 */
Ian Rogers0571d352011-11-03 19:51:38 -07003114 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003115 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 }
jeffhaobdb76512011-09-07 11:43:16 -07003117 }
3118
3119 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003120 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3121 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003122 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003124 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 * The state in work_line reflects the post-execution state. If the current instruction is a
3126 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003127 * it will do so before grabbing the lock).
3128 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003130 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003132 return false;
3133 }
3134 }
3135 }
3136
jeffhaod1f0fde2011-09-08 17:25:33 -07003137 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003138 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003139 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 return false;
3141 }
jeffhaobdb76512011-09-07 11:43:16 -07003142 }
3143
3144 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003145 * Update start_guess. Advance to the next instruction of that's
3146 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003147 * neither of those exists we're in a return or throw; leave start_guess
3148 * alone and let the caller sort it out.
3149 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003150 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003152 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003153 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003155 }
3156
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3158 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003159
3160 return true;
3161}
3162
Ian Rogers28ad40d2011-10-27 15:19:26 -07003163const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003164 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003165 Class* referrer = method_->GetDeclaringClass();
3166 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3167 const RegType& result =
3168 klass != NULL ? reg_types_.FromClass(klass)
3169 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3170 if (klass == NULL && !result.IsUnresolvedTypes()) {
3171 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 }
jeffhaod1224c72012-02-29 13:43:08 -08003173 if (result.IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07003174 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing unknown class in " << PrettyDescriptor(referrer);
jeffhaod1224c72012-02-29 13:43:08 -08003175 return result;
3176 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003177 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3178 // check at runtime if access is allowed and so pass here.
3179 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3180 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003181 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003182 << result << "'";
3183 return reg_types_.Unknown();
3184 } else {
3185 return result;
3186 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003187}
3188
Ian Rogers28ad40d2011-10-27 15:19:26 -07003189const RegType& DexVerifier::GetCaughtExceptionType() {
3190 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003192 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3194 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003195 CatchHandlerIterator iterator(handlers_ptr);
3196 for (; iterator.HasNext(); iterator.Next()) {
3197 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3198 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003199 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003201 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003202 if (common_super == NULL) {
3203 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3204 // as that is caught at runtime
3205 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003206 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003207 // We don't know enough about the type and the common path merge will result in
3208 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07003209 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003210 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003211 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003212 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003214 common_super = &common_super->Merge(exception, &reg_types_);
3215 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 }
3217 }
3218 }
3219 }
Ian Rogers0571d352011-11-03 19:51:38 -07003220 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 }
3222 }
3223 if (common_super == NULL) {
3224 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003225 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003226 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003228 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003229}
3230
jeffhao8cd6dda2012-02-22 10:15:34 -08003231Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003232 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3233 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3234 if (failure_ != VERIFY_ERROR_NONE) {
3235 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3236 return NULL;
3237 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003238 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003239 return NULL; // Can't resolve Class so no more to do here
3240 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003241 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003242 Class* referrer = method_->GetDeclaringClass();
3243 DexCache* dex_cache = referrer->GetDexCache();
3244 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3245 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003246 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003247 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003248
3249 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003251 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 res_method = klass->FindInterfaceMethod(name, signature);
3253 } else {
3254 res_method = klass->FindVirtualMethod(name, signature);
3255 }
3256 if (res_method != NULL) {
3257 dex_cache->SetResolvedMethod(method_idx, res_method);
3258 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003259 // If a virtual or interface method wasn't found with the expected type, look in
3260 // the direct methods. This can happen when the wrong invoke type is used or when
3261 // a class has changed, and will be flagged as an error in later checks.
3262 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3263 res_method = klass->FindDirectMethod(name, signature);
3264 }
3265 if (res_method == NULL) {
3266 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3267 << PrettyDescriptor(klass) << "." << name
3268 << " " << signature;
3269 return NULL;
3270 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 }
3272 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003273 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3274 // enforce them here.
3275 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3277 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 return NULL;
3279 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003280 // Disallow any calls to class initializers.
3281 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3283 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003284 return NULL;
3285 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003286 // Check if access is allowed.
3287 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3288 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3289 << " from " << PrettyDescriptor(referrer) << ")";
3290 return NULL;
3291 }
jeffhaode0d9c92012-02-27 13:58:13 -08003292 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3293 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3295 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003296 return NULL;
3297 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003298 // Check that interface methods match interface classes.
3299 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3300 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3301 << " is in an interface class " << PrettyClass(klass);
3302 return NULL;
3303 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3304 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3305 << " is in a non-interface class " << PrettyClass(klass);
3306 return NULL;
3307 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 // See if the method type implied by the invoke instruction matches the access flags for the
3309 // target method.
3310 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3311 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3312 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3313 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003314 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3315 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 return NULL;
3317 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003318 return res_method;
3319}
3320
Elliott Hughesadb8c672012-03-06 16:49:32 -08003321Method* DexVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08003322 MethodType method_type, bool is_range, bool is_super) {
3323 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3324 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003325 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003326 if (res_method == NULL) { // error or class is unresolved
3327 return NULL;
3328 }
3329
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3331 // has a vtable entry for the target method.
3332 if (is_super) {
3333 DCHECK(method_type == METHOD_VIRTUAL);
3334 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003335 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 if (super == NULL) { // Only Object has no super class
3337 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3338 << " to super " << PrettyMethod(res_method);
3339 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003340 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003342 << " to super " << PrettyDescriptor(super)
3343 << "." << mh.GetName()
3344 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 }
3346 return NULL;
3347 }
3348 }
3349 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3350 // match the call to the signature. Also, we might might be calling through an abstract method
3351 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08003352 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 /* caught by static verifier */
3354 DCHECK(is_range || expected_args <= 5);
3355 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003357 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3358 return NULL;
3359 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003360
jeffhaobdb76512011-09-07 11:43:16 -07003361 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 * Check the "this" argument, which must be an instance of the class
3363 * that declared the method. For an interface class, we don't do the
3364 * full interface merge, so we can't do a rigorous check here (which
3365 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003366 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003367 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 if (!res_method->IsStatic()) {
3369 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3370 if (failure_ != VERIFY_ERROR_NONE) {
3371 return NULL;
3372 }
3373 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 return NULL;
3376 }
3377 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003378 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3379 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003380 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003381 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 return NULL;
3383 }
3384 }
3385 actual_args++;
3386 }
3387 /*
3388 * Process the target method's signature. This signature may or may not
3389 * have been verified, so we can't assume it's properly formed.
3390 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003391 MethodHelper mh(res_method);
3392 const DexFile::TypeList* params = mh.GetParameterTypeList();
3393 size_t params_size = params == NULL ? 0 : params->Size();
3394 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003397 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3398 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 return NULL;
3400 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003401 const char* descriptor =
3402 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3403 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003405 << " missing signature component";
3406 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 }
3408 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003409 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003410 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003411 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3412 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003413 }
3414 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003418 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 return NULL;
3420 } else {
3421 return res_method;
3422 }
3423}
3424
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003425const RegType& DexVerifier::GetMethodReturnType() {
3426 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3427 MethodHelper(method_).GetReturnTypeDescriptor());
3428}
3429
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430void DexVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08003431 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003432 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003433 if (res_type.IsUnknown()) {
3434 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3435 } else {
3436 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3437 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003439 } else if (!is_filled) {
3440 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003441 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003442 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003444 } else {
3445 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3446 // the list and fail. It's legal, if silly, for arg_count to be zero.
3447 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3448 method_->GetDeclaringClass()->GetClassLoader());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003449 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003450 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003451 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003452 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3453 work_line_->SetResultRegisterType(reg_types_.Unknown());
3454 return;
3455 }
3456 }
3457 // filled-array result goes into "result" register
3458 work_line_->SetResultRegisterType(res_type);
3459 }
3460 }
3461}
3462
Elliott Hughesadb8c672012-03-06 16:49:32 -08003463void DexVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003465 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003469 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003470 if (array_type.IsZero()) {
3471 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3472 // instruction type. TODO: have a proper notion of bottom here.
3473 if (!is_primitive || insn_type.IsCategory1Types()) {
3474 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08003475 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003476 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003477 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08003478 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08003479 }
jeffhaofc3144e2012-02-01 17:21:15 -08003480 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003482 } else {
3483 /* verify the class */
3484 const RegType& component_type = reg_types_.GetComponentType(array_type,
3485 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003486 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003488 << " source for aget-object";
3489 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003491 << " source for category 1 aget";
3492 } else if (is_primitive && !insn_type.Equals(component_type) &&
3493 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3494 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003497 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 // Use knowledge of the field type which is stronger than the type inferred from the
3499 // instruction, which can't differentiate object types and ints from floats, longs from
3500 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003501 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003502 }
3503 }
3504 }
3505}
3506
Elliott Hughesadb8c672012-03-06 16:49:32 -08003507void DexVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003508 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003509 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003510 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003513 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003514 if (array_type.IsZero()) {
3515 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3516 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003517 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003519 } else {
3520 /* verify the class */
3521 const RegType& component_type = reg_types_.GetComponentType(array_type,
3522 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003523 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003525 << " source for aput-object";
3526 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003528 << " source for category 1 aput";
3529 } else if (is_primitive && !insn_type.Equals(component_type) &&
3530 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3531 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003533 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003534 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003535 // The instruction agrees with the type of array, confirm the value to be stored does too
3536 // Note: we use the instruction type (rather than the component type) for aput-object as
3537 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08003538 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003539 }
3540 }
3541 }
3542}
3543
3544Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003545 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3546 // Check access to class
3547 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3548 if (failure_ != VERIFY_ERROR_NONE) {
3549 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3550 << dex_file_->GetFieldName(field_id) << ") in "
3551 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3552 return NULL;
3553 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003554 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003555 return NULL; // Can't resolve Class so no more to do here
3556 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003557 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003559 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3560 << dex_file_->GetFieldName(field_id) << ") in "
3561 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 DCHECK(Thread::Current()->IsExceptionPending());
3563 Thread::Current()->ClearException();
3564 return NULL;
3565 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3566 field->GetAccessFlags())) {
3567 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3568 << " from " << PrettyClass(method_->GetDeclaringClass());
3569 return NULL;
3570 } else if (!field->IsStatic()) {
3571 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3572 return NULL;
3573 } else {
3574 return field;
3575 }
3576}
3577
Ian Rogersd81871c2011-10-03 13:57:23 -07003578Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003579 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3580 // Check access to class
3581 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3582 if (failure_ != VERIFY_ERROR_NONE) {
3583 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3584 << dex_file_->GetFieldName(field_id) << ") in "
3585 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3586 return NULL;
3587 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003588 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003589 return NULL; // Can't resolve Class so no more to do here
3590 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003591 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003592 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003593 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3594 << dex_file_->GetFieldName(field_id) << ") in "
3595 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 DCHECK(Thread::Current()->IsExceptionPending());
3597 Thread::Current()->ClearException();
3598 return NULL;
3599 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3600 field->GetAccessFlags())) {
3601 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3602 << " from " << PrettyClass(method_->GetDeclaringClass());
3603 return NULL;
3604 } else if (field->IsStatic()) {
3605 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3606 << " to not be static";
3607 return NULL;
3608 } else if (obj_type.IsZero()) {
3609 // Cannot infer and check type, however, access will cause null pointer exception
3610 return field;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003611 } else if (obj_type.IsUninitializedTypes() &&
3612 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3613 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 // Field accesses through uninitialized references are only allowable for constructors where
3615 // the field is declared in this class
jeffhaod5347e02012-03-22 17:25:05 -07003616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3617 << " of a not fully initialized object within the context of "
3618 << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 return NULL;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003620 } else if (!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3622 // of C1. For resolution to occur the declared class of the field must be compatible with
3623 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3624 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3625 << " from object of type " << PrettyClass(obj_type.GetClass());
3626 return NULL;
3627 } else {
3628 return field;
3629 }
3630}
3631
Elliott Hughesadb8c672012-03-06 16:49:32 -08003632void DexVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003633 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003634 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003635 Field* field;
3636 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003637 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003638 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003639 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003640 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003641 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003642 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003643 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003644 } else {
3645 const char* descriptor;
3646 const ClassLoader* loader;
3647 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003648 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003649 loader = field->GetDeclaringClass()->GetClassLoader();
3650 } else {
3651 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3652 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3653 loader = method_->GetDeclaringClass()->GetClassLoader();
3654 }
3655 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003657 if (field_type.Equals(insn_type) ||
3658 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3659 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003660 // expected that read is of the correct primitive type or that int reads are reading
3661 // floats or long reads are reading doubles
3662 } else {
3663 // This is a global failure rather than a class change failure as the instructions and
3664 // the descriptors for the type should have been consistent within the same file at
3665 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3667 << " to be of type '" << insn_type
3668 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003669 return;
3670 }
3671 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003672 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003673 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3674 << " to be compatible with type '" << insn_type
3675 << "' but found type '" << field_type
3676 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 return;
3678 }
3679 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003680 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 }
3682}
3683
Elliott Hughesadb8c672012-03-06 16:49:32 -08003684void DexVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003685 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003686 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003687 Field* field;
3688 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003689 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003690 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003691 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003692 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003693 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003694 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003695 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogers55d249f2011-11-02 16:48:09 -07003696 } else {
3697 const char* descriptor;
3698 const ClassLoader* loader;
3699 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003700 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003701 loader = field->GetDeclaringClass()->GetClassLoader();
3702 } else {
3703 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3704 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3705 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003706 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003707 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3708 if (field != NULL) {
3709 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3710 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3711 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3712 return;
3713 }
3714 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003716 // Primitive field assignability rules are weaker than regular assignability rules
3717 bool instruction_compatible;
3718 bool value_compatible;
Elliott Hughesadb8c672012-03-06 16:49:32 -08003719 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers2c8a8572011-10-24 17:11:36 -07003720 if (field_type.IsIntegralTypes()) {
3721 instruction_compatible = insn_type.IsIntegralTypes();
3722 value_compatible = value_type.IsIntegralTypes();
3723 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003724 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003725 value_compatible = value_type.IsFloatTypes();
3726 } else if (field_type.IsLong()) {
3727 instruction_compatible = insn_type.IsLong();
3728 value_compatible = value_type.IsLongTypes();
3729 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003730 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003731 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003733 instruction_compatible = false; // reference field with primitive store
3734 value_compatible = false; // unused
3735 }
3736 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 // This is a global failure rather than a class change failure as the instructions and
3738 // the descriptors for the type should have been consistent within the same file at
3739 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3741 << " to be of type '" << insn_type
3742 << "' but found type '" << field_type
3743 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003744 return;
3745 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003746 if (!value_compatible) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3748 << " of type " << value_type
3749 << " but expected " << field_type
3750 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003751 return;
3752 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003753 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003754 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003755 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3756 << " to be compatible with type '" << insn_type
3757 << "' but found type '" << field_type
3758 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 return;
3760 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003761 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003763 }
3764}
3765
jeffhaod5347e02012-03-22 17:25:05 -07003766bool DexVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 return false;
3770 }
3771 return true;
3772}
3773
Ian Rogersd81871c2011-10-03 13:57:23 -07003774void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003775 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003776 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003777 << " " << fail_messages_.str();
3778 return;
3779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3781 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3782 VerifyErrorRefType ref_type;
3783 switch (inst->Opcode()) {
3784 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003785 case Instruction::CHECK_CAST:
3786 case Instruction::INSTANCE_OF:
3787 case Instruction::NEW_INSTANCE:
3788 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003790 case Instruction::FILLED_NEW_ARRAY_RANGE:
3791 ref_type = VERIFY_ERROR_REF_CLASS;
3792 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003794 case Instruction::IGET_BOOLEAN:
3795 case Instruction::IGET_BYTE:
3796 case Instruction::IGET_CHAR:
3797 case Instruction::IGET_SHORT:
3798 case Instruction::IGET_WIDE:
3799 case Instruction::IGET_OBJECT:
3800 case Instruction::IPUT:
3801 case Instruction::IPUT_BOOLEAN:
3802 case Instruction::IPUT_BYTE:
3803 case Instruction::IPUT_CHAR:
3804 case Instruction::IPUT_SHORT:
3805 case Instruction::IPUT_WIDE:
3806 case Instruction::IPUT_OBJECT:
3807 case Instruction::SGET:
3808 case Instruction::SGET_BOOLEAN:
3809 case Instruction::SGET_BYTE:
3810 case Instruction::SGET_CHAR:
3811 case Instruction::SGET_SHORT:
3812 case Instruction::SGET_WIDE:
3813 case Instruction::SGET_OBJECT:
3814 case Instruction::SPUT:
3815 case Instruction::SPUT_BOOLEAN:
3816 case Instruction::SPUT_BYTE:
3817 case Instruction::SPUT_CHAR:
3818 case Instruction::SPUT_SHORT:
3819 case Instruction::SPUT_WIDE:
3820 case Instruction::SPUT_OBJECT:
3821 ref_type = VERIFY_ERROR_REF_FIELD;
3822 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003824 case Instruction::INVOKE_VIRTUAL_RANGE:
3825 case Instruction::INVOKE_SUPER:
3826 case Instruction::INVOKE_SUPER_RANGE:
3827 case Instruction::INVOKE_DIRECT:
3828 case Instruction::INVOKE_DIRECT_RANGE:
3829 case Instruction::INVOKE_STATIC:
3830 case Instruction::INVOKE_STATIC_RANGE:
3831 case Instruction::INVOKE_INTERFACE:
3832 case Instruction::INVOKE_INTERFACE_RANGE:
3833 ref_type = VERIFY_ERROR_REF_METHOD;
3834 break;
jeffhaobdb76512011-09-07 11:43:16 -07003835 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003836 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003837 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003838 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003839 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3840 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3841 // instruction, so assert it.
3842 size_t width = inst->SizeInCodeUnits();
3843 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003844 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 // NOPs
3846 for (size_t i = 2; i < width; i++) {
3847 insns[work_insn_idx_ + i] = Instruction::NOP;
3848 }
3849 // Encode the opcode, with the failure code in the high byte
3850 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3851 (failure_ << 8) | // AA - component
3852 (ref_type << (8 + kVerifyErrorRefTypeShift));
3853 insns[work_insn_idx_] = new_instruction;
3854 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3855 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003856 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3857 << fail_messages_.str();
3858 if (gDebugVerify) {
3859 std::cout << std::endl << info_messages_.str();
3860 Dump(std::cout);
3861 }
jeffhaobdb76512011-09-07 11:43:16 -07003862}
jeffhaoba5ebb92011-08-25 17:24:37 -07003863
Ian Rogersd81871c2011-10-03 13:57:23 -07003864bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 bool changed = true;
3866 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3867 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003868 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 * We haven't processed this instruction before, and we haven't touched the registers here, so
3870 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3871 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003872 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003874 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003875 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3876 if (gDebugVerify) {
3877 copy->CopyFromLine(target_line);
3878 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 changed = target_line->MergeRegisters(merge_line);
3880 if (failure_ != VERIFY_ERROR_NONE) {
3881 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003882 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003883 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003884 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
3885 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 << *copy.get() << " MERGE" << std::endl
3887 << *merge_line << " ==" << std::endl
3888 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003889 }
3890 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 if (changed) {
3892 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003893 }
3894 return true;
3895}
3896
Ian Rogersd81871c2011-10-03 13:57:23 -07003897void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3898 size_t* log2_max_gc_pc) {
3899 size_t local_gc_points = 0;
3900 size_t max_insn = 0;
3901 size_t max_ref_reg = -1;
3902 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3903 if (insn_flags_[i].IsGcPoint()) {
3904 local_gc_points++;
3905 max_insn = i;
3906 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003907 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003908 }
3909 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003910 *gc_points = local_gc_points;
3911 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3912 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003913 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003914 i++;
3915 }
3916 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003917}
3918
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003919const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003920 size_t num_entries, ref_bitmap_bits, pc_bits;
3921 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3922 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003923 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003924 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003926 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003927 return NULL;
3928 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3930 // There are 2 bytes to encode the number of entries
3931 if (num_entries >= 65536) {
3932 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003935 return NULL;
3936 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003937 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003938 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003939 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003940 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003941 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003942 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003943 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003944 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003945 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003946 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003948 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3949 return NULL;
3950 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003951 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003952 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003953 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003955 return NULL;
3956 }
3957 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003958 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3959 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003960 table->push_back(num_entries & 0xFF);
3961 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003962 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003963 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3964 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003965 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003966 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003967 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003968 }
3969 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003970 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 }
3972 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003973 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 return table;
3975}
jeffhaoa0a764a2011-09-16 10:43:38 -07003976
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003977void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003978 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3979 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003980 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003981 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003982 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003983 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3984 if (insn_flags_[i].IsGcPoint()) {
3985 CHECK_LT(map_index, map.NumEntries());
3986 CHECK_EQ(map.GetPC(map_index), i);
3987 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3988 map_index++;
3989 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003990 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003991 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003992 CHECK_LT(j / 8, map.RegWidth());
3993 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3994 } else if ((j / 8) < map.RegWidth()) {
3995 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3996 } else {
3997 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3998 }
3999 }
4000 } else {
4001 CHECK(reg_bitmap == NULL);
4002 }
4003 }
4004}
jeffhaoa0a764a2011-09-16 10:43:38 -07004005
Ian Rogersd81871c2011-10-03 13:57:23 -07004006const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
4007 size_t num_entries = NumEntries();
4008 // Do linear or binary search?
4009 static const size_t kSearchThreshold = 8;
4010 if (num_entries < kSearchThreshold) {
4011 for (size_t i = 0; i < num_entries; i++) {
4012 if (GetPC(i) == dex_pc) {
4013 return GetBitMap(i);
4014 }
4015 }
4016 } else {
4017 int lo = 0;
4018 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07004019 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 int mid = (hi + lo) / 2;
4021 int mid_pc = GetPC(mid);
4022 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004023 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07004024 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004025 hi = mid - 1;
4026 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004027 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07004028 }
4029 }
4030 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 if (error_if_not_present) {
4032 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
4033 }
jeffhaoa0a764a2011-09-16 10:43:38 -07004034 return NULL;
4035}
4036
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004037Mutex* DexVerifier::gc_maps_lock_ = NULL;
4038DexVerifier::GcMapTable* DexVerifier::gc_maps_ = NULL;
4039
4040void DexVerifier::InitGcMaps() {
4041 gc_maps_lock_ = new Mutex("verifier GC maps lock");
4042 MutexLock mu(*gc_maps_lock_);
4043 gc_maps_ = new DexVerifier::GcMapTable;
4044}
4045
4046void DexVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07004047 {
4048 MutexLock mu(*gc_maps_lock_);
4049 STLDeleteValues(gc_maps_);
4050 delete gc_maps_;
4051 gc_maps_ = NULL;
4052 }
4053 delete gc_maps_lock_;
4054 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004055}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004056
4057void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004058 MutexLock mu(*gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004059 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
4060 if (existing_gc_map != NULL) {
4061 CHECK(*existing_gc_map == gc_map);
4062 delete existing_gc_map;
4063 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004064 (*gc_maps_)[ref] = &gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004065 CHECK(GetGcMap(ref) != NULL);
4066}
4067
4068const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004069 MutexLock mu(*gc_maps_lock_);
4070 GcMapTable::const_iterator it = gc_maps_->find(ref);
4071 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004072 return NULL;
4073 }
4074 CHECK(it->second != NULL);
4075 return it->second;
4076}
4077
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004078static Mutex& GetRejectedClassesLock() {
4079 static Mutex rejected_classes_lock("verifier rejected classes lock");
4080 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004081}
4082
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004083static std::set<Compiler::ClassReference>& GetRejectedClasses() {
4084 static std::set<Compiler::ClassReference> rejected_classes;
4085 return rejected_classes;
4086}
jeffhaod1224c72012-02-29 13:43:08 -08004087
4088void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004089 MutexLock mu(GetRejectedClassesLock());
4090 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08004091 CHECK(IsClassRejected(ref));
4092}
4093
4094bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004095 MutexLock mu(GetRejectedClassesLock());
4096 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
4097 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08004098}
4099
Logan Chienfca7e872011-12-20 20:08:22 +08004100#if defined(ART_USE_LLVM_COMPILER)
4101InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
4102 uint32_t insns_size = code_item_->insns_size_in_code_units_;
4103 uint16_t regs_size = code_item_->registers_size_;
4104
4105 UniquePtr<InferredRegCategoryMap> table(
4106 new InferredRegCategoryMap(insns_size, regs_size));
4107
4108 for (size_t i = 0; i < insns_size; ++i) {
4109 if (RegisterLine* line = reg_table_.GetLine(i)) {
4110 for (size_t r = 0; r < regs_size; ++r) {
4111 RegType const &rt = line->GetRegisterType(r);
4112
4113 if (rt.IsZero()) {
4114 table->SetRegCategory(i, r, kRegZero);
4115 } else if (rt.IsCategory1Types()) {
4116 table->SetRegCategory(i, r, kRegCat1nr);
4117 } else if (rt.IsCategory2Types()) {
4118 table->SetRegCategory(i, r, kRegCat2);
4119 } else if (rt.IsReferenceTypes()) {
4120 table->SetRegCategory(i, r, kRegObject);
4121 } else {
4122 table->SetRegCategory(i, r, kRegUnknown);
4123 }
4124 }
4125 }
4126 }
4127
4128 return table.release();
4129}
4130#endif
4131
Ian Rogersd81871c2011-10-03 13:57:23 -07004132} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004133} // namespace art