blob: 5b8102ac421936629c7550aa3aaecb183d75cb08 [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;
49 case VERIFY_ERROR_GENERIC: os << "VERIFY_ERROR_GENERIC"; break;
50 case VERIFY_ERROR_NO_CLASS: os << "VERIFY_ERROR_NO_CLASS"; break;
51 case VERIFY_ERROR_NO_FIELD: os << "VERIFY_ERROR_NO_FIELD"; break;
52 case VERIFY_ERROR_NO_METHOD: os << "VERIFY_ERROR_NO_METHOD"; break;
53 case VERIFY_ERROR_ACCESS_CLASS: os << "VERIFY_ERROR_ACCESS_CLASS"; break;
54 case VERIFY_ERROR_ACCESS_FIELD: os << "VERIFY_ERROR_ACCESS_FIELD"; break;
55 case VERIFY_ERROR_ACCESS_METHOD: os << "VERIFY_ERROR_ACCESS_METHOD"; break;
56 case VERIFY_ERROR_CLASS_CHANGE: os << "VERIFY_ERROR_CLASS_CHANGE"; break;
57 case VERIFY_ERROR_INSTANTIATION: os << "VERIFY_ERROR_INSTANTIATION"; break;
58 default:
59 os << "VerifyError[" << static_cast<int>(rhs) << "]";
60 break;
61 }
62 return os;
Ian Rogersd81871c2011-10-03 13:57:23 -070063}
jeffhaobdb76512011-09-07 11:43:16 -070064
Ian Rogers84fa0742011-10-25 18:13:30 -070065static const char* type_strings[] = {
66 "Unknown",
67 "Conflict",
68 "Boolean",
69 "Byte",
70 "Short",
71 "Char",
72 "Integer",
73 "Float",
74 "Long (Low Half)",
75 "Long (High Half)",
76 "Double (Low Half)",
77 "Double (High Half)",
78 "64-bit Constant (Low Half)",
79 "64-bit Constant (High Half)",
80 "32-bit Constant",
81 "Unresolved Reference",
82 "Uninitialized Reference",
83 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070084 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070085 "Reference",
86};
Ian Rogersd81871c2011-10-03 13:57:23 -070087
Ian Rogers2c8a8572011-10-24 17:11:36 -070088std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070089 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
90 std::string result;
91 if (IsConstant()) {
92 uint32_t val = ConstantValue();
93 if (val == 0) {
94 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070095 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070096 if(IsConstantShort()) {
97 result = StringPrintf("32-bit Constant: %d", val);
98 } else {
99 result = StringPrintf("32-bit Constant: 0x%x", val);
100 }
101 }
102 } else {
103 result = type_strings[type_];
104 if (IsReferenceTypes()) {
105 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -0700106 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700107 result += PrettyDescriptor(GetDescriptor());
108 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800109 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -0700110 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700111 }
112 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700113 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -0700114}
115
116const RegType& RegType::HighHalf(RegTypeCache* cache) const {
117 CHECK(IsLowHalf());
118 if (type_ == kRegTypeLongLo) {
119 return cache->FromType(kRegTypeLongHi);
120 } else if (type_ == kRegTypeDoubleLo) {
121 return cache->FromType(kRegTypeDoubleHi);
122 } else {
123 return cache->FromType(kRegTypeConstHi);
124 }
125}
126
127/*
128 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
129 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
130 * 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
131 * is the deepest (lowest upper bound) parent of S and T).
132 *
133 * This operation applies for regular classes and arrays, however, for interface types there needn't
134 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
135 * introducing sets of types, however, the only operation permissible on an interface is
136 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
137 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700138 * the perversion of any Object being assignable to an interface type (note, however, that we don't
139 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
140 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700141 */
142Class* RegType::ClassJoin(Class* s, Class* t) {
143 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
144 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
145 if (s == t) {
146 return s;
147 } else if (s->IsAssignableFrom(t)) {
148 return s;
149 } else if (t->IsAssignableFrom(s)) {
150 return t;
151 } else if (s->IsArrayClass() && t->IsArrayClass()) {
152 Class* s_ct = s->GetComponentType();
153 Class* t_ct = t->GetComponentType();
154 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
155 // Given the types aren't the same, if either array is of primitive types then the only
156 // common parent is java.lang.Object
157 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
158 DCHECK(result->IsObjectClass());
159 return result;
160 }
161 Class* common_elem = ClassJoin(s_ct, t_ct);
162 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
163 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800164 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700166 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
167 DCHECK(array_class != NULL);
168 return array_class;
169 } else {
170 size_t s_depth = s->Depth();
171 size_t t_depth = t->Depth();
172 // Get s and t to the same depth in the hierarchy
173 if (s_depth > t_depth) {
174 while (s_depth > t_depth) {
175 s = s->GetSuperClass();
176 s_depth--;
177 }
178 } else {
179 while (t_depth > s_depth) {
180 t = t->GetSuperClass();
181 t_depth--;
182 }
183 }
184 // Go up the hierarchy until we get to the common parent
185 while (s != t) {
186 s = s->GetSuperClass();
187 t = t->GetSuperClass();
188 }
189 return s;
190 }
191}
192
Ian Rogersb5e95b92011-10-25 23:28:55 -0700193bool RegType::IsAssignableFrom(const RegType& src) const {
194 if (Equals(src)) {
195 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700196 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700197 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700198 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
199 case RegType::kRegTypeByte: return src.IsByteTypes();
200 case RegType::kRegTypeShort: return src.IsShortTypes();
201 case RegType::kRegTypeChar: return src.IsCharTypes();
202 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
203 case RegType::kRegTypeFloat: return src.IsFloatTypes();
204 case RegType::kRegTypeLongLo: return src.IsLongTypes();
205 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700206 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700207 if (!IsReferenceTypes()) {
208 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700209 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700210 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700211 return true; // all reference types can be assigned null
212 } else if (!src.IsReferenceTypes()) {
213 return false; // expect src to be a reference type
214 } else if (IsJavaLangObject()) {
215 return true; // all reference types can be assigned to Object
216 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700217 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers5d86e522012-02-01 11:45:24 -0800218 } else if (IsJavaLangObjectArray()) {
Ian Rogers89310de2012-02-01 13:47:30 -0800219 return src.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
Ian Rogers9074b992011-10-26 17:41:55 -0700220 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700221 GetClass()->IsAssignableFrom(src.GetClass())) {
222 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700223 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700224 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700225 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700226 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700227 }
228 }
229}
230
Ian Rogers84fa0742011-10-25 18:13:30 -0700231static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
232 return a.IsConstant() ? b : a;
233}
jeffhaobdb76512011-09-07 11:43:16 -0700234
Ian Rogersd81871c2011-10-03 13:57:23 -0700235const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
236 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700237 if (IsUnknown() && incoming_type.IsUnknown()) {
238 return *this; // Unknown MERGE Unknown => Unknown
239 } else if (IsConflict()) {
240 return *this; // Conflict MERGE * => Conflict
241 } else if (incoming_type.IsConflict()) {
242 return incoming_type; // * MERGE Conflict => Conflict
243 } else if (IsUnknown() || incoming_type.IsUnknown()) {
244 return reg_types->Conflict(); // Unknown MERGE * => Conflict
245 } else if(IsConstant() && incoming_type.IsConstant()) {
246 int32_t val1 = ConstantValue();
247 int32_t val2 = incoming_type.ConstantValue();
248 if (val1 >= 0 && val2 >= 0) {
249 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
250 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700251 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700252 } else {
253 return incoming_type;
254 }
255 } else if (val1 < 0 && val2 < 0) {
256 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
257 if (val1 <= val2) {
258 return *this;
259 } else {
260 return incoming_type;
261 }
262 } else {
263 // Values are +ve and -ve, choose smallest signed type in which they both fit
264 if (IsConstantByte()) {
265 if (incoming_type.IsConstantByte()) {
266 return reg_types->ByteConstant();
267 } else if (incoming_type.IsConstantShort()) {
268 return reg_types->ShortConstant();
269 } else {
270 return reg_types->IntConstant();
271 }
272 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700273 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700274 return reg_types->ShortConstant();
275 } else {
276 return reg_types->IntConstant();
277 }
278 } else {
279 return reg_types->IntConstant();
280 }
281 }
282 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
283 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
284 return reg_types->Boolean(); // boolean MERGE boolean => boolean
285 }
286 if (IsByteTypes() && incoming_type.IsByteTypes()) {
287 return reg_types->Byte(); // byte MERGE byte => byte
288 }
289 if (IsShortTypes() && incoming_type.IsShortTypes()) {
290 return reg_types->Short(); // short MERGE short => short
291 }
292 if (IsCharTypes() && incoming_type.IsCharTypes()) {
293 return reg_types->Char(); // char MERGE char => char
294 }
295 return reg_types->Integer(); // int MERGE * => int
296 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
297 (IsLongTypes() && incoming_type.IsLongTypes()) ||
298 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
299 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
300 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
301 // check constant case was handled prior to entry
302 DCHECK(!IsConstant() || !incoming_type.IsConstant());
303 // float/long/double MERGE float/long/double_constant => float/long/double
304 return SelectNonConstant(*this, incoming_type);
305 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700306 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700307 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700308 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
309 return reg_types->JavaLangObject(); // Object MERGE ref => Object
310 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
311 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
312 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
313 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700314 return reg_types->Conflict();
315 } else { // Two reference types, compute Join
316 Class* c1 = GetClass();
317 Class* c2 = incoming_type.GetClass();
318 DCHECK(c1 != NULL && !c1->IsPrimitive());
319 DCHECK(c2 != NULL && !c2->IsPrimitive());
320 Class* join_class = ClassJoin(c1, c2);
321 if (c1 == join_class) {
322 return *this;
323 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 return incoming_type;
325 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700326 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700328 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700329 } else {
330 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700331 }
332}
333
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700334static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700335 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700336 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
337 case Primitive::kPrimByte: return RegType::kRegTypeByte;
338 case Primitive::kPrimShort: return RegType::kRegTypeShort;
339 case Primitive::kPrimChar: return RegType::kRegTypeChar;
340 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
341 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
342 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
343 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
344 case Primitive::kPrimVoid:
345 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700346 }
347}
348
349static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
350 if (descriptor.length() == 1) {
351 switch (descriptor[0]) {
352 case 'Z': return RegType::kRegTypeBoolean;
353 case 'B': return RegType::kRegTypeByte;
354 case 'S': return RegType::kRegTypeShort;
355 case 'C': return RegType::kRegTypeChar;
356 case 'I': return RegType::kRegTypeInteger;
357 case 'J': return RegType::kRegTypeLongLo;
358 case 'F': return RegType::kRegTypeFloat;
359 case 'D': return RegType::kRegTypeDoubleLo;
360 case 'V':
361 default: return RegType::kRegTypeUnknown;
362 }
363 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
364 return RegType::kRegTypeReference;
365 } else {
366 return RegType::kRegTypeUnknown;
367 }
368}
369
370std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700371 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700372 return os;
373}
374
375const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800376 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
378}
379
380const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800381 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700382 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 // entries should be sized greater than primitive types
384 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
385 RegType* entry = entries_[type];
386 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700387 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800388 if (strlen(descriptor) != 0) {
389 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700390 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700391 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700392 entries_[type] = entry;
393 }
394 return *entry;
395 } else {
396 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800397 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700398 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700399 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700400 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 if (cur_entry->IsReference()) {
402 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800403 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800404 return *cur_entry;
405 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700406 } else if (cur_entry->IsUnresolvedReference() &&
407 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700408 return *cur_entry;
409 }
410 }
Ian Rogers672297c2012-01-10 14:50:55 -0800411 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700412 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700413 // Able to resolve so create resolved register type
414 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700415 entries_.push_back(entry);
416 return *entry;
417 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700418 // TODO: we assume unresolved, but we may be able to do better by validating whether the
419 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700420 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700421 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700422 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800423 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700424 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800425 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700426 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
427 entries_.size());
428 entries_.push_back(entry);
429 return *entry;
430 } else {
431 // The descriptor is broken return the unknown type as there's nothing sensible that
432 // could be done at runtime
433 return Unknown();
434 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 }
437}
438
439const RegType& RegTypeCache::FromClass(Class* klass) {
440 if (klass->IsPrimitive()) {
441 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
442 // entries should be sized greater than primitive types
443 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
444 RegType* entry = entries_[type];
445 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700446 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700447 entries_[type] = entry;
448 }
449 return *entry;
450 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700451 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700453 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700454 return *cur_entry;
455 }
456 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700457 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 entries_.push_back(entry);
459 return *entry;
460 }
461}
462
Ian Rogers28ad40d2011-10-27 15:19:26 -0700463const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
464 RegType* entry;
465 if (type.IsUnresolvedTypes()) {
466 String* descriptor = type.GetDescriptor();
467 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
468 RegType* cur_entry = entries_[i];
469 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
470 cur_entry->GetAllocationPc() == allocation_pc &&
471 cur_entry->GetDescriptor() == descriptor) {
472 return *cur_entry;
473 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700475 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
476 descriptor, allocation_pc, entries_.size());
477 } else {
478 Class* klass = type.GetClass();
479 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
480 RegType* cur_entry = entries_[i];
481 if (cur_entry->IsUninitializedReference() &&
482 cur_entry->GetAllocationPc() == allocation_pc &&
483 cur_entry->GetClass() == klass) {
484 return *cur_entry;
485 }
486 }
487 entry = new RegType(RegType::kRegTypeUninitializedReference,
488 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700490 entries_.push_back(entry);
491 return *entry;
492}
493
494const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
495 RegType* entry;
496 if (uninit_type.IsUnresolvedTypes()) {
497 String* descriptor = uninit_type.GetDescriptor();
498 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
499 RegType* cur_entry = entries_[i];
500 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
501 return *cur_entry;
502 }
503 }
504 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
505 } else {
506 Class* klass = uninit_type.GetClass();
507 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
508 RegType* cur_entry = entries_[i];
509 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
510 return *cur_entry;
511 }
512 }
513 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
514 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 entries_.push_back(entry);
516 return *entry;
517}
518
519const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700520 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 RegType* cur_entry = entries_[i];
522 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
523 return *cur_entry;
524 }
525 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700526 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 entries_.size());
528 entries_.push_back(entry);
529 return *entry;
530}
531
532const RegType& RegTypeCache::FromType(RegType::Type type) {
533 CHECK(type < RegType::kRegTypeReference);
534 switch (type) {
535 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
536 case RegType::kRegTypeByte: return From(type, NULL, "B");
537 case RegType::kRegTypeShort: return From(type, NULL, "S");
538 case RegType::kRegTypeChar: return From(type, NULL, "C");
539 case RegType::kRegTypeInteger: return From(type, NULL, "I");
540 case RegType::kRegTypeFloat: return From(type, NULL, "F");
541 case RegType::kRegTypeLongLo:
542 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
543 case RegType::kRegTypeDoubleLo:
544 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
545 default: return From(type, NULL, "");
546 }
547}
548
549const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700550 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
551 RegType* cur_entry = entries_[i];
552 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
553 return *cur_entry;
554 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700556 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
557 entries_.push_back(entry);
558 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700559}
560
Ian Rogers28ad40d2011-10-27 15:19:26 -0700561const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
Ian Rogers89310de2012-02-01 13:47:30 -0800562 CHECK(array.IsArrayTypes());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700563 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800564 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
565 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800566 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700567 } else {
568 return FromClass(array.GetClass()->GetComponentType());
569 }
570}
571
572
Ian Rogersd81871c2011-10-03 13:57:23 -0700573bool RegisterLine::CheckConstructorReturn() const {
574 for (size_t i = 0; i < num_regs_; i++) {
575 if (GetRegisterType(i).IsUninitializedThisReference()) {
576 verifier_->Fail(VERIFY_ERROR_GENERIC)
577 << "Constructor returning without calling superclass constructor";
578 return false;
579 }
580 }
581 return true;
582}
583
jeffhao60f83e32012-02-13 17:16:30 -0800584bool RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 DCHECK(vdst < num_regs_);
586 if (new_type.IsLowHalf()) {
587 line_[vdst] = new_type.GetId();
588 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
589 } else if (new_type.IsHighHalf()) {
590 /* should never set these explicitly */
591 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
jeffhao60f83e32012-02-13 17:16:30 -0800592 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 } else if (new_type.IsConflict()) { // should only be set during a merge
594 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
jeffhao60f83e32012-02-13 17:16:30 -0800595 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 } else {
597 line_[vdst] = new_type.GetId();
598 }
599 // Clear the monitor entry bits for this register.
600 ClearAllRegToLockDepths(vdst);
jeffhao60f83e32012-02-13 17:16:30 -0800601 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700602}
603
604void RegisterLine::SetResultTypeToUnknown() {
605 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
606 result_[0] = unknown_id;
607 result_[1] = unknown_id;
608}
609
610void RegisterLine::SetResultRegisterType(const RegType& new_type) {
611 result_[0] = new_type.GetId();
612 if(new_type.IsLowHalf()) {
613 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
614 result_[1] = new_type.GetId() + 1;
615 } else {
616 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
617 }
618}
619
620const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
621 // The register index was validated during the static pass, so we don't need to check it here.
622 DCHECK_LT(vsrc, num_regs_);
623 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
624}
625
Elliott Hughesadb8c672012-03-06 16:49:32 -0800626const RegType& RegisterLine::GetInvocationThis(const DecodedInstruction& dec_insn) {
627 if (dec_insn.vA < 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
629 return verifier_->GetRegTypeCache()->Unknown();
630 }
631 /* get the element type of the array held in vsrc */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800632 const RegType& this_type = GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 if (!this_type.IsReferenceTypes()) {
634 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
Elliott Hughesadb8c672012-03-06 16:49:32 -0800635 << dec_insn.vC << " (type=" << this_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 return verifier_->GetRegTypeCache()->Unknown();
637 }
638 return this_type;
639}
640
Ian Rogersd81871c2011-10-03 13:57:23 -0700641bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
642 // Verify the src register type against the check type refining the type of the register
643 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700644 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
646 << " but expected " << check_type;
647 return false;
648 }
jeffhao457cc512012-02-02 16:55:13 -0800649 if (check_type.IsLowHalf()) {
650 const RegType& src_type_h = GetRegisterType(vsrc + 1);
651 if (!src_type.CheckWidePair(src_type_h)) {
652 verifier_->Fail(VERIFY_ERROR_GENERIC) << "wide register v" << vsrc << " has type "
653 << src_type << "/" << src_type_h;
654 return false;
655 }
656 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
658 // precise than the subtype in vsrc so leave it for reference types. For primitive types
659 // if they are a defined type then they are as precise as we can get, however, for constant
660 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
661 return true;
662}
663
664void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700665 DCHECK(uninit_type.IsUninitializedTypes());
666 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
667 size_t changed = 0;
668 for (size_t i = 0; i < num_regs_; i++) {
669 if (GetRegisterType(i).Equals(uninit_type)) {
670 line_[i] = init_type.GetId();
671 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700674 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700675}
676
677void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
678 for (size_t i = 0; i < num_regs_; i++) {
679 if (GetRegisterType(i).Equals(uninit_type)) {
680 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
681 ClearAllRegToLockDepths(i);
682 }
683 }
684}
685
686void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
687 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
688 const RegType& type = GetRegisterType(vsrc);
jeffhao60f83e32012-02-13 17:16:30 -0800689 if (!SetRegisterType(vdst, type)) {
690 return;
691 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
693 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
694 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
695 << " cat=" << static_cast<int>(cat);
696 } else if (cat == kTypeCategoryRef) {
697 CopyRegToLockDepth(vdst, vsrc);
698 }
699}
700
701void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
702 const RegType& type_l = GetRegisterType(vsrc);
703 const RegType& type_h = GetRegisterType(vsrc + 1);
704
705 if (!type_l.CheckWidePair(type_h)) {
706 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
707 << " type=" << type_l << "/" << type_h;
708 } else {
709 SetRegisterType(vdst, type_l); // implicitly sets the second half
710 }
711}
712
713void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
714 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
715 if ((!is_reference && !type.IsCategory1Types()) ||
716 (is_reference && !type.IsReferenceTypes())) {
717 verifier_->Fail(VERIFY_ERROR_GENERIC)
718 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
719 } else {
720 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
721 SetRegisterType(vdst, type);
722 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
723 }
724}
725
726/*
727 * Implement "move-result-wide". Copy the category-2 value from the result
728 * register to another register, and reset the result register.
729 */
730void RegisterLine::CopyResultRegister2(uint32_t vdst) {
731 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
732 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
733 if (!type_l.IsCategory2Types()) {
734 verifier_->Fail(VERIFY_ERROR_GENERIC)
735 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
736 } else {
737 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
738 SetRegisterType(vdst, type_l); // also sets the high
739 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
740 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
741 }
742}
743
Elliott Hughesadb8c672012-03-06 16:49:32 -0800744void RegisterLine::CheckUnaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 const RegType& dst_type, const RegType& src_type) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800746 if (VerifyRegisterType(dec_insn.vB, src_type)) {
747 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 }
749}
750
Elliott Hughesadb8c672012-03-06 16:49:32 -0800751void RegisterLine::CheckBinaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 const RegType& dst_type,
753 const RegType& src_type1, const RegType& src_type2,
754 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800755 if (VerifyRegisterType(dec_insn.vB, src_type1) &&
756 VerifyRegisterType(dec_insn.vC, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (check_boolean_op) {
758 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800759 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
760 GetRegisterType(dec_insn.vC).IsBooleanTypes()) {
761 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 return;
763 }
764 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800765 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 }
767}
768
Elliott Hughesadb8c672012-03-06 16:49:32 -0800769void RegisterLine::CheckBinaryOp2addr(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 const RegType& dst_type, const RegType& src_type1,
771 const RegType& src_type2, bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800772 if (VerifyRegisterType(dec_insn.vA, src_type1) &&
773 VerifyRegisterType(dec_insn.vB, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 if (check_boolean_op) {
775 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800776 if (GetRegisterType(dec_insn.vA).IsBooleanTypes() &&
777 GetRegisterType(dec_insn.vB).IsBooleanTypes()) {
778 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return;
780 }
781 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800782 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 }
784}
785
Elliott Hughesadb8c672012-03-06 16:49:32 -0800786void RegisterLine::CheckLiteralOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 const RegType& dst_type, const RegType& src_type,
788 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800789 if (VerifyRegisterType(dec_insn.vB, src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 if (check_boolean_op) {
791 DCHECK(dst_type.IsInteger());
792 /* check vB with the call, then check the constant manually */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800793 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
794 (dec_insn.vC == 0 || dec_insn.vC == 1)) {
795 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 return;
797 }
798 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800799 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 }
801}
802
803void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
804 const RegType& reg_type = GetRegisterType(reg_idx);
805 if (!reg_type.IsReferenceTypes()) {
806 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800807 } else if (monitors_.size() >= 32) {
808 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 } else {
810 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700811 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 }
813}
814
815void RegisterLine::PopMonitor(uint32_t reg_idx) {
816 const RegType& reg_type = GetRegisterType(reg_idx);
817 if (!reg_type.IsReferenceTypes()) {
818 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
819 } else if (monitors_.empty()) {
820 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
821 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700822 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
824 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
825 // format "036" the constant collector may create unlocks on the same object but referenced
826 // via different registers.
827 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
828 : verifier_->LogVerifyInfo())
829 << "monitor-exit not unlocking the top of the monitor stack";
830 } else {
831 // Record the register was unlocked
832 ClearRegToLockDepth(reg_idx, monitors_.size());
833 }
834 }
835}
836
837bool RegisterLine::VerifyMonitorStackEmpty() {
838 if (MonitorStackDepth() != 0) {
839 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
840 return false;
841 } else {
842 return true;
843 }
844}
845
846bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
847 bool changed = false;
848 for (size_t idx = 0; idx < num_regs_; idx++) {
849 if (line_[idx] != incoming_line->line_[idx]) {
850 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
851 const RegType& cur_type = GetRegisterType(idx);
852 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
853 changed = changed || !cur_type.Equals(new_type);
854 line_[idx] = new_type.GetId();
855 }
856 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700857 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
859 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
860 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
861 for (uint32_t idx = 0; idx < num_regs_; idx++) {
862 size_t depths = reg_to_lock_depths_.count(idx);
863 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
864 if (depths != incoming_depths) {
865 if (depths == 0 || incoming_depths == 0) {
866 reg_to_lock_depths_.erase(idx);
867 } else {
868 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
869 << ": " << depths << " != " << incoming_depths;
870 break;
871 }
872 }
873 }
874 }
875 return changed;
876}
877
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800878void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 for (size_t i = 0; i < num_regs_; i += 8) {
880 uint8_t val = 0;
881 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
882 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700883 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 val |= 1 << j;
885 }
886 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800887 if ((i / 8) >= max_bytes) {
888 DCHECK_EQ(0, val);
889 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800891 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
892 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 }
894}
895
896std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700897 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 return os;
899}
900
901
902void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
903 uint32_t insns_size, uint16_t registers_size,
904 DexVerifier* verifier) {
905 DCHECK_GT(insns_size, 0U);
906
907 for (uint32_t i = 0; i < insns_size; i++) {
908 bool interesting = false;
909 switch (mode) {
910 case kTrackRegsAll:
911 interesting = flags[i].IsOpcode();
912 break;
913 case kTrackRegsGcPoints:
914 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
915 break;
916 case kTrackRegsBranches:
917 interesting = flags[i].IsBranchTarget();
918 break;
919 default:
920 break;
921 }
922 if (interesting) {
923 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
924 }
925 }
926}
927
Ian Rogers1c5eb702012-02-01 09:18:34 -0800928bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700929 if (klass->IsVerified()) {
930 return true;
931 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800933 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800934 error = "Verifier rejected class ";
935 error += PrettyDescriptor(klass);
936 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800939 if (super != NULL && super->IsFinal()) {
940 error = "Verifier rejected class ";
941 error += PrettyDescriptor(klass);
942 error += " that attempts to sub-class final class ";
943 error += PrettyDescriptor(super);
944 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 }
jeffhaobdb76512011-09-07 11:43:16 -0700946 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
947 Method* method = klass->GetDirectMethod(i);
948 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800949 error = "Verifier rejected class ";
950 error += PrettyDescriptor(klass);
951 error += " due to bad method ";
952 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700953 return false;
954 }
955 }
956 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
957 Method* method = klass->GetVirtualMethod(i);
958 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800959 error = "Verifier rejected class ";
960 error += PrettyDescriptor(klass);
961 error += " due to bad method ";
962 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700963 return false;
964 }
965 }
966 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700967}
968
jeffhaobdb76512011-09-07 11:43:16 -0700969bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800971 bool success = verifier.VerifyAll();
Brian Carlstrom75412882012-01-18 01:26:54 -0800972 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
973
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 // We expect either success and no verification error, or failure and a generic failure to
975 // reject the class.
976 if (success) {
977 if (verifier.failure_ != VERIFY_ERROR_NONE) {
978 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
979 << verifier.fail_messages_;
980 }
981 } else {
982 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700983 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700984 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700985 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700986 verifier.Dump(std::cout);
987 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
989 }
990 return success;
991}
992
Shih-wei Liao371814f2011-10-27 16:52:10 -0700993void DexVerifier::VerifyMethodAndDump(Method* method) {
994 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800995 verifier.VerifyAll();
Shih-wei Liao371814f2011-10-27 16:52:10 -0700996
Elliott Hughese0918552011-10-28 17:18:29 -0700997 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
998 << verifier.fail_messages_.str() << std::endl
999 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -07001000}
1001
jeffhaof56197c2012-03-05 18:01:54 -08001002bool DexVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
1003 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
1004 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
1005 const byte* class_data = dex_file->GetClassData(class_def);
1006 ClassDataItemIterator it(*dex_file, class_data);
1007 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
1008 it.Next();
1009 }
1010 while (it.HasNextDirectMethod()) {
1011 uint32_t method_idx = it.GetMemberIndex();
1012 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
1013 it.GetMethodCodeItem())) {
1014 error = "Verifier rejected class";
1015 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
1016 error += " due to bad method ";
1017 error += PrettyMethod(method_idx, *dex_file);
1018 return false;
1019 }
1020 it.Next();
1021 }
1022 while (it.HasNextVirtualMethod()) {
1023 uint32_t method_idx = it.GetMemberIndex();
1024 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
1025 it.GetMethodCodeItem())) {
1026 error = "Verifier rejected class";
1027 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
1028 error += " due to bad method ";
1029 error += PrettyMethod(method_idx, *dex_file);
1030 return false;
1031 }
1032 it.Next();
1033 }
1034 return true;
1035}
1036
1037bool DexVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
1038 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) {
1039 DexVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item);
1040
1041 // Without a method*, we can only verify the struture.
1042 bool success = verifier.VerifyStructure();
1043 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
1044
1045 // We expect either success and no verification error, or failure and a generic failure to
1046 // reject the class.
1047 if (success) {
1048 if (verifier.failure_ != VERIFY_ERROR_NONE) {
1049 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method_idx, *dex_file)
1050 << std::endl << verifier.fail_messages_;
1051 }
1052 } else {
1053 LOG(INFO) << "Verification error in " << PrettyMethod(method_idx, *dex_file) << " "
1054 << verifier.fail_messages_.str();
1055 if (gDebugVerify) {
1056 std::cout << std::endl << verifier.info_messages_.str();
1057 verifier.Dump(std::cout);
1058 }
1059 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
1060 }
1061 return success;
1062}
1063
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001064DexVerifier::DexVerifier(Method* method)
1065 : work_insn_idx_(-1),
1066 method_(method),
1067 failure_(VERIFY_ERROR_NONE),
1068 new_instance_count_(0),
1069 monitor_enter_count_(0) {
1070 CHECK(method != NULL);
jeffhaof56197c2012-03-05 18:01:54 -08001071 dex_cache_ = method->GetDeclaringClass()->GetDexCache();
1072 class_loader_ = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001073 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -08001074 dex_file_ = &class_linker->FindDexFile(dex_cache_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaof56197c2012-03-05 18:01:54 -08001076 const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef();
1077 class_def_idx_ = dex_file_->GetIndexForClassDef(*class_def);
jeffhaoba5ebb92011-08-25 17:24:37 -07001078}
1079
jeffhaof56197c2012-03-05 18:01:54 -08001080DexVerifier::DexVerifier(const DexFile* dex_file, DexCache* dex_cache,
1081 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item)
1082 : work_insn_idx_(-1),
1083 method_(NULL),
1084 dex_file_(dex_file),
1085 dex_cache_(dex_cache),
1086 class_loader_(class_loader),
1087 class_def_idx_(class_def_idx),
1088 code_item_(code_item),
1089 failure_(VERIFY_ERROR_NONE),
1090 new_instance_count_(0),
1091 monitor_enter_count_(0) {
1092}
1093
1094bool DexVerifier::VerifyAll() {
1095 CHECK(method_ != NULL);
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 // If there aren't any instructions, make sure that's expected, then exit successfully.
1097 if (code_item_ == NULL) {
1098 if (!method_->IsNative() && !method_->IsAbstract()) {
1099 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001100 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 } else {
1102 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
jeffhaobdb76512011-09-07 11:43:16 -07001104 }
jeffhaof56197c2012-03-05 18:01:54 -08001105 return VerifyStructure() && VerifyCodeFlow();
1106}
1107
1108bool DexVerifier::VerifyStructure() {
1109 if (code_item_ == NULL) {
1110 return true;
1111 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1113 if (code_item_->ins_size_ > code_item_->registers_size_) {
1114 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1115 << " regs=" << code_item_->registers_size_;
1116 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 // Allocate and initialize an array to hold instruction data.
1119 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1120 // Run through the instructions and see if the width checks out.
1121 bool result = ComputeWidthsAndCountOps();
1122 // Flag instructions guarded by a "try" block and check exception handlers.
1123 result = result && ScanTryCatchBlocks();
1124 // Perform static instruction verification.
1125 result = result && VerifyInstructions();
jeffhaobdb76512011-09-07 11:43:16 -07001126 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001127}
1128
Ian Rogers47a05882012-02-03 12:23:33 -08001129std::ostream& DexVerifier::Fail(VerifyError error) {
1130 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers47a05882012-02-03 12:23:33 -08001131 if (Runtime::Current()->IsCompiler()) {
jeffhaod1224c72012-02-29 13:43:08 -08001132 switch (error) {
1133 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
1134 // errors into generic errors so that we re-verify at runtime. We may fail to find or to agree
1135 // on access because of not yet available class loaders, or class loaders that will differ at
1136 // runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001137 case VERIFY_ERROR_NO_CLASS:
1138 case VERIFY_ERROR_NO_FIELD:
1139 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -08001140 case VERIFY_ERROR_ACCESS_CLASS:
1141 case VERIFY_ERROR_ACCESS_FIELD:
1142 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers47a05882012-02-03 12:23:33 -08001143 error = VERIFY_ERROR_GENERIC;
1144 break;
jeffhaod1224c72012-02-29 13:43:08 -08001145 // Generic failures at compile time will still fail at runtime, so the class is marked as
1146 // rejected to prevent it from being compiled.
1147 case VERIFY_ERROR_GENERIC: {
jeffhaof56197c2012-03-05 18:01:54 -08001148 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -08001149 AddRejectedClass(ref);
1150 break;
1151 }
Ian Rogers47a05882012-02-03 12:23:33 -08001152 default:
1153 break;
1154 }
1155 }
1156 failure_ = error;
1157 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1158 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1159}
1160
Ian Rogersd81871c2011-10-03 13:57:23 -07001161bool DexVerifier::ComputeWidthsAndCountOps() {
1162 const uint16_t* insns = code_item_->insns_;
1163 size_t insns_size = code_item_->insns_size_in_code_units_;
1164 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001165 size_t new_instance_count = 0;
1166 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001168
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001170 Instruction::Code opcode = inst->Opcode();
1171 if (opcode == Instruction::NEW_INSTANCE) {
1172 new_instance_count++;
1173 } else if (opcode == Instruction::MONITOR_ENTER) {
1174 monitor_enter_count++;
1175 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 size_t inst_size = inst->SizeInCodeUnits();
1177 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1178 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001179 inst = inst->Next();
1180 }
1181
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 if (dex_pc != insns_size) {
1183 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1184 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001185 return false;
1186 }
1187
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 new_instance_count_ = new_instance_count;
1189 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001190 return true;
1191}
1192
Ian Rogersd81871c2011-10-03 13:57:23 -07001193bool DexVerifier::ScanTryCatchBlocks() {
1194 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001195 if (tries_size == 0) {
1196 return true;
1197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001199 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001200
1201 for (uint32_t idx = 0; idx < tries_size; idx++) {
1202 const DexFile::TryItem* try_item = &tries[idx];
1203 uint32_t start = try_item->start_addr_;
1204 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001205 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1207 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001208 return false;
1209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 if (!insn_flags_[start].IsOpcode()) {
1211 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001212 return false;
1213 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 for (uint32_t dex_pc = start; dex_pc < end;
1215 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1216 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001217 }
1218 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001219 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001220 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001221 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001222 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001223 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001224 CatchHandlerIterator iterator(handlers_ptr);
1225 for (; iterator.HasNext(); iterator.Next()) {
1226 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 if (!insn_flags_[dex_pc].IsOpcode()) {
1228 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001229 return false;
1230 }
jeffhao60f83e32012-02-13 17:16:30 -08001231 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1232 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
1233 Fail(VERIFY_ERROR_GENERIC) << "exception handler doesn't start with move-exception ("
1234 << dex_pc << ")";
1235 return false;
1236 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001238 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1239 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001240 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -08001241 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
1242 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001243 if (exception_type == NULL) {
1244 DCHECK(Thread::Current()->IsExceptionPending());
1245 Thread::Current()->ClearException();
1246 }
1247 }
jeffhaobdb76512011-09-07 11:43:16 -07001248 }
Ian Rogers0571d352011-11-03 19:51:38 -07001249 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001250 }
jeffhaobdb76512011-09-07 11:43:16 -07001251 return true;
1252}
1253
Ian Rogersd81871c2011-10-03 13:57:23 -07001254bool DexVerifier::VerifyInstructions() {
1255 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001256
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 /* Flag the start of the method as a branch target. */
1258 insn_flags_[0].SetBranchTarget();
1259
1260 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1261 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1262 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001263 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
jeffhaod1224c72012-02-29 13:43:08 -08001264 fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 /* Flag instructions that are garbage collection points */
1268 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1269 insn_flags_[dex_pc].SetGcPoint();
1270 }
1271 dex_pc += inst->SizeInCodeUnits();
1272 inst = inst->Next();
1273 }
1274 return true;
1275}
1276
1277bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001278 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 bool result = true;
1280 switch (inst->GetVerifyTypeArgumentA()) {
1281 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 break;
1284 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001285 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 break;
1287 }
1288 switch (inst->GetVerifyTypeArgumentB()) {
1289 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001290 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 break;
1292 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001293 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 break;
1295 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001296 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 break;
1298 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 break;
1301 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001302 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 break;
1304 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001305 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 break;
1307 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001308 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 break;
1310 }
1311 switch (inst->GetVerifyTypeArgumentC()) {
1312 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001313 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 break;
1315 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001316 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 break;
1318 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001319 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 break;
1321 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 break;
1324 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 break;
1327 }
1328 switch (inst->GetVerifyExtraFlags()) {
1329 case Instruction::kVerifyArrayData:
1330 result = result && CheckArrayData(code_offset);
1331 break;
1332 case Instruction::kVerifyBranchTarget:
1333 result = result && CheckBranchTarget(code_offset);
1334 break;
1335 case Instruction::kVerifySwitchTargets:
1336 result = result && CheckSwitchTargets(code_offset);
1337 break;
1338 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 break;
1341 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001342 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 break;
1344 case Instruction::kVerifyError:
1345 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1346 result = false;
1347 break;
1348 }
1349 return result;
1350}
1351
1352bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1353 if (idx >= code_item_->registers_size_) {
1354 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1355 << code_item_->registers_size_ << ")";
1356 return false;
1357 }
1358 return true;
1359}
1360
1361bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1362 if (idx + 1 >= code_item_->registers_size_) {
1363 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1364 << "+1 >= " << code_item_->registers_size_ << ")";
1365 return false;
1366 }
1367 return true;
1368}
1369
1370bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1371 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1372 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1373 << dex_file_->GetHeader().field_ids_size_ << ")";
1374 return false;
1375 }
1376 return true;
1377}
1378
1379bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1380 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1381 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1382 << dex_file_->GetHeader().method_ids_size_ << ")";
1383 return false;
1384 }
1385 return true;
1386}
1387
1388bool DexVerifier::CheckNewInstance(uint32_t idx) {
1389 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1390 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1391 << dex_file_->GetHeader().type_ids_size_ << ")";
1392 return false;
1393 }
1394 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001395 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 if (descriptor[0] != 'L') {
1397 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1398 return false;
1399 }
1400 return true;
1401}
1402
1403bool DexVerifier::CheckStringIndex(uint32_t idx) {
1404 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1405 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1406 << dex_file_->GetHeader().string_ids_size_ << ")";
1407 return false;
1408 }
1409 return true;
1410}
1411
1412bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1413 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1414 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1415 << dex_file_->GetHeader().type_ids_size_ << ")";
1416 return false;
1417 }
1418 return true;
1419}
1420
1421bool DexVerifier::CheckNewArray(uint32_t idx) {
1422 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1423 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1424 << dex_file_->GetHeader().type_ids_size_ << ")";
1425 return false;
1426 }
1427 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001428 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 const char* cp = descriptor;
1430 while (*cp++ == '[') {
1431 bracket_count++;
1432 }
1433 if (bracket_count == 0) {
1434 /* The given class must be an array type. */
1435 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1436 return false;
1437 } else if (bracket_count > 255) {
1438 /* It is illegal to create an array of more than 255 dimensions. */
1439 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1440 return false;
1441 }
1442 return true;
1443}
1444
1445bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1446 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1447 const uint16_t* insns = code_item_->insns_ + cur_offset;
1448 const uint16_t* array_data;
1449 int32_t array_data_offset;
1450
1451 DCHECK_LT(cur_offset, insn_count);
1452 /* make sure the start of the array data table is in range */
1453 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1454 if ((int32_t) cur_offset + array_data_offset < 0 ||
1455 cur_offset + array_data_offset + 2 >= insn_count) {
1456 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1457 << ", data offset " << array_data_offset << ", count " << insn_count;
1458 return false;
1459 }
1460 /* offset to array data table is a relative branch-style offset */
1461 array_data = insns + array_data_offset;
1462 /* make sure the table is 32-bit aligned */
1463 if ((((uint32_t) array_data) & 0x03) != 0) {
1464 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1465 << ", data offset " << array_data_offset;
1466 return false;
1467 }
1468 uint32_t value_width = array_data[1];
1469 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1470 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1471 /* make sure the end of the switch is in range */
1472 if (cur_offset + array_data_offset + table_size > insn_count) {
1473 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1474 << ", data offset " << array_data_offset << ", end "
1475 << cur_offset + array_data_offset + table_size
1476 << ", count " << insn_count;
1477 return false;
1478 }
1479 return true;
1480}
1481
1482bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1483 int32_t offset;
1484 bool isConditional, selfOkay;
1485 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1486 return false;
1487 }
1488 if (!selfOkay && offset == 0) {
1489 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1490 return false;
1491 }
1492 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1493 // identical "wrap-around" behavior, but it's unwise to depend on that.
1494 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1495 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1496 return false;
1497 }
1498 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1499 int32_t abs_offset = cur_offset + offset;
1500 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1501 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1502 << (void*) abs_offset << ") at " << (void*) cur_offset;
1503 return false;
1504 }
1505 insn_flags_[abs_offset].SetBranchTarget();
1506 return true;
1507}
1508
1509bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1510 bool* selfOkay) {
1511 const uint16_t* insns = code_item_->insns_ + cur_offset;
1512 *pConditional = false;
1513 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001514 switch (*insns & 0xff) {
1515 case Instruction::GOTO:
1516 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001517 break;
1518 case Instruction::GOTO_32:
1519 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 *selfOkay = true;
1521 break;
1522 case Instruction::GOTO_16:
1523 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 break;
1525 case Instruction::IF_EQ:
1526 case Instruction::IF_NE:
1527 case Instruction::IF_LT:
1528 case Instruction::IF_GE:
1529 case Instruction::IF_GT:
1530 case Instruction::IF_LE:
1531 case Instruction::IF_EQZ:
1532 case Instruction::IF_NEZ:
1533 case Instruction::IF_LTZ:
1534 case Instruction::IF_GEZ:
1535 case Instruction::IF_GTZ:
1536 case Instruction::IF_LEZ:
1537 *pOffset = (int16_t) insns[1];
1538 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001539 break;
1540 default:
1541 return false;
1542 break;
1543 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001544 return true;
1545}
1546
Ian Rogersd81871c2011-10-03 13:57:23 -07001547bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1548 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001549 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001551 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1553 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1554 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1555 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001556 return false;
1557 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001558 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001560 /* make sure the table is 32-bit aligned */
1561 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1563 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 return false;
1565 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 uint32_t switch_count = switch_insns[1];
1567 int32_t keys_offset, targets_offset;
1568 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001569 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1570 /* 0=sig, 1=count, 2/3=firstKey */
1571 targets_offset = 4;
1572 keys_offset = -1;
1573 expected_signature = Instruction::kPackedSwitchSignature;
1574 } else {
1575 /* 0=sig, 1=count, 2..count*2 = keys */
1576 keys_offset = 2;
1577 targets_offset = 2 + 2 * switch_count;
1578 expected_signature = Instruction::kSparseSwitchSignature;
1579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001581 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001582 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1583 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001584 return false;
1585 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001586 /* make sure the end of the switch is in range */
1587 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1589 << switch_offset << ", end "
1590 << (cur_offset + switch_offset + table_size)
1591 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001592 return false;
1593 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001594 /* for a sparse switch, verify the keys are in ascending order */
1595 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1597 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001598 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1599 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1600 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1602 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001603 return false;
1604 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001605 last_key = key;
1606 }
1607 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001608 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 for (uint32_t targ = 0; targ < switch_count; targ++) {
1610 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1611 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1612 int32_t abs_offset = cur_offset + offset;
1613 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1614 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1615 << (void*) abs_offset << ") at "
1616 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001617 return false;
1618 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 insn_flags_[abs_offset].SetBranchTarget();
1620 }
1621 return true;
1622}
1623
1624bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1625 if (vA > 5) {
1626 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1627 return false;
1628 }
1629 uint16_t registers_size = code_item_->registers_size_;
1630 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001631 if (arg[idx] >= registers_size) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
jeffhao457cc512012-02-02 16:55:13 -08001633 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 return false;
1635 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001636 }
1637
1638 return true;
1639}
1640
Ian Rogersd81871c2011-10-03 13:57:23 -07001641bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1642 uint16_t registers_size = code_item_->registers_size_;
1643 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1644 // integer overflow when adding them here.
1645 if (vA + vC > registers_size) {
1646 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1647 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001648 return false;
1649 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001650 return true;
1651}
1652
Brian Carlstrom75412882012-01-18 01:26:54 -08001653const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1654 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1655 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1656 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1657 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1658 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1659 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1660 gc_map.begin(),
1661 gc_map.end());
1662 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1663 DCHECK_EQ(gc_map.size(),
1664 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1665 (length_prefixed_gc_map->at(1) << 16) |
1666 (length_prefixed_gc_map->at(2) << 8) |
1667 (length_prefixed_gc_map->at(3) << 0)));
1668 return length_prefixed_gc_map;
1669}
1670
Ian Rogersd81871c2011-10-03 13:57:23 -07001671bool DexVerifier::VerifyCodeFlow() {
1672 uint16_t registers_size = code_item_->registers_size_;
1673 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001674
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001676 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1677 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
1679 /* Create and initialize table holding register status */
1680 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1681 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001682
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 work_line_.reset(new RegisterLine(registers_size, this));
1684 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001685
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 /* Initialize register types of method arguments. */
1687 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001688 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1689 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 return false;
1691 }
1692 /* Perform code flow verification. */
1693 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001694 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001696 }
1697
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001699 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1700 if (map.get() == NULL) {
1701 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 return false; // Not a real failure, but a failure to encode
1703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001704#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001705 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001706#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001707 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1708 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1709 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001710
1711#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom75412882012-01-18 01:26:54 -08001712 method_->SetGcMap(&gc_map->at(0));
Logan Chienfca7e872011-12-20 20:08:22 +08001713#else
1714 /* Generate Inferred Register Category for LLVM-based Code Generator */
1715 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
1716 method_->SetInferredRegCategoryMap(table);
1717#endif
1718
jeffhaobdb76512011-09-07 11:43:16 -07001719 return true;
1720}
1721
Ian Rogersd81871c2011-10-03 13:57:23 -07001722void DexVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001723 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 os << "Native method" << std::endl;
1725 return;
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 DCHECK(code_item_ != NULL);
1728 const Instruction* inst = Instruction::At(code_item_->insns_);
1729 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1730 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001731 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001732 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1734 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001735 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001736 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001737 inst = inst->Next();
1738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739}
1740
Ian Rogersd81871c2011-10-03 13:57:23 -07001741static bool IsPrimitiveDescriptor(char descriptor) {
1742 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001743 case 'I':
1744 case 'C':
1745 case 'S':
1746 case 'B':
1747 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001748 case 'F':
1749 case 'D':
1750 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001752 default:
1753 return false;
1754 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001755}
1756
Ian Rogersd81871c2011-10-03 13:57:23 -07001757bool DexVerifier::SetTypesFromSignature() {
1758 RegisterLine* reg_line = reg_table_.GetLine(0);
1759 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1760 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001761
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1763 //Include the "this" pointer.
1764 size_t cur_arg = 0;
1765 if (!method_->IsStatic()) {
1766 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1767 // argument as uninitialized. This restricts field access until the superclass constructor is
1768 // called.
1769 Class* declaring_class = method_->GetDeclaringClass();
1770 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1771 reg_line->SetRegisterType(arg_start + cur_arg,
1772 reg_types_.UninitializedThisArgument(declaring_class));
1773 } else {
1774 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001775 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001777 }
1778
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001779 const DexFile::ProtoId& proto_id =
1780 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001781 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001782
1783 for (; iterator.HasNext(); iterator.Next()) {
1784 const char* descriptor = iterator.GetDescriptor();
1785 if (descriptor == NULL) {
1786 LOG(FATAL) << "Null descriptor";
1787 }
1788 if (cur_arg >= expected_args) {
1789 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1790 << " args, found more (" << descriptor << ")";
1791 return false;
1792 }
1793 switch (descriptor[0]) {
1794 case 'L':
1795 case '[':
1796 // We assume that reference arguments are initialized. The only way it could be otherwise
1797 // (assuming the caller was verified) is if the current method is <init>, but in that case
1798 // it's effectively considered initialized the instant we reach here (in the sense that we
1799 // can return without doing anything or call virtual methods).
1800 {
1801 const RegType& reg_type =
1802 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001803 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 }
1805 break;
1806 case 'Z':
1807 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1808 break;
1809 case 'C':
1810 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1811 break;
1812 case 'B':
1813 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1814 break;
1815 case 'I':
1816 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1817 break;
1818 case 'S':
1819 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1820 break;
1821 case 'F':
1822 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1823 break;
1824 case 'J':
1825 case 'D': {
1826 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1827 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1828 cur_arg++;
1829 break;
1830 }
1831 default:
1832 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1833 return false;
1834 }
1835 cur_arg++;
1836 }
1837 if (cur_arg != expected_args) {
1838 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1839 return false;
1840 }
1841 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1842 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1843 // format. Only major difference from the method argument format is that 'V' is supported.
1844 bool result;
1845 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1846 result = descriptor[1] == '\0';
1847 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1848 size_t i = 0;
1849 do {
1850 i++;
1851 } while (descriptor[i] == '['); // process leading [
1852 if (descriptor[i] == 'L') { // object array
1853 do {
1854 i++; // find closing ;
1855 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1856 result = descriptor[i] == ';';
1857 } else { // primitive array
1858 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1859 }
1860 } else if (descriptor[0] == 'L') {
1861 // could be more thorough here, but shouldn't be required
1862 size_t i = 0;
1863 do {
1864 i++;
1865 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1866 result = descriptor[i] == ';';
1867 } else {
1868 result = false;
1869 }
1870 if (!result) {
1871 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1872 << descriptor << "'";
1873 }
1874 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001875}
1876
Ian Rogersd81871c2011-10-03 13:57:23 -07001877bool DexVerifier::CodeFlowVerifyMethod() {
1878 const uint16_t* insns = code_item_->insns_;
1879 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001880
jeffhaobdb76512011-09-07 11:43:16 -07001881 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 insn_flags_[0].SetChanged();
1883 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001884
jeffhaobdb76512011-09-07 11:43:16 -07001885 /* Continue until no instructions are marked "changed". */
1886 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1888 uint32_t insn_idx = start_guess;
1889 for (; insn_idx < insns_size; insn_idx++) {
1890 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 if (insn_idx == insns_size) {
1894 if (start_guess != 0) {
1895 /* try again, starting from the top */
1896 start_guess = 0;
1897 continue;
1898 } else {
1899 /* all flags are clear */
1900 break;
1901 }
1902 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 // We carry the working set of registers from instruction to instruction. If this address can
1904 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1905 // "changed" flags, we need to load the set of registers from the table.
1906 // Because we always prefer to continue on to the next instruction, we should never have a
1907 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1908 // target.
1909 work_insn_idx_ = insn_idx;
1910 if (insn_flags_[insn_idx].IsBranchTarget()) {
1911 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001912 } else {
1913#ifndef NDEBUG
1914 /*
1915 * Sanity check: retrieve the stored register line (assuming
1916 * a full table) and make sure it actually matches.
1917 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1919 if (register_line != NULL) {
1920 if (work_line_->CompareLine(register_line) != 0) {
1921 Dump(std::cout);
1922 std::cout << info_messages_.str();
1923 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1924 << "@" << (void*)work_insn_idx_ << std::endl
1925 << " work_line=" << *work_line_ << std::endl
1926 << " expected=" << *register_line;
1927 }
jeffhaobdb76512011-09-07 11:43:16 -07001928 }
1929#endif
1930 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 if (!CodeFlowVerifyInstruction(&start_guess)) {
1932 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001933 return false;
1934 }
jeffhaobdb76512011-09-07 11:43:16 -07001935 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 insn_flags_[insn_idx].SetVisited();
1937 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001938 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001939
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001941 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001942 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001943 * (besides the wasted space), but it indicates a flaw somewhere
1944 * down the line, possibly in the verifier.
1945 *
1946 * If we've substituted "always throw" instructions into the stream,
1947 * we are almost certainly going to have some dead code.
1948 */
1949 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 uint32_t insn_idx = 0;
1951 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001952 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001953 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001954 * may or may not be preceded by a padding NOP (for alignment).
1955 */
1956 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1957 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1958 insns[insn_idx] == Instruction::kArrayDataSignature ||
1959 (insns[insn_idx] == Instruction::NOP &&
1960 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1961 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1962 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001964 }
1965
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 if (dead_start < 0)
1968 dead_start = insn_idx;
1969 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001971 dead_start = -1;
1972 }
1973 }
1974 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001976 }
1977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 return true;
1979}
1980
Ian Rogersd81871c2011-10-03 13:57:23 -07001981bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001982#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001984 gDvm.verifierStats.instrsReexamined++;
1985 } else {
1986 gDvm.verifierStats.instrsExamined++;
1987 }
1988#endif
1989
1990 /*
1991 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001992 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001993 * control to another statement:
1994 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001995 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001996 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001997 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001998 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001999 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002000 * throw an exception that is handled by an encompassing "try"
2001 * block.
2002 *
2003 * We can also return, in which case there is no successor instruction
2004 * from this point.
2005 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002006 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002007 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2009 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002010 DecodedInstruction dec_insn(inst);
2011 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002012
jeffhaobdb76512011-09-07 11:43:16 -07002013 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002014 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07002015 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002017 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2018 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 }
jeffhaobdb76512011-09-07 11:43:16 -07002020
2021 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002022 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002023 * can throw an exception, we will copy/merge this into the "catch"
2024 * address rather than work_line, because we don't want the result
2025 * from the "successful" code path (e.g. a check-cast that "improves"
2026 * a type) to be visible to the exception handler.
2027 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002028 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07002030 } else {
2031#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002033#endif
2034 }
2035
Elliott Hughesadb8c672012-03-06 16:49:32 -08002036 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::NOP:
2038 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002039 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002040 * a signature that looks like a NOP; if we see one of these in
2041 * the course of executing code then we have a problem.
2042 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002043 if (dec_insn.vA != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002045 }
2046 break;
2047
2048 case Instruction::MOVE:
2049 case Instruction::MOVE_FROM16:
2050 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002051 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::MOVE_WIDE:
2054 case Instruction::MOVE_WIDE_FROM16:
2055 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002056 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
2058 case Instruction::MOVE_OBJECT:
2059 case Instruction::MOVE_OBJECT_FROM16:
2060 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002061 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063
2064 /*
2065 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002067 * might want to hold the result in an actual CPU register, so the
2068 * Dalvik spec requires that these only appear immediately after an
2069 * invoke or filled-new-array.
2070 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002071 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002072 * redundant with the reset done below, but it can make the debug info
2073 * easier to read in some cases.)
2074 */
2075 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002082 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07002086 /*
jeffhao60f83e32012-02-13 17:16:30 -08002087 * This statement can only appear as the first instruction in an exception handler. We verify
2088 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002089 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002090 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 }
jeffhaobdb76512011-09-07 11:43:16 -07002094 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2096 if (!GetMethodReturnType().IsUnknown()) {
2097 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
2098 }
jeffhaobdb76512011-09-07 11:43:16 -07002099 }
2100 break;
2101 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002103 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 const RegType& return_type = GetMethodReturnType();
2105 if (!return_type.IsCategory1Types()) {
2106 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
2107 } else {
2108 // Compilers may generate synthetic functions that write byte values into boolean fields.
2109 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002110 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2112 ((return_type.IsBoolean() || return_type.IsByte() ||
2113 return_type.IsShort() || return_type.IsChar()) &&
2114 src_type.IsInteger()));
2115 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002116 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002118 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 }
jeffhaobdb76512011-09-07 11:43:16 -07002120 }
2121 }
2122 break;
2123 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002125 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 const RegType& return_type = GetMethodReturnType();
2127 if (!return_type.IsCategory2Types()) {
2128 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
2129 } else {
2130 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002131 work_line_->VerifyRegisterType(dec_insn.vA, return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002133 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 }
jeffhaobdb76512011-09-07 11:43:16 -07002135 }
2136 }
2137 break;
2138 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2140 const RegType& return_type = GetMethodReturnType();
2141 if (!return_type.IsReferenceTypes()) {
2142 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2143 } else {
2144 /* return_type is the *expected* return type, not register value */
2145 DCHECK(!return_type.IsZero());
2146 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07002148 // Disallow returning uninitialized values and verify that the reference in vAA is an
2149 // instance of the "return_type"
2150 if (reg_type.IsUninitializedTypes()) {
2151 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2152 } else if (!return_type.IsAssignableFrom(reg_type)) {
2153 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2154 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002155 }
2156 }
2157 }
2158 break;
2159
2160 case Instruction::CONST_4:
2161 case Instruction::CONST_16:
2162 case Instruction::CONST:
2163 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002164 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::CONST_HIGH16:
2167 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002168 work_line_->SetRegisterType(dec_insn.vA,
2169 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::CONST_WIDE_16:
2172 case Instruction::CONST_WIDE_32:
2173 case Instruction::CONST_WIDE:
2174 case Instruction::CONST_WIDE_HIGH16:
2175 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002176 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::CONST_STRING:
2179 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002180 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002183 // Get type from instruction if unresolved then we need an access check
2184 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002186 // Register holds class, ie its type is class, but on error we keep it Unknown
Elliott Hughesadb8c672012-03-06 16:49:32 -08002187 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers28ad40d2011-10-27 15:19:26 -07002188 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002192 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::MONITOR_EXIT:
2195 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002196 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002197 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002198 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002199 * to the need to handle asynchronous exceptions, a now-deprecated
2200 * feature that Dalvik doesn't support.)
2201 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002202 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002203 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002204 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002205 * structured locking checks are working, the former would have
2206 * failed on the -enter instruction, and the latter is impossible.
2207 *
2208 * This is fortunate, because issue 3221411 prevents us from
2209 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002210 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002211 * some catch blocks (which will show up as "dead" code when
2212 * we skip them here); if we can't, then the code path could be
2213 * "live" so we still need to check it.
2214 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002215 opcode_flags &= ~Instruction::kThrow;
2216 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002217 break;
2218
Ian Rogers28ad40d2011-10-27 15:19:26 -07002219 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002221 /*
2222 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2223 * could be a "upcast" -- not expected, so we don't try to address it.)
2224 *
2225 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002226 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002227 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002228 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002229 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002230 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002231 if (res_type.IsUnknown()) {
2232 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2233 break; // couldn't resolve class
2234 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002235 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2236 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002237 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 if (!res_type.IsNonZeroReferenceTypes()) {
2239 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2240 } else if (!orig_type.IsReferenceTypes()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002241 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07002242 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002244 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002246 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002247 }
jeffhaobdb76512011-09-07 11:43:16 -07002248 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002249 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 }
2251 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002252 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002253 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002254 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002257 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
2259 }
2260 break;
2261 }
2262 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002263 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
jeffhao8cd6dda2012-02-22 10:15:34 -08002264 if (res_type.IsUnknown()) {
2265 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2266 break; // couldn't resolve class
2267 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002268 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2269 // can't create an instance of an interface or abstract class */
2270 if (!res_type.IsInstantiableTypes()) {
2271 Fail(VERIFY_ERROR_INSTANTIATION)
2272 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002274 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2275 // Any registers holding previous allocations from this address that have not yet been
2276 // initialized must be marked invalid.
2277 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2278 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08002279 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 }
2281 break;
2282 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002283 case Instruction::NEW_ARRAY:
2284 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002287 VerifyNewArray(dec_insn, true, false);
2288 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002290 case Instruction::FILLED_NEW_ARRAY_RANGE:
2291 VerifyNewArray(dec_insn, true, true);
2292 just_set_result = true; // Filled new array range sets result register
2293 break;
jeffhaobdb76512011-09-07 11:43:16 -07002294 case Instruction::CMPL_FLOAT:
2295 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002296 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002297 break;
2298 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002299 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002300 break;
2301 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002302 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304 case Instruction::CMPL_DOUBLE:
2305 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002306 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002307 break;
2308 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002309 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002310 break;
2311 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002312 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002315 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002316 break;
2317 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002319 break;
2320 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002321 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002324 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002325 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2326 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002327 }
2328 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 }
jeffhaobdb76512011-09-07 11:43:16 -07002330 case Instruction::GOTO:
2331 case Instruction::GOTO_16:
2332 case Instruction::GOTO_32:
2333 /* no effect on or use of registers */
2334 break;
2335
2336 case Instruction::PACKED_SWITCH:
2337 case Instruction::SPARSE_SWITCH:
2338 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 case Instruction::FILL_ARRAY_DATA: {
2343 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002344 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08002345 /* array_type can be null if the reg type is Zero */
2346 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002347 if (!array_type.IsArrayTypes()) {
2348 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002349 } else {
jeffhao457cc512012-02-02 16:55:13 -08002350 const RegType& component_type = reg_types_.GetComponentType(array_type,
2351 method_->GetDeclaringClass()->GetClassLoader());
2352 DCHECK(!component_type.IsUnknown());
2353 if (component_type.IsNonZeroReferenceTypes()) {
2354 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with component type "
2355 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 } else {
jeffhao457cc512012-02-02 16:55:13 -08002357 // Now verify if the element width in the table matches the element width declared in
2358 // the array
2359 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2360 if (array_data[0] != Instruction::kArrayDataSignature) {
2361 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2362 } else {
2363 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2364 // Since we don't compress the data in Dex, expect to see equal width of data stored
2365 // in the table and expected from the array class.
2366 if (array_data[1] != elem_width) {
2367 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2368 << " vs " << elem_width << ")";
2369 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 }
2371 }
jeffhaobdb76512011-09-07 11:43:16 -07002372 }
2373 }
2374 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 }
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2379 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 bool mismatch = false;
2381 if (reg_type1.IsZero()) { // zero then integral or reference expected
2382 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2383 } else if (reg_type1.IsReferenceTypes()) { // both references?
2384 mismatch = !reg_type2.IsReferenceTypes();
2385 } else { // both integral?
2386 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2387 }
2388 if (mismatch) {
2389 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2390 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002391 }
2392 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 }
jeffhaobdb76512011-09-07 11:43:16 -07002394 case Instruction::IF_LT:
2395 case Instruction::IF_GE:
2396 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2399 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2401 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2402 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002403 }
2404 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 }
jeffhaobdb76512011-09-07 11:43:16 -07002406 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002408 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2410 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2411 }
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 }
jeffhaobdb76512011-09-07 11:43:16 -07002414 case Instruction::IF_LTZ:
2415 case Instruction::IF_GEZ:
2416 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002418 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 if (!reg_type.IsIntegralTypes()) {
2420 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2421 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2422 }
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 }
jeffhaobdb76512011-09-07 11:43:16 -07002425 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2427 break;
jeffhaobdb76512011-09-07 11:43:16 -07002428 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2430 break;
jeffhaobdb76512011-09-07 11:43:16 -07002431 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 VerifyAGet(dec_insn, reg_types_.Char(), true);
2433 break;
jeffhaobdb76512011-09-07 11:43:16 -07002434 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 case Instruction::AGET:
2438 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2439 break;
jeffhaobdb76512011-09-07 11:43:16 -07002440 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 VerifyAGet(dec_insn, reg_types_.Long(), true);
2442 break;
2443 case Instruction::AGET_OBJECT:
2444 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
2446
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 case Instruction::APUT_BOOLEAN:
2448 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2449 break;
2450 case Instruction::APUT_BYTE:
2451 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2452 break;
2453 case Instruction::APUT_CHAR:
2454 VerifyAPut(dec_insn, reg_types_.Char(), true);
2455 break;
2456 case Instruction::APUT_SHORT:
2457 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468
jeffhaobdb76512011-09-07 11:43:16 -07002469 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002470 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 break;
jeffhaobdb76512011-09-07 11:43:16 -07002472 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002473 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 break;
jeffhaobdb76512011-09-07 11:43:16 -07002475 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002476 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 break;
jeffhaobdb76512011-09-07 11:43:16 -07002478 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002479 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 break;
2481 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002482 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002485 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002488 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 break;
jeffhaobdb76512011-09-07 11:43:16 -07002490
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002492 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 break;
2494 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002495 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 break;
2497 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002498 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 break;
2500 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002501 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002504 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002507 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 break;
jeffhaobdb76512011-09-07 11:43:16 -07002509 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002510 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512
jeffhaobdb76512011-09-07 11:43:16 -07002513 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002514 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 break;
jeffhaobdb76512011-09-07 11:43:16 -07002516 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002517 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 break;
jeffhaobdb76512011-09-07 11:43:16 -07002519 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002520 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 break;
jeffhaobdb76512011-09-07 11:43:16 -07002522 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002523 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 break;
2525 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002526 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002529 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002532 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 break;
2534
2535 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002536 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 break;
2538 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002539 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 break;
2541 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002542 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 break;
2544 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002545 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002548 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002551 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002554 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556
2557 case Instruction::INVOKE_VIRTUAL:
2558 case Instruction::INVOKE_VIRTUAL_RANGE:
2559 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002561 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
2562 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2563 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
2564 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2566 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002567 const char* descriptor;
2568 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002569 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002570 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2571 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002572 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002574 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002575 }
Ian Rogers9074b992011-10-26 17:41:55 -07002576 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002577 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002579 just_set_result = true;
2580 }
2581 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 }
jeffhaobdb76512011-09-07 11:43:16 -07002583 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002585 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2587 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002588 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 * Some additional checks when calling a constructor. We know from the invocation arg check
2590 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2591 * that to require that called_method->klass is the same as this->klass or this->super,
2592 * allowing the latter only if the "this" argument is the same as the "this" argument to
2593 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002594 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002595 bool is_constructor;
2596 if (called_method != NULL) {
2597 is_constructor = called_method->IsConstructor();
2598 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002599 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002600 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2601 const char* name = dex_file_->GetMethodName(method_id);
2602 is_constructor = strcmp(name, "<init>") == 0;
2603 }
2604 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2606 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608
2609 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 if (this_type.IsZero()) {
2611 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002614 if (called_method != NULL) {
2615 Class* this_class = this_type.GetClass();
2616 DCHECK(this_class != NULL);
2617 /* must be in same class or in superclass */
2618 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2619 if (this_class != method_->GetDeclaringClass()) {
2620 Fail(VERIFY_ERROR_GENERIC)
2621 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2622 break;
2623 }
2624 } else if (called_method->GetDeclaringClass() != this_class) {
2625 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 }
jeffhaobdb76512011-09-07 11:43:16 -07002628 }
2629
2630 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002631 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2633 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 }
2636
2637 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002638 * Replace the uninitialized reference with an initialized one. We need to do this for all
2639 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002640 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 work_line_->MarkRefsAsInitialized(this_type);
2642 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002644 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002645 const char* descriptor;
2646 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002647 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002648 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2649 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002650 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002651 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002652 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002653 }
Ian Rogers9074b992011-10-26 17:41:55 -07002654 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002655 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002657 just_set_result = true;
2658 }
2659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 }
jeffhaobdb76512011-09-07 11:43:16 -07002661 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002663 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2665 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002666 const char* descriptor;
2667 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002669 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2670 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002671 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002672 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002673 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002674 }
Ian Rogers9074b992011-10-26 17:41:55 -07002675 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002676 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 work_line_->SetResultRegisterType(return_type);
2678 just_set_result = true;
2679 }
jeffhaobdb76512011-09-07 11:43:16 -07002680 }
2681 break;
2682 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2686 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002687 if (abs_method != NULL) {
2688 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002689 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002690 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2691 << PrettyMethod(abs_method) << "'";
2692 break;
2693 }
2694 }
2695 /* Get the type of the "this" arg, which should either be a sub-interface of called
2696 * interface or Object (see comments in RegType::JoinClass).
2697 */
2698 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2699 if (failure_ == VERIFY_ERROR_NONE) {
2700 if (this_type.IsZero()) {
2701 /* null pointer always passes (and always fails at runtime) */
2702 } else {
2703 if (this_type.IsUninitializedTypes()) {
2704 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2705 << this_type;
2706 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002708 // In the past we have tried to assert that "called_interface" is assignable
2709 // from "this_type.GetClass()", however, as we do an imprecise Join
2710 // (RegType::JoinClass) we don't have full information on what interfaces are
2711 // implemented by "this_type". For example, two classes may implement the same
2712 // interfaces and have a common parent that doesn't implement the interface. The
2713 // join will set "this_type" to the parent class and a test that this implements
2714 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002715 }
2716 }
jeffhaobdb76512011-09-07 11:43:16 -07002717 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 * We don't have an object instance, so we can't find the concrete method. However, all of
2719 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002720 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002721 const char* descriptor;
2722 if (abs_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002724 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2725 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002726 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002727 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002728 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002729 }
Ian Rogers9074b992011-10-26 17:41:55 -07002730 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002731 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2732 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002734 just_set_result = true;
2735 }
2736 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 }
jeffhaobdb76512011-09-07 11:43:16 -07002738 case Instruction::NEG_INT:
2739 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::NEG_LONG:
2743 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002787 break;
2788 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
2794 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002796 break;
2797
2798 case Instruction::ADD_INT:
2799 case Instruction::SUB_INT:
2800 case Instruction::MUL_INT:
2801 case Instruction::REM_INT:
2802 case Instruction::DIV_INT:
2803 case Instruction::SHL_INT:
2804 case Instruction::SHR_INT:
2805 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808 case Instruction::AND_INT:
2809 case Instruction::OR_INT:
2810 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813 case Instruction::ADD_LONG:
2814 case Instruction::SUB_LONG:
2815 case Instruction::MUL_LONG:
2816 case Instruction::DIV_LONG:
2817 case Instruction::REM_LONG:
2818 case Instruction::AND_LONG:
2819 case Instruction::OR_LONG:
2820 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002822 break;
2823 case Instruction::SHL_LONG:
2824 case Instruction::SHR_LONG:
2825 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 /* shift distance is Int, making these different from other binary operations */
2827 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::ADD_FLOAT:
2830 case Instruction::SUB_FLOAT:
2831 case Instruction::MUL_FLOAT:
2832 case Instruction::DIV_FLOAT:
2833 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836 case Instruction::ADD_DOUBLE:
2837 case Instruction::SUB_DOUBLE:
2838 case Instruction::MUL_DOUBLE:
2839 case Instruction::DIV_DOUBLE:
2840 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843 case Instruction::ADD_INT_2ADDR:
2844 case Instruction::SUB_INT_2ADDR:
2845 case Instruction::MUL_INT_2ADDR:
2846 case Instruction::REM_INT_2ADDR:
2847 case Instruction::SHL_INT_2ADDR:
2848 case Instruction::SHR_INT_2ADDR:
2849 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::AND_INT_2ADDR:
2853 case Instruction::OR_INT_2ADDR:
2854 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002856 break;
2857 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002859 break;
2860 case Instruction::ADD_LONG_2ADDR:
2861 case Instruction::SUB_LONG_2ADDR:
2862 case Instruction::MUL_LONG_2ADDR:
2863 case Instruction::DIV_LONG_2ADDR:
2864 case Instruction::REM_LONG_2ADDR:
2865 case Instruction::AND_LONG_2ADDR:
2866 case Instruction::OR_LONG_2ADDR:
2867 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
2870 case Instruction::SHL_LONG_2ADDR:
2871 case Instruction::SHR_LONG_2ADDR:
2872 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002874 break;
2875 case Instruction::ADD_FLOAT_2ADDR:
2876 case Instruction::SUB_FLOAT_2ADDR:
2877 case Instruction::MUL_FLOAT_2ADDR:
2878 case Instruction::DIV_FLOAT_2ADDR:
2879 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882 case Instruction::ADD_DOUBLE_2ADDR:
2883 case Instruction::SUB_DOUBLE_2ADDR:
2884 case Instruction::MUL_DOUBLE_2ADDR:
2885 case Instruction::DIV_DOUBLE_2ADDR:
2886 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002888 break;
2889 case Instruction::ADD_INT_LIT16:
2890 case Instruction::RSUB_INT:
2891 case Instruction::MUL_INT_LIT16:
2892 case Instruction::DIV_INT_LIT16:
2893 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::AND_INT_LIT16:
2897 case Instruction::OR_INT_LIT16:
2898 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::ADD_INT_LIT8:
2902 case Instruction::RSUB_INT_LIT8:
2903 case Instruction::MUL_INT_LIT8:
2904 case Instruction::DIV_INT_LIT8:
2905 case Instruction::REM_INT_LIT8:
2906 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002907 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002908 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911 case Instruction::AND_INT_LIT8:
2912 case Instruction::OR_INT_LIT8:
2913 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002915 break;
2916
2917 /*
2918 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002919 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002920 * inserted in the course of verification, we can expect to see it here.
2921 */
jeffhaob4df5142011-09-19 20:25:32 -07002922 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002923 break;
2924
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002926 case Instruction::UNUSED_EE:
2927 case Instruction::UNUSED_EF:
2928 case Instruction::UNUSED_F2:
2929 case Instruction::UNUSED_F3:
2930 case Instruction::UNUSED_F4:
2931 case Instruction::UNUSED_F5:
2932 case Instruction::UNUSED_F6:
2933 case Instruction::UNUSED_F7:
2934 case Instruction::UNUSED_F8:
2935 case Instruction::UNUSED_F9:
2936 case Instruction::UNUSED_FA:
2937 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002938 case Instruction::UNUSED_F0:
2939 case Instruction::UNUSED_F1:
2940 case Instruction::UNUSED_E3:
2941 case Instruction::UNUSED_E8:
2942 case Instruction::UNUSED_E7:
2943 case Instruction::UNUSED_E4:
2944 case Instruction::UNUSED_E9:
2945 case Instruction::UNUSED_FC:
2946 case Instruction::UNUSED_E5:
2947 case Instruction::UNUSED_EA:
2948 case Instruction::UNUSED_FD:
2949 case Instruction::UNUSED_E6:
2950 case Instruction::UNUSED_EB:
2951 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002952 case Instruction::UNUSED_3E:
2953 case Instruction::UNUSED_3F:
2954 case Instruction::UNUSED_40:
2955 case Instruction::UNUSED_41:
2956 case Instruction::UNUSED_42:
2957 case Instruction::UNUSED_43:
2958 case Instruction::UNUSED_73:
2959 case Instruction::UNUSED_79:
2960 case Instruction::UNUSED_7A:
2961 case Instruction::UNUSED_EC:
2962 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002963 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
2965
2966 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002967 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002968 * complain if an instruction is missing (which is desirable).
2969 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002970 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002971
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 if (failure_ != VERIFY_ERROR_NONE) {
2973 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002974 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002975 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002976 return false;
2977 } else {
2978 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002979 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002981 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002983 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 failure_ = VERIFY_ERROR_NONE;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002985 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002986 }
2987 }
jeffhaobdb76512011-09-07 11:43:16 -07002988 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 * If we didn't just set the result register, clear it out. This ensures that you can only use
2990 * "move-result" immediately after the result is set. (We could check this statically, but it's
2991 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002992 */
2993 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002995 }
2996
jeffhaoa0a764a2011-09-16 10:43:38 -07002997 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002998 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
3000 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3001 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07003002 return false;
3003 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3005 // next instruction isn't one.
3006 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07003007 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 }
3009 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
3010 if (next_line != NULL) {
3011 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3012 // needed.
3013 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003014 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 }
jeffhaobdb76512011-09-07 11:43:16 -07003016 } else {
3017 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 * We're not recording register data for the next instruction, so we don't know what the prior
3019 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07003020 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003022 }
3023 }
3024
3025 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003026 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003027 *
3028 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003029 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003030 * somebody could get a reference field, check it for zero, and if the
3031 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003032 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003033 * that, and will reject the code.
3034 *
3035 * TODO: avoid re-fetching the branch target
3036 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003038 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003040 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003042 return false;
3043 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003044 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003045 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003046 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 }
jeffhaobdb76512011-09-07 11:43:16 -07003048 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003050 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 }
jeffhaobdb76512011-09-07 11:43:16 -07003052 }
3053
3054 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003055 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003056 *
3057 * We've already verified that the table is structurally sound, so we
3058 * just need to walk through and tag the targets.
3059 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003060 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003061 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3062 const uint16_t* switch_insns = insns + offset_to_switch;
3063 int switch_count = switch_insns[1];
3064 int offset_to_targets, targ;
3065
3066 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3067 /* 0 = sig, 1 = count, 2/3 = first key */
3068 offset_to_targets = 4;
3069 } else {
3070 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003071 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003072 offset_to_targets = 2 + 2 * switch_count;
3073 }
3074
3075 /* verify each switch target */
3076 for (targ = 0; targ < switch_count; targ++) {
3077 int offset;
3078 uint32_t abs_offset;
3079
3080 /* offsets are 32-bit, and only partly endian-swapped */
3081 offset = switch_insns[offset_to_targets + targ * 2] |
3082 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 abs_offset = work_insn_idx_ + offset;
3084 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
3085 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003086 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 }
3088 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07003089 return false;
3090 }
3091 }
3092
3093 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3095 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003096 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003099 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003100
Ian Rogers0571d352011-11-03 19:51:38 -07003101 for (; iterator.HasNext(); iterator.Next()) {
3102 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 within_catch_all = true;
3104 }
jeffhaobdb76512011-09-07 11:43:16 -07003105 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3107 * "work_regs", because at runtime the exception will be thrown before the instruction
3108 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003109 */
Ian Rogers0571d352011-11-03 19:51:38 -07003110 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003111 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 }
jeffhaobdb76512011-09-07 11:43:16 -07003113 }
3114
3115 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3117 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003118 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003120 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 * The state in work_line reflects the post-execution state. If the current instruction is a
3122 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003123 * it will do so before grabbing the lock).
3124 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003125 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 Fail(VERIFY_ERROR_GENERIC)
3127 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003128 return false;
3129 }
3130 }
3131 }
3132
jeffhaod1f0fde2011-09-08 17:25:33 -07003133 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 if(!work_line_->VerifyMonitorStackEmpty()) {
3136 return false;
3137 }
jeffhaobdb76512011-09-07 11:43:16 -07003138 }
3139
3140 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003141 * Update start_guess. Advance to the next instruction of that's
3142 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003143 * neither of those exists we're in a return or throw; leave start_guess
3144 * alone and let the caller sort it out.
3145 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003146 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003148 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003149 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003151 }
3152
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3154 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003155
3156 return true;
3157}
3158
Ian Rogers28ad40d2011-10-27 15:19:26 -07003159const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003160 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003161 Class* referrer = method_->GetDeclaringClass();
3162 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3163 const RegType& result =
3164 klass != NULL ? reg_types_.FromClass(klass)
3165 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3166 if (klass == NULL && !result.IsUnresolvedTypes()) {
3167 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 }
jeffhaod1224c72012-02-29 13:43:08 -08003169 if (result.IsUnknown()) {
3170 Fail(VERIFY_ERROR_GENERIC) << "accessing unknown class in " << PrettyDescriptor(referrer);
3171 return result;
3172 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003173 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3174 // check at runtime if access is allowed and so pass here.
3175 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3176 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003177 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003178 << result << "'";
3179 return reg_types_.Unknown();
3180 } else {
3181 return result;
3182 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003183}
3184
Ian Rogers28ad40d2011-10-27 15:19:26 -07003185const RegType& DexVerifier::GetCaughtExceptionType() {
3186 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003188 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3190 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003191 CatchHandlerIterator iterator(handlers_ptr);
3192 for (; iterator.HasNext(); iterator.Next()) {
3193 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3194 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003195 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003197 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003198 if (common_super == NULL) {
3199 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3200 // as that is caught at runtime
3201 common_super = &exception;
3202 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3203 // We don't know enough about the type and the common path merge will result in
3204 // Conflict. Fail here knowing the correct thing can be done at runtime.
Ian Rogers28ad40d2011-10-27 15:19:26 -07003205 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3206 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003207 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003208 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003210 common_super = &common_super->Merge(exception, &reg_types_);
3211 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 }
3213 }
3214 }
3215 }
Ian Rogers0571d352011-11-03 19:51:38 -07003216 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 }
3218 }
3219 if (common_super == NULL) {
3220 /* no catch blocks, or no catches with classes we can find */
3221 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003222 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003224 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003225}
3226
jeffhao8cd6dda2012-02-22 10:15:34 -08003227Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003228 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3229 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3230 if (failure_ != VERIFY_ERROR_NONE) {
3231 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3232 return NULL;
3233 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003234 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003235 return NULL; // Can't resolve Class so no more to do here
3236 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003237 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 Class* referrer = method_->GetDeclaringClass();
3239 DexCache* dex_cache = referrer->GetDexCache();
3240 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3241 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003242 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003243 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003244
3245 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003247 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 res_method = klass->FindInterfaceMethod(name, signature);
3249 } else {
3250 res_method = klass->FindVirtualMethod(name, signature);
3251 }
3252 if (res_method != NULL) {
3253 dex_cache->SetResolvedMethod(method_idx, res_method);
3254 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003255 // If a virtual or interface method wasn't found with the expected type, look in
3256 // the direct methods. This can happen when the wrong invoke type is used or when
3257 // a class has changed, and will be flagged as an error in later checks.
3258 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3259 res_method = klass->FindDirectMethod(name, signature);
3260 }
3261 if (res_method == NULL) {
3262 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3263 << PrettyDescriptor(klass) << "." << name
3264 << " " << signature;
3265 return NULL;
3266 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 }
3268 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3270 // enforce them here.
3271 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3272 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3273 << PrettyMethod(res_method);
3274 return NULL;
3275 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003276 // Disallow any calls to class initializers.
3277 if (MethodHelper(res_method).IsClassInitializer()) {
3278 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to class initializer "
3279 << PrettyMethod(res_method);
3280 return NULL;
3281 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003282 // Check if access is allowed.
3283 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3284 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3285 << " from " << PrettyDescriptor(referrer) << ")";
3286 return NULL;
3287 }
jeffhaode0d9c92012-02-27 13:58:13 -08003288 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3289 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
3290 Fail(VERIFY_ERROR_GENERIC) << "invoke-super/virtual can't be used on private method "
3291 << PrettyMethod(res_method);
3292 return NULL;
3293 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003294 // Check that interface methods match interface classes.
3295 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3296 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3297 << " is in an interface class " << PrettyClass(klass);
3298 return NULL;
3299 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3300 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3301 << " is in a non-interface class " << PrettyClass(klass);
3302 return NULL;
3303 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 // See if the method type implied by the invoke instruction matches the access flags for the
3305 // target method.
3306 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3307 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3308 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3309 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003310 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3311 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 return NULL;
3313 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003314 return res_method;
3315}
3316
Elliott Hughesadb8c672012-03-06 16:49:32 -08003317Method* DexVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08003318 MethodType method_type, bool is_range, bool is_super) {
3319 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3320 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003321 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003322 if (res_method == NULL) { // error or class is unresolved
3323 return NULL;
3324 }
3325
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3327 // has a vtable entry for the target method.
3328 if (is_super) {
3329 DCHECK(method_type == METHOD_VIRTUAL);
3330 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003331 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 if (super == NULL) { // Only Object has no super class
3333 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3334 << " to super " << PrettyMethod(res_method);
3335 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003336 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003338 << " to super " << PrettyDescriptor(super)
3339 << "." << mh.GetName()
3340 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 }
3342 return NULL;
3343 }
3344 }
3345 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3346 // match the call to the signature. Also, we might might be calling through an abstract method
3347 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08003348 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 /* caught by static verifier */
3350 DCHECK(is_range || expected_args <= 5);
3351 if (expected_args > code_item_->outs_size_) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003352 Fail(VERIFY_ERROR_GENERIC) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3354 return NULL;
3355 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003356
jeffhaobdb76512011-09-07 11:43:16 -07003357 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 * Check the "this" argument, which must be an instance of the class
3359 * that declared the method. For an interface class, we don't do the
3360 * full interface merge, so we can't do a rigorous check here (which
3361 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003362 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003363 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 if (!res_method->IsStatic()) {
3365 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3366 if (failure_ != VERIFY_ERROR_NONE) {
3367 return NULL;
3368 }
3369 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3370 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3371 return NULL;
3372 }
3373 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003374 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3375 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003376 Fail(VERIFY_ERROR_GENERIC) << "'this' argument '" << actual_arg_type
3377 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 return NULL;
3379 }
3380 }
3381 actual_args++;
3382 }
3383 /*
3384 * Process the target method's signature. This signature may or may not
3385 * have been verified, so we can't assume it's properly formed.
3386 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003387 MethodHelper mh(res_method);
3388 const DexFile::TypeList* params = mh.GetParameterTypeList();
3389 size_t params_size = params == NULL ? 0 : params->Size();
3390 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 if (actual_args >= expected_args) {
3392 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003393 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3394 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 return NULL;
3396 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003397 const char* descriptor =
3398 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3399 if (descriptor == NULL) {
3400 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3401 << " missing signature component";
3402 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 }
3404 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003405 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003406 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003407 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3408 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003409 }
3410 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3411 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003412 if (actual_args != expected_args) {
3413 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003414 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 return NULL;
3416 } else {
3417 return res_method;
3418 }
3419}
3420
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003421const RegType& DexVerifier::GetMethodReturnType() {
3422 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3423 MethodHelper(method_).GetReturnTypeDescriptor());
3424}
3425
Elliott Hughesadb8c672012-03-06 16:49:32 -08003426void DexVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08003427 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003429 if (res_type.IsUnknown()) {
3430 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3431 } else {
3432 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3433 if (!res_type.IsArrayTypes()) {
3434 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
3435 } else if (!is_filled) {
3436 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003437 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003438 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003439 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003440 } else {
3441 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3442 // the list and fail. It's legal, if silly, for arg_count to be zero.
3443 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3444 method_->GetDeclaringClass()->GetClassLoader());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003446 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003447 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003448 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3449 work_line_->SetResultRegisterType(reg_types_.Unknown());
3450 return;
3451 }
3452 }
3453 // filled-array result goes into "result" register
3454 work_line_->SetResultRegisterType(res_type);
3455 }
3456 }
3457}
3458
Elliott Hughesadb8c672012-03-06 16:49:32 -08003459void DexVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003461 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 if (!index_type.IsArrayIndexTypes()) {
3463 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3464 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003465 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003466 if (array_type.IsZero()) {
3467 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3468 // instruction type. TODO: have a proper notion of bottom here.
3469 if (!is_primitive || insn_type.IsCategory1Types()) {
3470 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08003471 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003473 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08003474 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08003475 }
jeffhaofc3144e2012-02-01 17:21:15 -08003476 } else if (!array_type.IsArrayTypes()) {
3477 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003478 } else {
3479 /* verify the class */
3480 const RegType& component_type = reg_types_.GetComponentType(array_type,
3481 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003482 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003483 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3484 << " source for aget-object";
3485 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3486 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3487 << " source for category 1 aget";
3488 } else if (is_primitive && !insn_type.Equals(component_type) &&
3489 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3490 (insn_type.IsLong() && component_type.IsDouble()))) {
3491 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003493 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003494 // Use knowledge of the field type which is stronger than the type inferred from the
3495 // instruction, which can't differentiate object types and ints from floats, longs from
3496 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003497 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 }
3499 }
3500 }
3501}
3502
Elliott Hughesadb8c672012-03-06 16:49:32 -08003503void DexVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003505 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003506 if (!index_type.IsArrayIndexTypes()) {
3507 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3508 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003509 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003510 if (array_type.IsZero()) {
3511 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3512 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003513 } else if (!array_type.IsArrayTypes()) {
3514 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003515 } else {
3516 /* verify the class */
3517 const RegType& component_type = reg_types_.GetComponentType(array_type,
3518 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003519 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003520 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3521 << " source for aput-object";
3522 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3523 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3524 << " source for category 1 aput";
3525 } else if (is_primitive && !insn_type.Equals(component_type) &&
3526 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3527 (insn_type.IsLong() && component_type.IsDouble()))) {
3528 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
3529 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003530 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003531 // The instruction agrees with the type of array, confirm the value to be stored does too
3532 // Note: we use the instruction type (rather than the component type) for aput-object as
3533 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08003534 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 }
3536 }
3537 }
3538}
3539
3540Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003541 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3542 // Check access to class
3543 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3544 if (failure_ != VERIFY_ERROR_NONE) {
3545 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3546 << dex_file_->GetFieldName(field_id) << ") in "
3547 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3548 return NULL;
3549 }
3550 if(klass_type.IsUnresolvedTypes()) {
3551 return NULL; // Can't resolve Class so no more to do here
3552 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003553 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003554 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003555 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3556 << dex_file_->GetFieldName(field_id) << ") in "
3557 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 DCHECK(Thread::Current()->IsExceptionPending());
3559 Thread::Current()->ClearException();
3560 return NULL;
3561 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3562 field->GetAccessFlags())) {
3563 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3564 << " from " << PrettyClass(method_->GetDeclaringClass());
3565 return NULL;
3566 } else if (!field->IsStatic()) {
3567 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3568 return NULL;
3569 } else {
3570 return field;
3571 }
3572}
3573
Ian Rogersd81871c2011-10-03 13:57:23 -07003574Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003575 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3576 // Check access to class
3577 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3578 if (failure_ != VERIFY_ERROR_NONE) {
3579 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3580 << dex_file_->GetFieldName(field_id) << ") in "
3581 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3582 return NULL;
3583 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003584 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003585 return NULL; // Can't resolve Class so no more to do here
3586 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003587 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003589 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3590 << dex_file_->GetFieldName(field_id) << ") in "
3591 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003592 DCHECK(Thread::Current()->IsExceptionPending());
3593 Thread::Current()->ClearException();
3594 return NULL;
3595 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3596 field->GetAccessFlags())) {
3597 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3598 << " from " << PrettyClass(method_->GetDeclaringClass());
3599 return NULL;
3600 } else if (field->IsStatic()) {
3601 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3602 << " to not be static";
3603 return NULL;
3604 } else if (obj_type.IsZero()) {
3605 // Cannot infer and check type, however, access will cause null pointer exception
3606 return field;
jeffhao8cd6dda2012-02-22 10:15:34 -08003607 } else if(obj_type.IsUninitializedTypes() &&
Ian Rogersd81871c2011-10-03 13:57:23 -07003608 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3609 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3610 // Field accesses through uninitialized references are only allowable for constructors where
3611 // the field is declared in this class
3612 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3613 << " of a not fully initialized object within the context of "
3614 << PrettyMethod(method_);
3615 return NULL;
3616 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3617 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3618 // of C1. For resolution to occur the declared class of the field must be compatible with
3619 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3620 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3621 << " from object of type " << PrettyClass(obj_type.GetClass());
3622 return NULL;
3623 } else {
3624 return field;
3625 }
3626}
3627
Elliott Hughesadb8c672012-03-06 16:49:32 -08003628void DexVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003629 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003630 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003631 Field* field;
3632 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003633 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003634 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003635 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003636 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003637 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003638 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003639 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003640 } else {
3641 const char* descriptor;
3642 const ClassLoader* loader;
3643 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003644 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003645 loader = field->GetDeclaringClass()->GetClassLoader();
3646 } else {
3647 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3648 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3649 loader = method_->GetDeclaringClass()->GetClassLoader();
3650 }
3651 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003653 if (field_type.Equals(insn_type) ||
3654 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3655 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 // expected that read is of the correct primitive type or that int reads are reading
3657 // floats or long reads are reading doubles
3658 } else {
3659 // This is a global failure rather than a class change failure as the instructions and
3660 // the descriptors for the type should have been consistent within the same file at
3661 // compile time
3662 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003663 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003664 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 return;
3666 }
3667 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003668 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003669 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003670 << " to be compatible with type '" << insn_type
3671 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003672 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003673 return;
3674 }
3675 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003676 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 }
3678}
3679
Elliott Hughesadb8c672012-03-06 16:49:32 -08003680void DexVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003681 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003682 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003683 Field* field;
3684 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003685 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003686 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003687 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003688 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003689 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003690 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003691 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogers55d249f2011-11-02 16:48:09 -07003692 } else {
3693 const char* descriptor;
3694 const ClassLoader* loader;
3695 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003696 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003697 loader = field->GetDeclaringClass()->GetClassLoader();
3698 } else {
3699 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3700 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3701 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003702 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003703 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3704 if (field != NULL) {
3705 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3706 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3707 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3708 return;
3709 }
3710 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003711 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003712 // Primitive field assignability rules are weaker than regular assignability rules
3713 bool instruction_compatible;
3714 bool value_compatible;
Elliott Hughesadb8c672012-03-06 16:49:32 -08003715 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers2c8a8572011-10-24 17:11:36 -07003716 if (field_type.IsIntegralTypes()) {
3717 instruction_compatible = insn_type.IsIntegralTypes();
3718 value_compatible = value_type.IsIntegralTypes();
3719 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003720 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003721 value_compatible = value_type.IsFloatTypes();
3722 } else if (field_type.IsLong()) {
3723 instruction_compatible = insn_type.IsLong();
3724 value_compatible = value_type.IsLongTypes();
3725 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003726 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003727 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003729 instruction_compatible = false; // reference field with primitive store
3730 value_compatible = false; // unused
3731 }
3732 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 // This is a global failure rather than a class change failure as the instructions and
3734 // the descriptors for the type should have been consistent within the same file at
3735 // compile time
3736 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003737 << " to be of type '" << insn_type
3738 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003739 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 return;
3741 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003742 if (!value_compatible) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003743 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA
Ian Rogers2c8a8572011-10-24 17:11:36 -07003744 << " of type " << value_type
3745 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003746 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003747 return;
3748 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003749 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003750 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003752 << " to be compatible with type '" << insn_type
3753 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003754 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 return;
3756 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003757 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 }
3760}
3761
3762bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3763 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3764 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3765 return false;
3766 }
3767 return true;
3768}
3769
Ian Rogersd81871c2011-10-03 13:57:23 -07003770void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003771 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003772 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003773 << " " << fail_messages_.str();
3774 return;
3775 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3777 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3778 VerifyErrorRefType ref_type;
3779 switch (inst->Opcode()) {
3780 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003781 case Instruction::CHECK_CAST:
3782 case Instruction::INSTANCE_OF:
3783 case Instruction::NEW_INSTANCE:
3784 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003786 case Instruction::FILLED_NEW_ARRAY_RANGE:
3787 ref_type = VERIFY_ERROR_REF_CLASS;
3788 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003790 case Instruction::IGET_BOOLEAN:
3791 case Instruction::IGET_BYTE:
3792 case Instruction::IGET_CHAR:
3793 case Instruction::IGET_SHORT:
3794 case Instruction::IGET_WIDE:
3795 case Instruction::IGET_OBJECT:
3796 case Instruction::IPUT:
3797 case Instruction::IPUT_BOOLEAN:
3798 case Instruction::IPUT_BYTE:
3799 case Instruction::IPUT_CHAR:
3800 case Instruction::IPUT_SHORT:
3801 case Instruction::IPUT_WIDE:
3802 case Instruction::IPUT_OBJECT:
3803 case Instruction::SGET:
3804 case Instruction::SGET_BOOLEAN:
3805 case Instruction::SGET_BYTE:
3806 case Instruction::SGET_CHAR:
3807 case Instruction::SGET_SHORT:
3808 case Instruction::SGET_WIDE:
3809 case Instruction::SGET_OBJECT:
3810 case Instruction::SPUT:
3811 case Instruction::SPUT_BOOLEAN:
3812 case Instruction::SPUT_BYTE:
3813 case Instruction::SPUT_CHAR:
3814 case Instruction::SPUT_SHORT:
3815 case Instruction::SPUT_WIDE:
3816 case Instruction::SPUT_OBJECT:
3817 ref_type = VERIFY_ERROR_REF_FIELD;
3818 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003820 case Instruction::INVOKE_VIRTUAL_RANGE:
3821 case Instruction::INVOKE_SUPER:
3822 case Instruction::INVOKE_SUPER_RANGE:
3823 case Instruction::INVOKE_DIRECT:
3824 case Instruction::INVOKE_DIRECT_RANGE:
3825 case Instruction::INVOKE_STATIC:
3826 case Instruction::INVOKE_STATIC_RANGE:
3827 case Instruction::INVOKE_INTERFACE:
3828 case Instruction::INVOKE_INTERFACE_RANGE:
3829 ref_type = VERIFY_ERROR_REF_METHOD;
3830 break;
jeffhaobdb76512011-09-07 11:43:16 -07003831 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003832 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003833 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003834 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3836 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3837 // instruction, so assert it.
3838 size_t width = inst->SizeInCodeUnits();
3839 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003840 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 // NOPs
3842 for (size_t i = 2; i < width; i++) {
3843 insns[work_insn_idx_ + i] = Instruction::NOP;
3844 }
3845 // Encode the opcode, with the failure code in the high byte
3846 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3847 (failure_ << 8) | // AA - component
3848 (ref_type << (8 + kVerifyErrorRefTypeShift));
3849 insns[work_insn_idx_] = new_instruction;
3850 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3851 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003852 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3853 << fail_messages_.str();
3854 if (gDebugVerify) {
3855 std::cout << std::endl << info_messages_.str();
3856 Dump(std::cout);
3857 }
jeffhaobdb76512011-09-07 11:43:16 -07003858}
jeffhaoba5ebb92011-08-25 17:24:37 -07003859
Ian Rogersd81871c2011-10-03 13:57:23 -07003860bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 bool changed = true;
3862 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3863 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003864 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 * We haven't processed this instruction before, and we haven't touched the registers here, so
3866 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3867 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003868 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003870 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003871 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3872 if (gDebugVerify) {
3873 copy->CopyFromLine(target_line);
3874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 changed = target_line->MergeRegisters(merge_line);
3876 if (failure_ != VERIFY_ERROR_NONE) {
3877 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003878 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003879 if (gDebugVerify && changed) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003880 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "]"
3881 " to [" <<(void*)next_insn << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003882 << *copy.get() << " MERGE" << std::endl
3883 << *merge_line << " ==" << std::endl
3884 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003885 }
3886 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 if (changed) {
3888 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003889 }
3890 return true;
3891}
3892
Ian Rogersd81871c2011-10-03 13:57:23 -07003893void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3894 size_t* log2_max_gc_pc) {
3895 size_t local_gc_points = 0;
3896 size_t max_insn = 0;
3897 size_t max_ref_reg = -1;
3898 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3899 if (insn_flags_[i].IsGcPoint()) {
3900 local_gc_points++;
3901 max_insn = i;
3902 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003903 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003904 }
3905 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 *gc_points = local_gc_points;
3907 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3908 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003909 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003910 i++;
3911 }
3912 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003913}
3914
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003915const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 size_t num_entries, ref_bitmap_bits, pc_bits;
3917 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3918 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003919 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003920 // TODO: either a better GC map format or per method failures
3921 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3922 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003923 return NULL;
3924 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3926 // There are 2 bytes to encode the number of entries
3927 if (num_entries >= 65536) {
3928 // TODO: either a better GC map format or per method failures
3929 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3930 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003931 return NULL;
3932 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003933 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003934 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003935 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003936 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003937 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003938 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003939 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003940 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003941 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003942 // TODO: either a better GC map format or per method failures
3943 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3944 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3945 return NULL;
3946 }
3947 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003948 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003949 if (table == NULL) {
3950 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3951 return NULL;
3952 }
3953 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003954 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3955 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003956 table->push_back(num_entries & 0xFF);
3957 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003958 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003959 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3960 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003961 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003962 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003963 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003964 }
3965 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003966 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003967 }
3968 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003969 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003970 return table;
3971}
jeffhaoa0a764a2011-09-16 10:43:38 -07003972
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003973void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3975 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003976 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003977 size_t map_index = 0;
3978 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3979 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3980 if (insn_flags_[i].IsGcPoint()) {
3981 CHECK_LT(map_index, map.NumEntries());
3982 CHECK_EQ(map.GetPC(map_index), i);
3983 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3984 map_index++;
3985 RegisterLine* line = reg_table_.GetLine(i);
3986 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003987 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003988 CHECK_LT(j / 8, map.RegWidth());
3989 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3990 } else if ((j / 8) < map.RegWidth()) {
3991 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3992 } else {
3993 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3994 }
3995 }
3996 } else {
3997 CHECK(reg_bitmap == NULL);
3998 }
3999 }
4000}
jeffhaoa0a764a2011-09-16 10:43:38 -07004001
Ian Rogersd81871c2011-10-03 13:57:23 -07004002const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
4003 size_t num_entries = NumEntries();
4004 // Do linear or binary search?
4005 static const size_t kSearchThreshold = 8;
4006 if (num_entries < kSearchThreshold) {
4007 for (size_t i = 0; i < num_entries; i++) {
4008 if (GetPC(i) == dex_pc) {
4009 return GetBitMap(i);
4010 }
4011 }
4012 } else {
4013 int lo = 0;
4014 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07004015 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004016 int mid = (hi + lo) / 2;
4017 int mid_pc = GetPC(mid);
4018 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004019 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004021 hi = mid - 1;
4022 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07004024 }
4025 }
4026 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004027 if (error_if_not_present) {
4028 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
4029 }
jeffhaoa0a764a2011-09-16 10:43:38 -07004030 return NULL;
4031}
4032
Elliott Hughesd9c67be2012-02-02 19:54:06 -08004033Mutex DexVerifier::gc_maps_lock_("verifier gc maps lock");
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004034DexVerifier::GcMapTable DexVerifier::gc_maps_;
4035
4036void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08004037 MutexLock mu(gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004038 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
4039 if (existing_gc_map != NULL) {
4040 CHECK(*existing_gc_map == gc_map);
4041 delete existing_gc_map;
4042 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004043 gc_maps_[ref] = &gc_map;
4044 CHECK(GetGcMap(ref) != NULL);
4045}
4046
4047const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08004048 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004049 GcMapTable::const_iterator it = gc_maps_.find(ref);
4050 if (it == gc_maps_.end()) {
4051 return NULL;
4052 }
4053 CHECK(it->second != NULL);
4054 return it->second;
4055}
4056
4057void DexVerifier::DeleteGcMaps() {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08004058 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004059 STLDeleteValues(&gc_maps_);
4060}
4061
jeffhaod1224c72012-02-29 13:43:08 -08004062Mutex DexVerifier::rejected_classes_lock_("verifier rejected classes lock");
4063std::set<Compiler::ClassReference> DexVerifier::rejected_classes_;
4064
4065void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) {
4066 MutexLock mu(rejected_classes_lock_);
4067 rejected_classes_.insert(ref);
4068 CHECK(IsClassRejected(ref));
4069}
4070
4071bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) {
4072 MutexLock mu(rejected_classes_lock_);
4073 return (rejected_classes_.find(ref) != rejected_classes_.end());
4074}
4075
Logan Chienfca7e872011-12-20 20:08:22 +08004076#if defined(ART_USE_LLVM_COMPILER)
4077InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
4078 uint32_t insns_size = code_item_->insns_size_in_code_units_;
4079 uint16_t regs_size = code_item_->registers_size_;
4080
4081 UniquePtr<InferredRegCategoryMap> table(
4082 new InferredRegCategoryMap(insns_size, regs_size));
4083
4084 for (size_t i = 0; i < insns_size; ++i) {
4085 if (RegisterLine* line = reg_table_.GetLine(i)) {
4086 for (size_t r = 0; r < regs_size; ++r) {
4087 RegType const &rt = line->GetRegisterType(r);
4088
4089 if (rt.IsZero()) {
4090 table->SetRegCategory(i, r, kRegZero);
4091 } else if (rt.IsCategory1Types()) {
4092 table->SetRegCategory(i, r, kRegCat1nr);
4093 } else if (rt.IsCategory2Types()) {
4094 table->SetRegCategory(i, r, kRegCat2);
4095 } else if (rt.IsReferenceTypes()) {
4096 table->SetRegCategory(i, r, kRegObject);
4097 } else {
4098 table->SetRegCategory(i, r, kRegUnknown);
4099 }
4100 }
4101 }
4102 }
4103
4104 return table.release();
4105}
4106#endif
4107
Ian Rogersd81871c2011-10-03 13:57:23 -07004108} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004109} // namespace art