blob: b68c7d570cd879b5fd2c43ae324412baa1e03d01 [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
626const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
627 if (dec_insn.vA_ < 1) {
628 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 */
632 const RegType& this_type = GetRegisterType(dec_insn.vC_);
633 if (!this_type.IsReferenceTypes()) {
634 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
635 << dec_insn.vC_ << " (type=" << this_type << ")";
636 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
744void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
745 const RegType& dst_type, const RegType& src_type) {
746 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
747 SetRegisterType(dec_insn.vA_, dst_type);
748 }
749}
750
751void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
752 const RegType& dst_type,
753 const RegType& src_type1, const RegType& src_type2,
754 bool check_boolean_op) {
755 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
756 VerifyRegisterType(dec_insn.vC_, src_type2)) {
757 if (check_boolean_op) {
758 DCHECK(dst_type.IsInteger());
759 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
760 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
761 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
762 return;
763 }
764 }
765 SetRegisterType(dec_insn.vA_, dst_type);
766 }
767}
768
769void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
770 const RegType& dst_type, const RegType& src_type1,
771 const RegType& src_type2, bool check_boolean_op) {
772 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
773 VerifyRegisterType(dec_insn.vB_, src_type2)) {
774 if (check_boolean_op) {
775 DCHECK(dst_type.IsInteger());
776 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
777 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
778 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
779 return;
780 }
781 }
782 SetRegisterType(dec_insn.vA_, dst_type);
783 }
784}
785
786void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
787 const RegType& dst_type, const RegType& src_type,
788 bool check_boolean_op) {
789 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
790 if (check_boolean_op) {
791 DCHECK(dst_type.IsInteger());
792 /* check vB with the call, then check the constant manually */
793 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
794 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
795 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
796 return;
797 }
798 }
799 SetRegisterType(dec_insn.vA_, dst_type);
800 }
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);
971 bool success = verifier.Verify();
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);
995 verifier.Verify();
996
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
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001002DexVerifier::DexVerifier(Method* method)
1003 : work_insn_idx_(-1),
1004 method_(method),
1005 failure_(VERIFY_ERROR_NONE),
1006 new_instance_count_(0),
1007 monitor_enter_count_(0) {
1008 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001009 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001010 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 dex_file_ = &class_linker->FindDexFile(dex_cache);
1012 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001013}
1014
Ian Rogersd81871c2011-10-03 13:57:23 -07001015bool DexVerifier::Verify() {
1016 // If there aren't any instructions, make sure that's expected, then exit successfully.
1017 if (code_item_ == NULL) {
1018 if (!method_->IsNative() && !method_->IsAbstract()) {
1019 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001020 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 } else {
1022 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001023 }
jeffhaobdb76512011-09-07 11:43:16 -07001024 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1026 if (code_item_->ins_size_ > code_item_->registers_size_) {
1027 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1028 << " regs=" << code_item_->registers_size_;
1029 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001030 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 // Allocate and initialize an array to hold instruction data.
1032 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1033 // Run through the instructions and see if the width checks out.
1034 bool result = ComputeWidthsAndCountOps();
1035 // Flag instructions guarded by a "try" block and check exception handlers.
1036 result = result && ScanTryCatchBlocks();
1037 // Perform static instruction verification.
1038 result = result && VerifyInstructions();
1039 // Perform code flow analysis.
1040 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001041 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001042}
1043
Ian Rogers47a05882012-02-03 12:23:33 -08001044std::ostream& DexVerifier::Fail(VerifyError error) {
1045 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers47a05882012-02-03 12:23:33 -08001046 if (Runtime::Current()->IsCompiler()) {
jeffhaod1224c72012-02-29 13:43:08 -08001047 switch (error) {
1048 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
1049 // errors into generic errors so that we re-verify at runtime. We may fail to find or to agree
1050 // on access because of not yet available class loaders, or class loaders that will differ at
1051 // runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001052 case VERIFY_ERROR_NO_CLASS:
1053 case VERIFY_ERROR_NO_FIELD:
1054 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -08001055 case VERIFY_ERROR_ACCESS_CLASS:
1056 case VERIFY_ERROR_ACCESS_FIELD:
1057 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers47a05882012-02-03 12:23:33 -08001058 error = VERIFY_ERROR_GENERIC;
1059 break;
jeffhaod1224c72012-02-29 13:43:08 -08001060 // Generic failures at compile time will still fail at runtime, so the class is marked as
1061 // rejected to prevent it from being compiled.
1062 case VERIFY_ERROR_GENERIC: {
1063 const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef();
1064 CHECK(class_def != NULL);
1065 Compiler::ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def));
1066 AddRejectedClass(ref);
1067 break;
1068 }
Ian Rogers47a05882012-02-03 12:23:33 -08001069 default:
1070 break;
1071 }
1072 }
1073 failure_ = error;
1074 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1075 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1076}
1077
Ian Rogersd81871c2011-10-03 13:57:23 -07001078bool DexVerifier::ComputeWidthsAndCountOps() {
1079 const uint16_t* insns = code_item_->insns_;
1080 size_t insns_size = code_item_->insns_size_in_code_units_;
1081 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001082 size_t new_instance_count = 0;
1083 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001085
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001087 Instruction::Code opcode = inst->Opcode();
1088 if (opcode == Instruction::NEW_INSTANCE) {
1089 new_instance_count++;
1090 } else if (opcode == Instruction::MONITOR_ENTER) {
1091 monitor_enter_count++;
1092 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 size_t inst_size = inst->SizeInCodeUnits();
1094 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1095 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001096 inst = inst->Next();
1097 }
1098
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 if (dex_pc != insns_size) {
1100 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1101 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001102 return false;
1103 }
1104
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 new_instance_count_ = new_instance_count;
1106 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001107 return true;
1108}
1109
Ian Rogersd81871c2011-10-03 13:57:23 -07001110bool DexVerifier::ScanTryCatchBlocks() {
1111 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001112 if (tries_size == 0) {
1113 return true;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001116 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001117
1118 for (uint32_t idx = 0; idx < tries_size; idx++) {
1119 const DexFile::TryItem* try_item = &tries[idx];
1120 uint32_t start = try_item->start_addr_;
1121 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001122 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1124 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001125 return false;
1126 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 if (!insn_flags_[start].IsOpcode()) {
1128 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001129 return false;
1130 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 for (uint32_t dex_pc = start; dex_pc < end;
1132 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1133 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001134 }
1135 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001136 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001137 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001138 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001139 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001140 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001141 CatchHandlerIterator iterator(handlers_ptr);
1142 for (; iterator.HasNext(); iterator.Next()) {
1143 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 if (!insn_flags_[dex_pc].IsOpcode()) {
1145 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001146 return false;
1147 }
jeffhao60f83e32012-02-13 17:16:30 -08001148 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1149 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
1150 Fail(VERIFY_ERROR_GENERIC) << "exception handler doesn't start with move-exception ("
1151 << dex_pc << ")";
1152 return false;
1153 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001155 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1156 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001157 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1158 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001159 if (exception_type == NULL) {
1160 DCHECK(Thread::Current()->IsExceptionPending());
1161 Thread::Current()->ClearException();
1162 }
1163 }
jeffhaobdb76512011-09-07 11:43:16 -07001164 }
Ian Rogers0571d352011-11-03 19:51:38 -07001165 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001166 }
jeffhaobdb76512011-09-07 11:43:16 -07001167 return true;
1168}
1169
Ian Rogersd81871c2011-10-03 13:57:23 -07001170bool DexVerifier::VerifyInstructions() {
1171 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001172
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 /* Flag the start of the method as a branch target. */
1174 insn_flags_[0].SetBranchTarget();
1175
1176 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1177 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1178 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001179 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
jeffhaod1224c72012-02-29 13:43:08 -08001180 fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
1183 /* Flag instructions that are garbage collection points */
1184 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1185 insn_flags_[dex_pc].SetGcPoint();
1186 }
1187 dex_pc += inst->SizeInCodeUnits();
1188 inst = inst->Next();
1189 }
1190 return true;
1191}
1192
1193bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1194 Instruction::DecodedInstruction dec_insn(inst);
1195 bool result = true;
1196 switch (inst->GetVerifyTypeArgumentA()) {
1197 case Instruction::kVerifyRegA:
1198 result = result && CheckRegisterIndex(dec_insn.vA_);
1199 break;
1200 case Instruction::kVerifyRegAWide:
1201 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1202 break;
1203 }
1204 switch (inst->GetVerifyTypeArgumentB()) {
1205 case Instruction::kVerifyRegB:
1206 result = result && CheckRegisterIndex(dec_insn.vB_);
1207 break;
1208 case Instruction::kVerifyRegBField:
1209 result = result && CheckFieldIndex(dec_insn.vB_);
1210 break;
1211 case Instruction::kVerifyRegBMethod:
1212 result = result && CheckMethodIndex(dec_insn.vB_);
1213 break;
1214 case Instruction::kVerifyRegBNewInstance:
1215 result = result && CheckNewInstance(dec_insn.vB_);
1216 break;
1217 case Instruction::kVerifyRegBString:
1218 result = result && CheckStringIndex(dec_insn.vB_);
1219 break;
1220 case Instruction::kVerifyRegBType:
1221 result = result && CheckTypeIndex(dec_insn.vB_);
1222 break;
1223 case Instruction::kVerifyRegBWide:
1224 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1225 break;
1226 }
1227 switch (inst->GetVerifyTypeArgumentC()) {
1228 case Instruction::kVerifyRegC:
1229 result = result && CheckRegisterIndex(dec_insn.vC_);
1230 break;
1231 case Instruction::kVerifyRegCField:
1232 result = result && CheckFieldIndex(dec_insn.vC_);
1233 break;
1234 case Instruction::kVerifyRegCNewArray:
1235 result = result && CheckNewArray(dec_insn.vC_);
1236 break;
1237 case Instruction::kVerifyRegCType:
1238 result = result && CheckTypeIndex(dec_insn.vC_);
1239 break;
1240 case Instruction::kVerifyRegCWide:
1241 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1242 break;
1243 }
1244 switch (inst->GetVerifyExtraFlags()) {
1245 case Instruction::kVerifyArrayData:
1246 result = result && CheckArrayData(code_offset);
1247 break;
1248 case Instruction::kVerifyBranchTarget:
1249 result = result && CheckBranchTarget(code_offset);
1250 break;
1251 case Instruction::kVerifySwitchTargets:
1252 result = result && CheckSwitchTargets(code_offset);
1253 break;
1254 case Instruction::kVerifyVarArg:
1255 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1256 break;
1257 case Instruction::kVerifyVarArgRange:
1258 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1259 break;
1260 case Instruction::kVerifyError:
1261 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1262 result = false;
1263 break;
1264 }
1265 return result;
1266}
1267
1268bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1269 if (idx >= code_item_->registers_size_) {
1270 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1271 << code_item_->registers_size_ << ")";
1272 return false;
1273 }
1274 return true;
1275}
1276
1277bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1278 if (idx + 1 >= code_item_->registers_size_) {
1279 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1280 << "+1 >= " << code_item_->registers_size_ << ")";
1281 return false;
1282 }
1283 return true;
1284}
1285
1286bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1287 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1288 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1289 << dex_file_->GetHeader().field_ids_size_ << ")";
1290 return false;
1291 }
1292 return true;
1293}
1294
1295bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1296 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1297 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1298 << dex_file_->GetHeader().method_ids_size_ << ")";
1299 return false;
1300 }
1301 return true;
1302}
1303
1304bool DexVerifier::CheckNewInstance(uint32_t idx) {
1305 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1306 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1307 << dex_file_->GetHeader().type_ids_size_ << ")";
1308 return false;
1309 }
1310 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001311 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 if (descriptor[0] != 'L') {
1313 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1314 return false;
1315 }
1316 return true;
1317}
1318
1319bool DexVerifier::CheckStringIndex(uint32_t idx) {
1320 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1321 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1322 << dex_file_->GetHeader().string_ids_size_ << ")";
1323 return false;
1324 }
1325 return true;
1326}
1327
1328bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1329 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1330 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1331 << dex_file_->GetHeader().type_ids_size_ << ")";
1332 return false;
1333 }
1334 return true;
1335}
1336
1337bool DexVerifier::CheckNewArray(uint32_t idx) {
1338 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1339 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1340 << dex_file_->GetHeader().type_ids_size_ << ")";
1341 return false;
1342 }
1343 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001344 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 const char* cp = descriptor;
1346 while (*cp++ == '[') {
1347 bracket_count++;
1348 }
1349 if (bracket_count == 0) {
1350 /* The given class must be an array type. */
1351 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1352 return false;
1353 } else if (bracket_count > 255) {
1354 /* It is illegal to create an array of more than 255 dimensions. */
1355 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1356 return false;
1357 }
1358 return true;
1359}
1360
1361bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1362 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1363 const uint16_t* insns = code_item_->insns_ + cur_offset;
1364 const uint16_t* array_data;
1365 int32_t array_data_offset;
1366
1367 DCHECK_LT(cur_offset, insn_count);
1368 /* make sure the start of the array data table is in range */
1369 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1370 if ((int32_t) cur_offset + array_data_offset < 0 ||
1371 cur_offset + array_data_offset + 2 >= insn_count) {
1372 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1373 << ", data offset " << array_data_offset << ", count " << insn_count;
1374 return false;
1375 }
1376 /* offset to array data table is a relative branch-style offset */
1377 array_data = insns + array_data_offset;
1378 /* make sure the table is 32-bit aligned */
1379 if ((((uint32_t) array_data) & 0x03) != 0) {
1380 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1381 << ", data offset " << array_data_offset;
1382 return false;
1383 }
1384 uint32_t value_width = array_data[1];
1385 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1386 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1387 /* make sure the end of the switch is in range */
1388 if (cur_offset + array_data_offset + table_size > insn_count) {
1389 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1390 << ", data offset " << array_data_offset << ", end "
1391 << cur_offset + array_data_offset + table_size
1392 << ", count " << insn_count;
1393 return false;
1394 }
1395 return true;
1396}
1397
1398bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1399 int32_t offset;
1400 bool isConditional, selfOkay;
1401 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1402 return false;
1403 }
1404 if (!selfOkay && offset == 0) {
1405 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1406 return false;
1407 }
1408 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1409 // identical "wrap-around" behavior, but it's unwise to depend on that.
1410 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1411 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1412 return false;
1413 }
1414 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1415 int32_t abs_offset = cur_offset + offset;
1416 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1417 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1418 << (void*) abs_offset << ") at " << (void*) cur_offset;
1419 return false;
1420 }
1421 insn_flags_[abs_offset].SetBranchTarget();
1422 return true;
1423}
1424
1425bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1426 bool* selfOkay) {
1427 const uint16_t* insns = code_item_->insns_ + cur_offset;
1428 *pConditional = false;
1429 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 switch (*insns & 0xff) {
1431 case Instruction::GOTO:
1432 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 break;
1434 case Instruction::GOTO_32:
1435 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001436 *selfOkay = true;
1437 break;
1438 case Instruction::GOTO_16:
1439 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001440 break;
1441 case Instruction::IF_EQ:
1442 case Instruction::IF_NE:
1443 case Instruction::IF_LT:
1444 case Instruction::IF_GE:
1445 case Instruction::IF_GT:
1446 case Instruction::IF_LE:
1447 case Instruction::IF_EQZ:
1448 case Instruction::IF_NEZ:
1449 case Instruction::IF_LTZ:
1450 case Instruction::IF_GEZ:
1451 case Instruction::IF_GTZ:
1452 case Instruction::IF_LEZ:
1453 *pOffset = (int16_t) insns[1];
1454 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 break;
1456 default:
1457 return false;
1458 break;
1459 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 return true;
1461}
1462
Ian Rogersd81871c2011-10-03 13:57:23 -07001463bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1464 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001465 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1469 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1470 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1471 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001472 return false;
1473 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001474 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 /* make sure the table is 32-bit aligned */
1477 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1479 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 return false;
1481 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 uint32_t switch_count = switch_insns[1];
1483 int32_t keys_offset, targets_offset;
1484 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1486 /* 0=sig, 1=count, 2/3=firstKey */
1487 targets_offset = 4;
1488 keys_offset = -1;
1489 expected_signature = Instruction::kPackedSwitchSignature;
1490 } else {
1491 /* 0=sig, 1=count, 2..count*2 = keys */
1492 keys_offset = 2;
1493 targets_offset = 2 + 2 * switch_count;
1494 expected_signature = Instruction::kSparseSwitchSignature;
1495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001497 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001498 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1499 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001500 return false;
1501 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001502 /* make sure the end of the switch is in range */
1503 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1505 << switch_offset << ", end "
1506 << (cur_offset + switch_offset + table_size)
1507 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 return false;
1509 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001510 /* for a sparse switch, verify the keys are in ascending order */
1511 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1513 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001514 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1515 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1516 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1518 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001519 return false;
1520 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001521 last_key = key;
1522 }
1523 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 for (uint32_t targ = 0; targ < switch_count; targ++) {
1526 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1527 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1528 int32_t abs_offset = cur_offset + offset;
1529 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1530 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1531 << (void*) abs_offset << ") at "
1532 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 return false;
1534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 insn_flags_[abs_offset].SetBranchTarget();
1536 }
1537 return true;
1538}
1539
1540bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1541 if (vA > 5) {
1542 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1543 return false;
1544 }
1545 uint16_t registers_size = code_item_->registers_size_;
1546 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001547 if (arg[idx] >= registers_size) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
jeffhao457cc512012-02-02 16:55:13 -08001549 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 return false;
1551 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001552 }
1553
1554 return true;
1555}
1556
Ian Rogersd81871c2011-10-03 13:57:23 -07001557bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1558 uint16_t registers_size = code_item_->registers_size_;
1559 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1560 // integer overflow when adding them here.
1561 if (vA + vC > registers_size) {
1562 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1563 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 return false;
1565 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001566 return true;
1567}
1568
Brian Carlstrom75412882012-01-18 01:26:54 -08001569const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1570 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1571 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1572 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1573 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1574 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1575 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1576 gc_map.begin(),
1577 gc_map.end());
1578 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1579 DCHECK_EQ(gc_map.size(),
1580 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1581 (length_prefixed_gc_map->at(1) << 16) |
1582 (length_prefixed_gc_map->at(2) << 8) |
1583 (length_prefixed_gc_map->at(3) << 0)));
1584 return length_prefixed_gc_map;
1585}
1586
Ian Rogersd81871c2011-10-03 13:57:23 -07001587bool DexVerifier::VerifyCodeFlow() {
1588 uint16_t registers_size = code_item_->registers_size_;
1589 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001590
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001592 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1593 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 }
1595 /* Create and initialize table holding register status */
1596 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1597 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001598
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 work_line_.reset(new RegisterLine(registers_size, this));
1600 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001601
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 /* Initialize register types of method arguments. */
1603 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001604 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1605 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 return false;
1607 }
1608 /* Perform code flow verification. */
1609 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001610 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001612 }
1613
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001615 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1616 if (map.get() == NULL) {
1617 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 return false; // Not a real failure, but a failure to encode
1619 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001620#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001621 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001622#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001623 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1624 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1625 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001626
1627#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom75412882012-01-18 01:26:54 -08001628 method_->SetGcMap(&gc_map->at(0));
Logan Chienfca7e872011-12-20 20:08:22 +08001629#else
1630 /* Generate Inferred Register Category for LLVM-based Code Generator */
1631 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
1632 method_->SetInferredRegCategoryMap(table);
1633#endif
1634
jeffhaobdb76512011-09-07 11:43:16 -07001635 return true;
1636}
1637
Ian Rogersd81871c2011-10-03 13:57:23 -07001638void DexVerifier::Dump(std::ostream& os) {
1639 if (method_->IsNative()) {
1640 os << "Native method" << std::endl;
1641 return;
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 DCHECK(code_item_ != NULL);
1644 const Instruction* inst = Instruction::At(code_item_->insns_);
1645 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1646 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001647 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001648 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1650 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001651 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001652 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001653 inst = inst->Next();
1654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655}
1656
Ian Rogersd81871c2011-10-03 13:57:23 -07001657static bool IsPrimitiveDescriptor(char descriptor) {
1658 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001659 case 'I':
1660 case 'C':
1661 case 'S':
1662 case 'B':
1663 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001664 case 'F':
1665 case 'D':
1666 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001668 default:
1669 return false;
1670 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001671}
1672
Ian Rogersd81871c2011-10-03 13:57:23 -07001673bool DexVerifier::SetTypesFromSignature() {
1674 RegisterLine* reg_line = reg_table_.GetLine(0);
1675 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1676 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001677
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1679 //Include the "this" pointer.
1680 size_t cur_arg = 0;
1681 if (!method_->IsStatic()) {
1682 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1683 // argument as uninitialized. This restricts field access until the superclass constructor is
1684 // called.
1685 Class* declaring_class = method_->GetDeclaringClass();
1686 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1687 reg_line->SetRegisterType(arg_start + cur_arg,
1688 reg_types_.UninitializedThisArgument(declaring_class));
1689 } else {
1690 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001693 }
1694
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001695 const DexFile::ProtoId& proto_id =
1696 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001697 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001698
1699 for (; iterator.HasNext(); iterator.Next()) {
1700 const char* descriptor = iterator.GetDescriptor();
1701 if (descriptor == NULL) {
1702 LOG(FATAL) << "Null descriptor";
1703 }
1704 if (cur_arg >= expected_args) {
1705 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1706 << " args, found more (" << descriptor << ")";
1707 return false;
1708 }
1709 switch (descriptor[0]) {
1710 case 'L':
1711 case '[':
1712 // We assume that reference arguments are initialized. The only way it could be otherwise
1713 // (assuming the caller was verified) is if the current method is <init>, but in that case
1714 // it's effectively considered initialized the instant we reach here (in the sense that we
1715 // can return without doing anything or call virtual methods).
1716 {
1717 const RegType& reg_type =
1718 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001719 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 }
1721 break;
1722 case 'Z':
1723 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1724 break;
1725 case 'C':
1726 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1727 break;
1728 case 'B':
1729 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1730 break;
1731 case 'I':
1732 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1733 break;
1734 case 'S':
1735 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1736 break;
1737 case 'F':
1738 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1739 break;
1740 case 'J':
1741 case 'D': {
1742 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1743 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1744 cur_arg++;
1745 break;
1746 }
1747 default:
1748 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1749 return false;
1750 }
1751 cur_arg++;
1752 }
1753 if (cur_arg != expected_args) {
1754 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1755 return false;
1756 }
1757 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1758 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1759 // format. Only major difference from the method argument format is that 'V' is supported.
1760 bool result;
1761 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1762 result = descriptor[1] == '\0';
1763 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1764 size_t i = 0;
1765 do {
1766 i++;
1767 } while (descriptor[i] == '['); // process leading [
1768 if (descriptor[i] == 'L') { // object array
1769 do {
1770 i++; // find closing ;
1771 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1772 result = descriptor[i] == ';';
1773 } else { // primitive array
1774 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1775 }
1776 } else if (descriptor[0] == 'L') {
1777 // could be more thorough here, but shouldn't be required
1778 size_t i = 0;
1779 do {
1780 i++;
1781 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1782 result = descriptor[i] == ';';
1783 } else {
1784 result = false;
1785 }
1786 if (!result) {
1787 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1788 << descriptor << "'";
1789 }
1790 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001791}
1792
Ian Rogersd81871c2011-10-03 13:57:23 -07001793bool DexVerifier::CodeFlowVerifyMethod() {
1794 const uint16_t* insns = code_item_->insns_;
1795 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001796
jeffhaobdb76512011-09-07 11:43:16 -07001797 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 insn_flags_[0].SetChanged();
1799 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001800
jeffhaobdb76512011-09-07 11:43:16 -07001801 /* Continue until no instructions are marked "changed". */
1802 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1804 uint32_t insn_idx = start_guess;
1805 for (; insn_idx < insns_size; insn_idx++) {
1806 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808 }
jeffhaobdb76512011-09-07 11:43:16 -07001809 if (insn_idx == insns_size) {
1810 if (start_guess != 0) {
1811 /* try again, starting from the top */
1812 start_guess = 0;
1813 continue;
1814 } else {
1815 /* all flags are clear */
1816 break;
1817 }
1818 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 // We carry the working set of registers from instruction to instruction. If this address can
1820 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1821 // "changed" flags, we need to load the set of registers from the table.
1822 // Because we always prefer to continue on to the next instruction, we should never have a
1823 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1824 // target.
1825 work_insn_idx_ = insn_idx;
1826 if (insn_flags_[insn_idx].IsBranchTarget()) {
1827 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001828 } else {
1829#ifndef NDEBUG
1830 /*
1831 * Sanity check: retrieve the stored register line (assuming
1832 * a full table) and make sure it actually matches.
1833 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1835 if (register_line != NULL) {
1836 if (work_line_->CompareLine(register_line) != 0) {
1837 Dump(std::cout);
1838 std::cout << info_messages_.str();
1839 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1840 << "@" << (void*)work_insn_idx_ << std::endl
1841 << " work_line=" << *work_line_ << std::endl
1842 << " expected=" << *register_line;
1843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 }
1845#endif
1846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 if (!CodeFlowVerifyInstruction(&start_guess)) {
1848 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001849 return false;
1850 }
jeffhaobdb76512011-09-07 11:43:16 -07001851 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 insn_flags_[insn_idx].SetVisited();
1853 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001855
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001857 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001858 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001859 * (besides the wasted space), but it indicates a flaw somewhere
1860 * down the line, possibly in the verifier.
1861 *
1862 * If we've substituted "always throw" instructions into the stream,
1863 * we are almost certainly going to have some dead code.
1864 */
1865 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 uint32_t insn_idx = 0;
1867 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001868 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001869 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001870 * may or may not be preceded by a padding NOP (for alignment).
1871 */
1872 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1873 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1874 insns[insn_idx] == Instruction::kArrayDataSignature ||
1875 (insns[insn_idx] == Instruction::NOP &&
1876 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1877 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1878 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001880 }
1881
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001883 if (dead_start < 0)
1884 dead_start = insn_idx;
1885 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001887 dead_start = -1;
1888 }
1889 }
1890 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001892 }
1893 }
jeffhaobdb76512011-09-07 11:43:16 -07001894 return true;
1895}
1896
Ian Rogersd81871c2011-10-03 13:57:23 -07001897bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001898#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001900 gDvm.verifierStats.instrsReexamined++;
1901 } else {
1902 gDvm.verifierStats.instrsExamined++;
1903 }
1904#endif
1905
1906 /*
1907 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001908 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001909 * control to another statement:
1910 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001911 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001912 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001913 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001914 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001915 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001916 * throw an exception that is handled by an encompassing "try"
1917 * block.
1918 *
1919 * We can also return, in which case there is no successor instruction
1920 * from this point.
1921 *
1922 * The behavior can be determined from the OpcodeFlags.
1923 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1925 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001926 Instruction::DecodedInstruction dec_insn(inst);
1927 int opcode_flag = inst->Flag();
1928
jeffhaobdb76512011-09-07 11:43:16 -07001929 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001930 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001931 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001933 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1934 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 }
jeffhaobdb76512011-09-07 11:43:16 -07001936
1937 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001938 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001939 * can throw an exception, we will copy/merge this into the "catch"
1940 * address rather than work_line, because we don't want the result
1941 * from the "successful" code path (e.g. a check-cast that "improves"
1942 * a type) to be visible to the exception handler.
1943 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1945 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001946 } else {
1947#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001949#endif
1950 }
1951
1952 switch (dec_insn.opcode_) {
1953 case Instruction::NOP:
1954 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001955 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001956 * a signature that looks like a NOP; if we see one of these in
1957 * the course of executing code then we have a problem.
1958 */
1959 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001961 }
1962 break;
1963
1964 case Instruction::MOVE:
1965 case Instruction::MOVE_FROM16:
1966 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001968 break;
1969 case Instruction::MOVE_WIDE:
1970 case Instruction::MOVE_WIDE_FROM16:
1971 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974 case Instruction::MOVE_OBJECT:
1975 case Instruction::MOVE_OBJECT_FROM16:
1976 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001978 break;
1979
1980 /*
1981 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001982 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001983 * might want to hold the result in an actual CPU register, so the
1984 * Dalvik spec requires that these only appear immediately after an
1985 * invoke or filled-new-array.
1986 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001987 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001988 * redundant with the reset done below, but it can make the debug info
1989 * easier to read in some cases.)
1990 */
1991 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
1994 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001996 break;
1997 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001999 break;
2000
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07002002 /*
jeffhao60f83e32012-02-13 17:16:30 -08002003 * This statement can only appear as the first instruction in an exception handler. We verify
2004 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002005 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002006 const RegType& res_type = GetCaughtExceptionType();
2007 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002008 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 }
jeffhaobdb76512011-09-07 11:43:16 -07002010 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2012 if (!GetMethodReturnType().IsUnknown()) {
2013 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
2014 }
jeffhaobdb76512011-09-07 11:43:16 -07002015 }
2016 break;
2017 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002019 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 const RegType& return_type = GetMethodReturnType();
2021 if (!return_type.IsCategory1Types()) {
2022 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
2023 } else {
2024 // Compilers may generate synthetic functions that write byte values into boolean fields.
2025 // Also, it may use integer values for boolean, byte, short, and character return types.
2026 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
2027 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2028 ((return_type.IsBoolean() || return_type.IsByte() ||
2029 return_type.IsShort() || return_type.IsChar()) &&
2030 src_type.IsInteger()));
2031 /* check the register contents */
2032 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
2033 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002034 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 }
jeffhaobdb76512011-09-07 11:43:16 -07002036 }
2037 }
2038 break;
2039 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002041 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 const RegType& return_type = GetMethodReturnType();
2043 if (!return_type.IsCategory2Types()) {
2044 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
2045 } else {
2046 /* check the register contents */
2047 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
2048 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002049 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 }
jeffhaobdb76512011-09-07 11:43:16 -07002051 }
2052 }
2053 break;
2054 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2056 const RegType& return_type = GetMethodReturnType();
2057 if (!return_type.IsReferenceTypes()) {
2058 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2059 } else {
2060 /* return_type is the *expected* return type, not register value */
2061 DCHECK(!return_type.IsZero());
2062 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002063 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2064 // Disallow returning uninitialized values and verify that the reference in vAA is an
2065 // instance of the "return_type"
2066 if (reg_type.IsUninitializedTypes()) {
2067 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2068 } else if (!return_type.IsAssignableFrom(reg_type)) {
2069 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2070 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002071 }
2072 }
2073 }
2074 break;
2075
2076 case Instruction::CONST_4:
2077 case Instruction::CONST_16:
2078 case Instruction::CONST:
2079 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::CONST_HIGH16:
2083 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 work_line_->SetRegisterType(dec_insn.vA_,
2085 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::CONST_WIDE_16:
2088 case Instruction::CONST_WIDE_32:
2089 case Instruction::CONST_WIDE:
2090 case Instruction::CONST_WIDE_HIGH16:
2091 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::CONST_STRING:
2095 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002099 // Get type from instruction if unresolved then we need an access check
2100 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2101 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2102 // Register holds class, ie its type is class, but on error we keep it Unknown
2103 work_line_->SetRegisterType(dec_insn.vA_,
2104 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 }
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::MONITOR_EXIT:
2111 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002112 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002113 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002114 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002115 * to the need to handle asynchronous exceptions, a now-deprecated
2116 * feature that Dalvik doesn't support.)
2117 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002118 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002119 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002120 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002121 * structured locking checks are working, the former would have
2122 * failed on the -enter instruction, and the latter is impossible.
2123 *
2124 * This is fortunate, because issue 3221411 prevents us from
2125 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002126 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002127 * some catch blocks (which will show up as "dead" code when
2128 * we skip them here); if we can't, then the code path could be
2129 * "live" so we still need to check it.
2130 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 opcode_flag &= ~Instruction::kThrow;
2132 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134
Ian Rogers28ad40d2011-10-27 15:19:26 -07002135 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002137 /*
2138 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2139 * could be a "upcast" -- not expected, so we don't try to address it.)
2140 *
2141 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2142 * dec_insn.vA_ when branching to a handler.
2143 */
2144 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2145 const RegType& res_type =
2146 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002147 if (res_type.IsUnknown()) {
2148 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2149 break; // couldn't resolve class
2150 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002151 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2152 const RegType& orig_type =
2153 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2154 if (!res_type.IsNonZeroReferenceTypes()) {
2155 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2156 } else if (!orig_type.IsReferenceTypes()) {
2157 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002158 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002159 if (is_checkcast) {
2160 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002163 }
jeffhaobdb76512011-09-07 11:43:16 -07002164 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002165 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 }
2167 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002168 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2169 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002170 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002171 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 } else {
2173 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2174 }
2175 }
2176 break;
2177 }
2178 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002179 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhao8cd6dda2012-02-22 10:15:34 -08002180 if (res_type.IsUnknown()) {
2181 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2182 break; // couldn't resolve class
2183 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002184 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2185 // can't create an instance of an interface or abstract class */
2186 if (!res_type.IsInstantiableTypes()) {
2187 Fail(VERIFY_ERROR_INSTANTIATION)
2188 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002190 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2191 // Any registers holding previous allocations from this address that have not yet been
2192 // initialized must be marked invalid.
2193 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2194 // add the new uninitialized reference to the register state
2195 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 }
2197 break;
2198 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002199 case Instruction::NEW_ARRAY:
2200 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002203 VerifyNewArray(dec_insn, true, false);
2204 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002205 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002206 case Instruction::FILLED_NEW_ARRAY_RANGE:
2207 VerifyNewArray(dec_insn, true, true);
2208 just_set_result = true; // Filled new array range sets result register
2209 break;
jeffhaobdb76512011-09-07 11:43:16 -07002210 case Instruction::CMPL_FLOAT:
2211 case Instruction::CMPG_FLOAT:
jeffhao457cc512012-02-02 16:55:13 -08002212 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float())) {
2213 break;
2214 }
2215 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float())) {
2216 break;
2217 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::CMPL_DOUBLE:
2221 case Instruction::CMPG_DOUBLE:
jeffhao457cc512012-02-02 16:55:13 -08002222 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double())) {
2223 break;
2224 }
2225 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double())) {
2226 break;
2227 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::CMP_LONG:
jeffhao457cc512012-02-02 16:55:13 -08002231 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long())) {
2232 break;
2233 }
2234 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long())) {
2235 break;
2236 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002240 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2241 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2242 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002243 }
2244 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 }
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::GOTO:
2247 case Instruction::GOTO_16:
2248 case Instruction::GOTO_32:
2249 /* no effect on or use of registers */
2250 break;
2251
2252 case Instruction::PACKED_SWITCH:
2253 case Instruction::SPARSE_SWITCH:
2254 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 case Instruction::FILL_ARRAY_DATA: {
2259 /* Similar to the verification done for APUT */
Ian Rogers89310de2012-02-01 13:47:30 -08002260 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA_);
2261 /* array_type can be null if the reg type is Zero */
2262 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002263 if (!array_type.IsArrayTypes()) {
2264 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002265 } else {
jeffhao457cc512012-02-02 16:55:13 -08002266 const RegType& component_type = reg_types_.GetComponentType(array_type,
2267 method_->GetDeclaringClass()->GetClassLoader());
2268 DCHECK(!component_type.IsUnknown());
2269 if (component_type.IsNonZeroReferenceTypes()) {
2270 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with component type "
2271 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 } else {
jeffhao457cc512012-02-02 16:55:13 -08002273 // Now verify if the element width in the table matches the element width declared in
2274 // the array
2275 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2276 if (array_data[0] != Instruction::kArrayDataSignature) {
2277 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2278 } else {
2279 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2280 // Since we don't compress the data in Dex, expect to see equal width of data stored
2281 // in the table and expected from the array class.
2282 if (array_data[1] != elem_width) {
2283 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2284 << " vs " << elem_width << ")";
2285 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 }
2287 }
jeffhaobdb76512011-09-07 11:43:16 -07002288 }
2289 }
2290 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 }
jeffhaobdb76512011-09-07 11:43:16 -07002292 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 case Instruction::IF_NE: {
2294 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2295 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2296 bool mismatch = false;
2297 if (reg_type1.IsZero()) { // zero then integral or reference expected
2298 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2299 } else if (reg_type1.IsReferenceTypes()) { // both references?
2300 mismatch = !reg_type2.IsReferenceTypes();
2301 } else { // both integral?
2302 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2303 }
2304 if (mismatch) {
2305 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2306 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002307 }
2308 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 }
jeffhaobdb76512011-09-07 11:43:16 -07002310 case Instruction::IF_LT:
2311 case Instruction::IF_GE:
2312 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 case Instruction::IF_LE: {
2314 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2315 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2316 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2317 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2318 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002319 }
2320 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 }
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 case Instruction::IF_NEZ: {
2324 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2325 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2326 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2327 }
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 }
jeffhaobdb76512011-09-07 11:43:16 -07002330 case Instruction::IF_LTZ:
2331 case Instruction::IF_GEZ:
2332 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 case Instruction::IF_LEZ: {
2334 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2335 if (!reg_type.IsIntegralTypes()) {
2336 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2337 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2338 }
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 }
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2343 break;
jeffhaobdb76512011-09-07 11:43:16 -07002344 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 VerifyAGet(dec_insn, reg_types_.Char(), true);
2349 break;
jeffhaobdb76512011-09-07 11:43:16 -07002350 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 case Instruction::AGET:
2354 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2355 break;
jeffhaobdb76512011-09-07 11:43:16 -07002356 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 VerifyAGet(dec_insn, reg_types_.Long(), true);
2358 break;
2359 case Instruction::AGET_OBJECT:
2360 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 case Instruction::APUT_BOOLEAN:
2364 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2365 break;
2366 case Instruction::APUT_BYTE:
2367 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2368 break;
2369 case Instruction::APUT_CHAR:
2370 VerifyAPut(dec_insn, reg_types_.Char(), true);
2371 break;
2372 case Instruction::APUT_SHORT:
2373 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002376 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384
jeffhaobdb76512011-09-07 11:43:16 -07002385 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002386 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
jeffhaobdb76512011-09-07 11:43:16 -07002388 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002389 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 break;
jeffhaobdb76512011-09-07 11:43:16 -07002391 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002392 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 break;
jeffhaobdb76512011-09-07 11:43:16 -07002394 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002395 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 break;
2397 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002398 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002401 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002404 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 break;
jeffhaobdb76512011-09-07 11:43:16 -07002406
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002408 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 break;
2410 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002411 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 break;
2413 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002414 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 break;
2416 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002417 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002420 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002423 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 break;
jeffhaobdb76512011-09-07 11:43:16 -07002425 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002426 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
2428
jeffhaobdb76512011-09-07 11:43:16 -07002429 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002430 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 break;
jeffhaobdb76512011-09-07 11:43:16 -07002432 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002433 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 break;
jeffhaobdb76512011-09-07 11:43:16 -07002435 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002436 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 break;
jeffhaobdb76512011-09-07 11:43:16 -07002438 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002439 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 break;
2441 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002442 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002445 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002448 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 break;
2450
2451 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002452 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 break;
2454 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002455 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 break;
2457 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002458 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 break;
2460 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002461 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002464 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002467 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002470 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472
2473 case Instruction::INVOKE_VIRTUAL:
2474 case Instruction::INVOKE_VIRTUAL_RANGE:
2475 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 case Instruction::INVOKE_SUPER_RANGE: {
2477 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2478 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2479 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2480 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2481 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2482 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 const char* descriptor;
2484 if (called_method == NULL) {
2485 uint32_t method_idx = dec_insn.vB_;
2486 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2487 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002488 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002490 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002491 }
Ian Rogers9074b992011-10-26 17:41:55 -07002492 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002493 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002495 just_set_result = true;
2496 }
2497 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 }
jeffhaobdb76512011-09-07 11:43:16 -07002499 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 case Instruction::INVOKE_DIRECT_RANGE: {
2501 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2502 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2503 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002504 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 * Some additional checks when calling a constructor. We know from the invocation arg check
2506 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2507 * that to require that called_method->klass is the same as this->klass or this->super,
2508 * allowing the latter only if the "this" argument is the same as the "this" argument to
2509 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002510 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002511 bool is_constructor;
2512 if (called_method != NULL) {
2513 is_constructor = called_method->IsConstructor();
2514 } else {
2515 uint32_t method_idx = dec_insn.vB_;
2516 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2517 const char* name = dex_file_->GetMethodName(method_id);
2518 is_constructor = strcmp(name, "<init>") == 0;
2519 }
2520 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2522 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524
2525 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 if (this_type.IsZero()) {
2527 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002530 if (called_method != NULL) {
2531 Class* this_class = this_type.GetClass();
2532 DCHECK(this_class != NULL);
2533 /* must be in same class or in superclass */
2534 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2535 if (this_class != method_->GetDeclaringClass()) {
2536 Fail(VERIFY_ERROR_GENERIC)
2537 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2538 break;
2539 }
2540 } else if (called_method->GetDeclaringClass() != this_class) {
2541 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
2543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 }
2545
2546 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002547 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2549 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 }
2552
2553 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002554 * Replace the uninitialized reference with an initialized one. We need to do this for all
2555 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002556 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 work_line_->MarkRefsAsInitialized(this_type);
2558 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002560 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002561 const char* descriptor;
2562 if (called_method == NULL) {
2563 uint32_t method_idx = dec_insn.vB_;
2564 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2565 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002566 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002567 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002568 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002569 }
Ian Rogers9074b992011-10-26 17:41:55 -07002570 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002571 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002573 just_set_result = true;
2574 }
2575 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 }
jeffhaobdb76512011-09-07 11:43:16 -07002577 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 case Instruction::INVOKE_STATIC_RANGE: {
2579 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2580 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2581 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002582 const char* descriptor;
2583 if (called_method == NULL) {
2584 uint32_t method_idx = dec_insn.vB_;
2585 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2586 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002587 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002588 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002589 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002590 }
Ian Rogers9074b992011-10-26 17:41:55 -07002591 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002592 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 work_line_->SetResultRegisterType(return_type);
2594 just_set_result = true;
2595 }
jeffhaobdb76512011-09-07 11:43:16 -07002596 }
2597 break;
2598 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 case Instruction::INVOKE_INTERFACE_RANGE: {
2600 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2601 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2602 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002603 if (abs_method != NULL) {
2604 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002605 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002606 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2607 << PrettyMethod(abs_method) << "'";
2608 break;
2609 }
2610 }
2611 /* Get the type of the "this" arg, which should either be a sub-interface of called
2612 * interface or Object (see comments in RegType::JoinClass).
2613 */
2614 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2615 if (failure_ == VERIFY_ERROR_NONE) {
2616 if (this_type.IsZero()) {
2617 /* null pointer always passes (and always fails at runtime) */
2618 } else {
2619 if (this_type.IsUninitializedTypes()) {
2620 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2621 << this_type;
2622 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002624 // In the past we have tried to assert that "called_interface" is assignable
2625 // from "this_type.GetClass()", however, as we do an imprecise Join
2626 // (RegType::JoinClass) we don't have full information on what interfaces are
2627 // implemented by "this_type". For example, two classes may implement the same
2628 // interfaces and have a common parent that doesn't implement the interface. The
2629 // join will set "this_type" to the parent class and a test that this implements
2630 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002631 }
2632 }
jeffhaobdb76512011-09-07 11:43:16 -07002633 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 * We don't have an object instance, so we can't find the concrete method. However, all of
2635 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002636 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002637 const char* descriptor;
2638 if (abs_method == NULL) {
2639 uint32_t method_idx = dec_insn.vB_;
2640 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2641 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002642 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002643 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002644 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002645 }
Ian Rogers9074b992011-10-26 17:41:55 -07002646 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002647 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2648 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002650 just_set_result = true;
2651 }
2652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 }
jeffhaobdb76512011-09-07 11:43:16 -07002654 case Instruction::NEG_INT:
2655 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::NEG_LONG:
2659 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002691 break;
2692 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713
2714 case Instruction::ADD_INT:
2715 case Instruction::SUB_INT:
2716 case Instruction::MUL_INT:
2717 case Instruction::REM_INT:
2718 case Instruction::DIV_INT:
2719 case Instruction::SHL_INT:
2720 case Instruction::SHR_INT:
2721 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::AND_INT:
2725 case Instruction::OR_INT:
2726 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::ADD_LONG:
2730 case Instruction::SUB_LONG:
2731 case Instruction::MUL_LONG:
2732 case Instruction::DIV_LONG:
2733 case Instruction::REM_LONG:
2734 case Instruction::AND_LONG:
2735 case Instruction::OR_LONG:
2736 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::SHL_LONG:
2740 case Instruction::SHR_LONG:
2741 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 /* shift distance is Int, making these different from other binary operations */
2743 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745 case Instruction::ADD_FLOAT:
2746 case Instruction::SUB_FLOAT:
2747 case Instruction::MUL_FLOAT:
2748 case Instruction::DIV_FLOAT:
2749 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752 case Instruction::ADD_DOUBLE:
2753 case Instruction::SUB_DOUBLE:
2754 case Instruction::MUL_DOUBLE:
2755 case Instruction::DIV_DOUBLE:
2756 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::ADD_INT_2ADDR:
2760 case Instruction::SUB_INT_2ADDR:
2761 case Instruction::MUL_INT_2ADDR:
2762 case Instruction::REM_INT_2ADDR:
2763 case Instruction::SHL_INT_2ADDR:
2764 case Instruction::SHR_INT_2ADDR:
2765 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::AND_INT_2ADDR:
2769 case Instruction::OR_INT_2ADDR:
2770 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::ADD_LONG_2ADDR:
2777 case Instruction::SUB_LONG_2ADDR:
2778 case Instruction::MUL_LONG_2ADDR:
2779 case Instruction::DIV_LONG_2ADDR:
2780 case Instruction::REM_LONG_2ADDR:
2781 case Instruction::AND_LONG_2ADDR:
2782 case Instruction::OR_LONG_2ADDR:
2783 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::SHL_LONG_2ADDR:
2787 case Instruction::SHR_LONG_2ADDR:
2788 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::ADD_FLOAT_2ADDR:
2792 case Instruction::SUB_FLOAT_2ADDR:
2793 case Instruction::MUL_FLOAT_2ADDR:
2794 case Instruction::DIV_FLOAT_2ADDR:
2795 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798 case Instruction::ADD_DOUBLE_2ADDR:
2799 case Instruction::SUB_DOUBLE_2ADDR:
2800 case Instruction::MUL_DOUBLE_2ADDR:
2801 case Instruction::DIV_DOUBLE_2ADDR:
2802 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
2805 case Instruction::ADD_INT_LIT16:
2806 case Instruction::RSUB_INT:
2807 case Instruction::MUL_INT_LIT16:
2808 case Instruction::DIV_INT_LIT16:
2809 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812 case Instruction::AND_INT_LIT16:
2813 case Instruction::OR_INT_LIT16:
2814 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::ADD_INT_LIT8:
2818 case Instruction::RSUB_INT_LIT8:
2819 case Instruction::MUL_INT_LIT8:
2820 case Instruction::DIV_INT_LIT8:
2821 case Instruction::REM_INT_LIT8:
2822 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002823 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002824 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::AND_INT_LIT8:
2828 case Instruction::OR_INT_LIT8:
2829 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832
2833 /*
2834 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002835 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002836 * inserted in the course of verification, we can expect to see it here.
2837 */
jeffhaob4df5142011-09-19 20:25:32 -07002838 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002839 break;
2840
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002842 case Instruction::UNUSED_EE:
2843 case Instruction::UNUSED_EF:
2844 case Instruction::UNUSED_F2:
2845 case Instruction::UNUSED_F3:
2846 case Instruction::UNUSED_F4:
2847 case Instruction::UNUSED_F5:
2848 case Instruction::UNUSED_F6:
2849 case Instruction::UNUSED_F7:
2850 case Instruction::UNUSED_F8:
2851 case Instruction::UNUSED_F9:
2852 case Instruction::UNUSED_FA:
2853 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002854 case Instruction::UNUSED_F0:
2855 case Instruction::UNUSED_F1:
2856 case Instruction::UNUSED_E3:
2857 case Instruction::UNUSED_E8:
2858 case Instruction::UNUSED_E7:
2859 case Instruction::UNUSED_E4:
2860 case Instruction::UNUSED_E9:
2861 case Instruction::UNUSED_FC:
2862 case Instruction::UNUSED_E5:
2863 case Instruction::UNUSED_EA:
2864 case Instruction::UNUSED_FD:
2865 case Instruction::UNUSED_E6:
2866 case Instruction::UNUSED_EB:
2867 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002868 case Instruction::UNUSED_3E:
2869 case Instruction::UNUSED_3F:
2870 case Instruction::UNUSED_40:
2871 case Instruction::UNUSED_41:
2872 case Instruction::UNUSED_42:
2873 case Instruction::UNUSED_43:
2874 case Instruction::UNUSED_73:
2875 case Instruction::UNUSED_79:
2876 case Instruction::UNUSED_7A:
2877 case Instruction::UNUSED_EC:
2878 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002879 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002880 break;
2881
2882 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002883 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002884 * complain if an instruction is missing (which is desirable).
2885 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002887
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 if (failure_ != VERIFY_ERROR_NONE) {
2889 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002890 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002891 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002892 return false;
2893 } else {
2894 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002895 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002897 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002899 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002901 opcode_flag = Instruction::kThrow;
2902 }
2903 }
jeffhaobdb76512011-09-07 11:43:16 -07002904 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 * If we didn't just set the result register, clear it out. This ensures that you can only use
2906 * "move-result" immediately after the result is set. (We could check this statically, but it's
2907 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002908 */
2909 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002911 }
2912
jeffhaoa0a764a2011-09-16 10:43:38 -07002913 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002914 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2916 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2917 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002918 return false;
2919 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2921 // next instruction isn't one.
2922 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002923 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 }
2925 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2926 if (next_line != NULL) {
2927 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2928 // needed.
2929 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002930 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 }
jeffhaobdb76512011-09-07 11:43:16 -07002932 } else {
2933 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 * We're not recording register data for the next instruction, so we don't know what the prior
2935 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002936 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002938 }
2939 }
2940
2941 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002942 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002943 *
2944 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002945 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002946 * somebody could get a reference field, check it for zero, and if the
2947 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002948 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002949 * that, and will reject the code.
2950 *
2951 * TODO: avoid re-fetching the branch target
2952 */
2953 if ((opcode_flag & Instruction::kBranch) != 0) {
2954 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002956 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002958 return false;
2959 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002960 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002962 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 }
jeffhaobdb76512011-09-07 11:43:16 -07002964 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002966 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 }
jeffhaobdb76512011-09-07 11:43:16 -07002968 }
2969
2970 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002971 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002972 *
2973 * We've already verified that the table is structurally sound, so we
2974 * just need to walk through and tag the targets.
2975 */
2976 if ((opcode_flag & Instruction::kSwitch) != 0) {
2977 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2978 const uint16_t* switch_insns = insns + offset_to_switch;
2979 int switch_count = switch_insns[1];
2980 int offset_to_targets, targ;
2981
2982 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2983 /* 0 = sig, 1 = count, 2/3 = first key */
2984 offset_to_targets = 4;
2985 } else {
2986 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002987 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002988 offset_to_targets = 2 + 2 * switch_count;
2989 }
2990
2991 /* verify each switch target */
2992 for (targ = 0; targ < switch_count; targ++) {
2993 int offset;
2994 uint32_t abs_offset;
2995
2996 /* offsets are 32-bit, and only partly endian-swapped */
2997 offset = switch_insns[offset_to_targets + targ * 2] |
2998 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 abs_offset = work_insn_idx_ + offset;
3000 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
3001 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003002 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 }
3004 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07003005 return false;
3006 }
3007 }
3008
3009 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3011 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003012 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
3014 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003015 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003016
Ian Rogers0571d352011-11-03 19:51:38 -07003017 for (; iterator.HasNext(); iterator.Next()) {
3018 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 within_catch_all = true;
3020 }
jeffhaobdb76512011-09-07 11:43:16 -07003021 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3023 * "work_regs", because at runtime the exception will be thrown before the instruction
3024 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003025 */
Ian Rogers0571d352011-11-03 19:51:38 -07003026 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003027 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 }
jeffhaobdb76512011-09-07 11:43:16 -07003029 }
3030
3031 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3033 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003034 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003036 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 * The state in work_line reflects the post-execution state. If the current instruction is a
3038 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003039 * it will do so before grabbing the lock).
3040 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
3042 Fail(VERIFY_ERROR_GENERIC)
3043 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003044 return false;
3045 }
3046 }
3047 }
3048
jeffhaod1f0fde2011-09-08 17:25:33 -07003049 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 if ((opcode_flag & Instruction::kReturn) != 0) {
3051 if(!work_line_->VerifyMonitorStackEmpty()) {
3052 return false;
3053 }
jeffhaobdb76512011-09-07 11:43:16 -07003054 }
3055
3056 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003057 * Update start_guess. Advance to the next instruction of that's
3058 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003059 * neither of those exists we're in a return or throw; leave start_guess
3060 * alone and let the caller sort it out.
3061 */
3062 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003064 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3065 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003067 }
3068
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3070 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003071
3072 return true;
3073}
3074
Ian Rogers28ad40d2011-10-27 15:19:26 -07003075const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003076 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003077 Class* referrer = method_->GetDeclaringClass();
3078 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3079 const RegType& result =
3080 klass != NULL ? reg_types_.FromClass(klass)
3081 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3082 if (klass == NULL && !result.IsUnresolvedTypes()) {
3083 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 }
jeffhaod1224c72012-02-29 13:43:08 -08003085 if (result.IsUnknown()) {
3086 Fail(VERIFY_ERROR_GENERIC) << "accessing unknown class in " << PrettyDescriptor(referrer);
3087 return result;
3088 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003089 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3090 // check at runtime if access is allowed and so pass here.
3091 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3092 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003093 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003094 << result << "'";
3095 return reg_types_.Unknown();
3096 } else {
3097 return result;
3098 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003099}
3100
Ian Rogers28ad40d2011-10-27 15:19:26 -07003101const RegType& DexVerifier::GetCaughtExceptionType() {
3102 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003104 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3106 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003107 CatchHandlerIterator iterator(handlers_ptr);
3108 for (; iterator.HasNext(); iterator.Next()) {
3109 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3110 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003111 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003113 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003114 if (common_super == NULL) {
3115 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3116 // as that is caught at runtime
3117 common_super = &exception;
3118 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3119 // We don't know enough about the type and the common path merge will result in
3120 // Conflict. Fail here knowing the correct thing can be done at runtime.
Ian Rogers28ad40d2011-10-27 15:19:26 -07003121 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3122 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003123 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003124 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003126 common_super = &common_super->Merge(exception, &reg_types_);
3127 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 }
3129 }
3130 }
3131 }
Ian Rogers0571d352011-11-03 19:51:38 -07003132 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 }
3134 }
3135 if (common_super == NULL) {
3136 /* no catch blocks, or no catches with classes we can find */
3137 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003138 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003140 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003141}
3142
jeffhao8cd6dda2012-02-22 10:15:34 -08003143Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003144 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3145 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3146 if (failure_ != VERIFY_ERROR_NONE) {
3147 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3148 return NULL;
3149 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003150 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003151 return NULL; // Can't resolve Class so no more to do here
3152 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003153 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 Class* referrer = method_->GetDeclaringClass();
3155 DexCache* dex_cache = referrer->GetDexCache();
3156 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3157 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003158 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003159 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003160
3161 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003163 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 res_method = klass->FindInterfaceMethod(name, signature);
3165 } else {
3166 res_method = klass->FindVirtualMethod(name, signature);
3167 }
3168 if (res_method != NULL) {
3169 dex_cache->SetResolvedMethod(method_idx, res_method);
3170 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003171 // If a virtual or interface method wasn't found with the expected type, look in
3172 // the direct methods. This can happen when the wrong invoke type is used or when
3173 // a class has changed, and will be flagged as an error in later checks.
3174 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3175 res_method = klass->FindDirectMethod(name, signature);
3176 }
3177 if (res_method == NULL) {
3178 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3179 << PrettyDescriptor(klass) << "." << name
3180 << " " << signature;
3181 return NULL;
3182 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 }
3184 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003185 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3186 // enforce them here.
3187 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3188 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3189 << PrettyMethod(res_method);
3190 return NULL;
3191 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003192 // Disallow any calls to class initializers.
3193 if (MethodHelper(res_method).IsClassInitializer()) {
3194 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to class initializer "
3195 << PrettyMethod(res_method);
3196 return NULL;
3197 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003198 // Check if access is allowed.
3199 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3200 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3201 << " from " << PrettyDescriptor(referrer) << ")";
3202 return NULL;
3203 }
jeffhaode0d9c92012-02-27 13:58:13 -08003204 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3205 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
3206 Fail(VERIFY_ERROR_GENERIC) << "invoke-super/virtual can't be used on private method "
3207 << PrettyMethod(res_method);
3208 return NULL;
3209 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003210 // Check that interface methods match interface classes.
3211 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3212 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3213 << " is in an interface class " << PrettyClass(klass);
3214 return NULL;
3215 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3216 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3217 << " is in a non-interface class " << PrettyClass(klass);
3218 return NULL;
3219 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 // See if the method type implied by the invoke instruction matches the access flags for the
3221 // target method.
3222 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3223 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3224 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3225 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003226 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3227 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003228 return NULL;
3229 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003230 return res_method;
3231}
3232
3233Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3234 MethodType method_type, bool is_range, bool is_super) {
3235 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3236 // we're making.
3237 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_, method_type);
3238 if (res_method == NULL) { // error or class is unresolved
3239 return NULL;
3240 }
3241
Ian Rogersd81871c2011-10-03 13:57:23 -07003242 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3243 // has a vtable entry for the target method.
3244 if (is_super) {
3245 DCHECK(method_type == METHOD_VIRTUAL);
3246 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003247 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 if (super == NULL) { // Only Object has no super class
3249 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3250 << " to super " << PrettyMethod(res_method);
3251 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003252 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003254 << " to super " << PrettyDescriptor(super)
3255 << "." << mh.GetName()
3256 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 }
3258 return NULL;
3259 }
3260 }
3261 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3262 // match the call to the signature. Also, we might might be calling through an abstract method
3263 // definition (which doesn't have register count values).
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003264 size_t expected_args = dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 /* caught by static verifier */
3266 DCHECK(is_range || expected_args <= 5);
3267 if (expected_args > code_item_->outs_size_) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003268 Fail(VERIFY_ERROR_GENERIC) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3270 return NULL;
3271 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003272
jeffhaobdb76512011-09-07 11:43:16 -07003273 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 * Check the "this" argument, which must be an instance of the class
3275 * that declared the method. For an interface class, we don't do the
3276 * full interface merge, so we can't do a rigorous check here (which
3277 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003278 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003279 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 if (!res_method->IsStatic()) {
3281 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3282 if (failure_ != VERIFY_ERROR_NONE) {
3283 return NULL;
3284 }
3285 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3286 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3287 return NULL;
3288 }
3289 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003290 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3291 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003292 Fail(VERIFY_ERROR_GENERIC) << "'this' argument '" << actual_arg_type
3293 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 return NULL;
3295 }
3296 }
3297 actual_args++;
3298 }
3299 /*
3300 * Process the target method's signature. This signature may or may not
3301 * have been verified, so we can't assume it's properly formed.
3302 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003303 MethodHelper mh(res_method);
3304 const DexFile::TypeList* params = mh.GetParameterTypeList();
3305 size_t params_size = params == NULL ? 0 : params->Size();
3306 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 if (actual_args >= expected_args) {
3308 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003309 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3310 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 return NULL;
3312 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003313 const char* descriptor =
3314 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3315 if (descriptor == NULL) {
3316 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3317 << " missing signature component";
3318 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 }
3320 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003321 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003322 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3323 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3324 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 }
3326 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 if (actual_args != expected_args) {
3329 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003330 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 return NULL;
3332 } else {
3333 return res_method;
3334 }
3335}
3336
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003337const RegType& DexVerifier::GetMethodReturnType() {
3338 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3339 MethodHelper(method_).GetReturnTypeDescriptor());
3340}
3341
Ian Rogers0c4a5062012-02-03 15:18:59 -08003342void DexVerifier::VerifyNewArray(const Instruction::DecodedInstruction& dec_insn, bool is_filled,
3343 bool is_range) {
3344 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB_ : dec_insn.vC_);
3345 if (res_type.IsUnknown()) {
3346 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3347 } else {
3348 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3349 if (!res_type.IsArrayTypes()) {
3350 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
3351 } else if (!is_filled) {
3352 /* make sure "size" register is valid type */
3353 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
3354 /* set register type to array class */
3355 work_line_->SetRegisterType(dec_insn.vA_, res_type);
3356 } else {
3357 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3358 // the list and fail. It's legal, if silly, for arg_count to be zero.
3359 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3360 method_->GetDeclaringClass()->GetClassLoader());
3361 uint32_t arg_count = dec_insn.vA_;
3362 for (size_t ui = 0; ui < arg_count; ui++) {
3363 uint32_t get_reg = is_range ? dec_insn.vC_ + ui : dec_insn.arg_[ui];
3364 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3365 work_line_->SetResultRegisterType(reg_types_.Unknown());
3366 return;
3367 }
3368 }
3369 // filled-array result goes into "result" register
3370 work_line_->SetResultRegisterType(res_type);
3371 }
3372 }
3373}
3374
Ian Rogersd81871c2011-10-03 13:57:23 -07003375void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3376 const RegType& insn_type, bool is_primitive) {
3377 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3378 if (!index_type.IsArrayIndexTypes()) {
3379 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3380 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003381 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3382 if (array_type.IsZero()) {
3383 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3384 // instruction type. TODO: have a proper notion of bottom here.
3385 if (!is_primitive || insn_type.IsCategory1Types()) {
3386 // Reference or category 1
3387 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003389 // Category 2
3390 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3391 }
jeffhaofc3144e2012-02-01 17:21:15 -08003392 } else if (!array_type.IsArrayTypes()) {
3393 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003394 } else {
3395 /* verify the class */
3396 const RegType& component_type = reg_types_.GetComponentType(array_type,
3397 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003398 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003399 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3400 << " source for aget-object";
3401 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3402 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3403 << " source for category 1 aget";
3404 } else if (is_primitive && !insn_type.Equals(component_type) &&
3405 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3406 (insn_type.IsLong() && component_type.IsDouble()))) {
3407 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003409 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 // Use knowledge of the field type which is stronger than the type inferred from the
3411 // instruction, which can't differentiate object types and ints from floats, longs from
3412 // doubles.
3413 work_line_->SetRegisterType(dec_insn.vA_, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 }
3415 }
3416 }
3417}
3418
3419void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3420 const RegType& insn_type, bool is_primitive) {
3421 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3422 if (!index_type.IsArrayIndexTypes()) {
3423 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3424 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003425 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3426 if (array_type.IsZero()) {
3427 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3428 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003429 } else if (!array_type.IsArrayTypes()) {
3430 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003431 } else {
3432 /* verify the class */
3433 const RegType& component_type = reg_types_.GetComponentType(array_type,
3434 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003435 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003436 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3437 << " source for aput-object";
3438 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3439 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3440 << " source for category 1 aput";
3441 } else if (is_primitive && !insn_type.Equals(component_type) &&
3442 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3443 (insn_type.IsLong() && component_type.IsDouble()))) {
3444 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
3445 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003447 // The instruction agrees with the type of array, confirm the value to be stored does too
3448 // Note: we use the instruction type (rather than the component type) for aput-object as
3449 // incompatible classes will be caught at runtime as an array store exception
3450 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003451 }
3452 }
3453 }
3454}
3455
3456Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003457 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3458 // Check access to class
3459 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3460 if (failure_ != VERIFY_ERROR_NONE) {
3461 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3462 << dex_file_->GetFieldName(field_id) << ") in "
3463 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3464 return NULL;
3465 }
3466 if(klass_type.IsUnresolvedTypes()) {
3467 return NULL; // Can't resolve Class so no more to do here
3468 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003469 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003471 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3472 << dex_file_->GetFieldName(field_id) << ") in "
3473 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 DCHECK(Thread::Current()->IsExceptionPending());
3475 Thread::Current()->ClearException();
3476 return NULL;
3477 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3478 field->GetAccessFlags())) {
3479 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3480 << " from " << PrettyClass(method_->GetDeclaringClass());
3481 return NULL;
3482 } else if (!field->IsStatic()) {
3483 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3484 return NULL;
3485 } else {
3486 return field;
3487 }
3488}
3489
Ian Rogersd81871c2011-10-03 13:57:23 -07003490Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003491 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3492 // Check access to class
3493 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3494 if (failure_ != VERIFY_ERROR_NONE) {
3495 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3496 << dex_file_->GetFieldName(field_id) << ") in "
3497 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3498 return NULL;
3499 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003500 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003501 return NULL; // Can't resolve Class so no more to do here
3502 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003503 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003505 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3506 << dex_file_->GetFieldName(field_id) << ") in "
3507 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003508 DCHECK(Thread::Current()->IsExceptionPending());
3509 Thread::Current()->ClearException();
3510 return NULL;
3511 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3512 field->GetAccessFlags())) {
3513 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3514 << " from " << PrettyClass(method_->GetDeclaringClass());
3515 return NULL;
3516 } else if (field->IsStatic()) {
3517 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3518 << " to not be static";
3519 return NULL;
3520 } else if (obj_type.IsZero()) {
3521 // Cannot infer and check type, however, access will cause null pointer exception
3522 return field;
jeffhao8cd6dda2012-02-22 10:15:34 -08003523 } else if(obj_type.IsUninitializedTypes() &&
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3525 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3526 // Field accesses through uninitialized references are only allowable for constructors where
3527 // the field is declared in this class
3528 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3529 << " of a not fully initialized object within the context of "
3530 << PrettyMethod(method_);
3531 return NULL;
3532 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3533 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3534 // of C1. For resolution to occur the declared class of the field must be compatible with
3535 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3536 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3537 << " from object of type " << PrettyClass(obj_type.GetClass());
3538 return NULL;
3539 } else {
3540 return field;
3541 }
3542}
3543
Ian Rogersb94a27b2011-10-26 00:33:41 -07003544void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3545 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003546 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003547 Field* field;
3548 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003549 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003550 } else {
3551 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003552 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003553 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003554 if (failure_ != VERIFY_ERROR_NONE) {
3555 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3556 } else {
3557 const char* descriptor;
3558 const ClassLoader* loader;
3559 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003560 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003561 loader = field->GetDeclaringClass()->GetClassLoader();
3562 } else {
3563 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3564 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3565 loader = method_->GetDeclaringClass()->GetClassLoader();
3566 }
3567 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003568 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003569 if (field_type.Equals(insn_type) ||
3570 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3571 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 // expected that read is of the correct primitive type or that int reads are reading
3573 // floats or long reads are reading doubles
3574 } else {
3575 // This is a global failure rather than a class change failure as the instructions and
3576 // the descriptors for the type should have been consistent within the same file at
3577 // compile time
3578 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003579 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003580 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 return;
3582 }
3583 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003584 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003586 << " to be compatible with type '" << insn_type
3587 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003588 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 return;
3590 }
3591 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003592 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003593 }
3594}
3595
Ian Rogersb94a27b2011-10-26 00:33:41 -07003596void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3597 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003598 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003599 Field* field;
3600 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003601 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003602 } else {
3603 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003604 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003605 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003606 if (failure_ != VERIFY_ERROR_NONE) {
3607 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3608 } else {
3609 const char* descriptor;
3610 const ClassLoader* loader;
3611 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003612 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003613 loader = field->GetDeclaringClass()->GetClassLoader();
3614 } else {
3615 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3616 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3617 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003619 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3620 if (field != NULL) {
3621 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3622 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3623 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3624 return;
3625 }
3626 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003628 // Primitive field assignability rules are weaker than regular assignability rules
3629 bool instruction_compatible;
3630 bool value_compatible;
3631 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3632 if (field_type.IsIntegralTypes()) {
3633 instruction_compatible = insn_type.IsIntegralTypes();
3634 value_compatible = value_type.IsIntegralTypes();
3635 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003636 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003637 value_compatible = value_type.IsFloatTypes();
3638 } else if (field_type.IsLong()) {
3639 instruction_compatible = insn_type.IsLong();
3640 value_compatible = value_type.IsLongTypes();
3641 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003642 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003643 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003645 instruction_compatible = false; // reference field with primitive store
3646 value_compatible = false; // unused
3647 }
3648 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 // This is a global failure rather than a class change failure as the instructions and
3650 // the descriptors for the type should have been consistent within the same file at
3651 // compile time
3652 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003653 << " to be of type '" << insn_type
3654 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003655 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 return;
3657 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003658 if (!value_compatible) {
3659 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3660 << " of type " << value_type
3661 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003662 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003663 return;
3664 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003666 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003667 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003668 << " to be compatible with type '" << insn_type
3669 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003670 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003671 return;
3672 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003673 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003674 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 }
3676}
3677
3678bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3679 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3680 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3681 return false;
3682 }
3683 return true;
3684}
3685
Ian Rogersd81871c2011-10-03 13:57:23 -07003686void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003687 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003688 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003689 << " " << fail_messages_.str();
3690 return;
3691 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3693 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3694 VerifyErrorRefType ref_type;
3695 switch (inst->Opcode()) {
3696 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003697 case Instruction::CHECK_CAST:
3698 case Instruction::INSTANCE_OF:
3699 case Instruction::NEW_INSTANCE:
3700 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003701 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003702 case Instruction::FILLED_NEW_ARRAY_RANGE:
3703 ref_type = VERIFY_ERROR_REF_CLASS;
3704 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003705 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003706 case Instruction::IGET_BOOLEAN:
3707 case Instruction::IGET_BYTE:
3708 case Instruction::IGET_CHAR:
3709 case Instruction::IGET_SHORT:
3710 case Instruction::IGET_WIDE:
3711 case Instruction::IGET_OBJECT:
3712 case Instruction::IPUT:
3713 case Instruction::IPUT_BOOLEAN:
3714 case Instruction::IPUT_BYTE:
3715 case Instruction::IPUT_CHAR:
3716 case Instruction::IPUT_SHORT:
3717 case Instruction::IPUT_WIDE:
3718 case Instruction::IPUT_OBJECT:
3719 case Instruction::SGET:
3720 case Instruction::SGET_BOOLEAN:
3721 case Instruction::SGET_BYTE:
3722 case Instruction::SGET_CHAR:
3723 case Instruction::SGET_SHORT:
3724 case Instruction::SGET_WIDE:
3725 case Instruction::SGET_OBJECT:
3726 case Instruction::SPUT:
3727 case Instruction::SPUT_BOOLEAN:
3728 case Instruction::SPUT_BYTE:
3729 case Instruction::SPUT_CHAR:
3730 case Instruction::SPUT_SHORT:
3731 case Instruction::SPUT_WIDE:
3732 case Instruction::SPUT_OBJECT:
3733 ref_type = VERIFY_ERROR_REF_FIELD;
3734 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003735 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003736 case Instruction::INVOKE_VIRTUAL_RANGE:
3737 case Instruction::INVOKE_SUPER:
3738 case Instruction::INVOKE_SUPER_RANGE:
3739 case Instruction::INVOKE_DIRECT:
3740 case Instruction::INVOKE_DIRECT_RANGE:
3741 case Instruction::INVOKE_STATIC:
3742 case Instruction::INVOKE_STATIC_RANGE:
3743 case Instruction::INVOKE_INTERFACE:
3744 case Instruction::INVOKE_INTERFACE_RANGE:
3745 ref_type = VERIFY_ERROR_REF_METHOD;
3746 break;
jeffhaobdb76512011-09-07 11:43:16 -07003747 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003748 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003749 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003750 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3752 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3753 // instruction, so assert it.
3754 size_t width = inst->SizeInCodeUnits();
3755 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003756 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003757 // NOPs
3758 for (size_t i = 2; i < width; i++) {
3759 insns[work_insn_idx_ + i] = Instruction::NOP;
3760 }
3761 // Encode the opcode, with the failure code in the high byte
3762 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3763 (failure_ << 8) | // AA - component
3764 (ref_type << (8 + kVerifyErrorRefTypeShift));
3765 insns[work_insn_idx_] = new_instruction;
3766 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3767 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003768 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3769 << fail_messages_.str();
3770 if (gDebugVerify) {
3771 std::cout << std::endl << info_messages_.str();
3772 Dump(std::cout);
3773 }
jeffhaobdb76512011-09-07 11:43:16 -07003774}
jeffhaoba5ebb92011-08-25 17:24:37 -07003775
Ian Rogersd81871c2011-10-03 13:57:23 -07003776bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 bool changed = true;
3778 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3779 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003780 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 * We haven't processed this instruction before, and we haven't touched the registers here, so
3782 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3783 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003784 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003786 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003787 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3788 if (gDebugVerify) {
3789 copy->CopyFromLine(target_line);
3790 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 changed = target_line->MergeRegisters(merge_line);
3792 if (failure_ != VERIFY_ERROR_NONE) {
3793 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003794 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003795 if (gDebugVerify && changed) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003796 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "]"
3797 " to [" <<(void*)next_insn << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 << *copy.get() << " MERGE" << std::endl
3799 << *merge_line << " ==" << std::endl
3800 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003801 }
3802 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 if (changed) {
3804 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003805 }
3806 return true;
3807}
3808
Ian Rogersd81871c2011-10-03 13:57:23 -07003809void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3810 size_t* log2_max_gc_pc) {
3811 size_t local_gc_points = 0;
3812 size_t max_insn = 0;
3813 size_t max_ref_reg = -1;
3814 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3815 if (insn_flags_[i].IsGcPoint()) {
3816 local_gc_points++;
3817 max_insn = i;
3818 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003819 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003820 }
3821 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 *gc_points = local_gc_points;
3823 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3824 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003825 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 i++;
3827 }
3828 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003829}
3830
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003831const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 size_t num_entries, ref_bitmap_bits, pc_bits;
3833 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3834 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003835 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 // TODO: either a better GC map format or per method failures
3837 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3838 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003839 return NULL;
3840 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3842 // There are 2 bytes to encode the number of entries
3843 if (num_entries >= 65536) {
3844 // TODO: either a better GC map format or per method failures
3845 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3846 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003847 return NULL;
3848 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003849 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003850 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003851 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003852 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003854 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003855 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003856 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003857 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 // TODO: either a better GC map format or per method failures
3859 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3860 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3861 return NULL;
3862 }
3863 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003864 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 if (table == NULL) {
3866 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3867 return NULL;
3868 }
3869 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003870 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3871 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003872 table->push_back(num_entries & 0xFF);
3873 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3876 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003877 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003878 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003879 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 }
3881 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003882 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 }
3884 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003885 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 return table;
3887}
jeffhaoa0a764a2011-09-16 10:43:38 -07003888
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003889void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3891 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003892 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003893 size_t map_index = 0;
3894 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3895 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3896 if (insn_flags_[i].IsGcPoint()) {
3897 CHECK_LT(map_index, map.NumEntries());
3898 CHECK_EQ(map.GetPC(map_index), i);
3899 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3900 map_index++;
3901 RegisterLine* line = reg_table_.GetLine(i);
3902 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003903 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003904 CHECK_LT(j / 8, map.RegWidth());
3905 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3906 } else if ((j / 8) < map.RegWidth()) {
3907 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3908 } else {
3909 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3910 }
3911 }
3912 } else {
3913 CHECK(reg_bitmap == NULL);
3914 }
3915 }
3916}
jeffhaoa0a764a2011-09-16 10:43:38 -07003917
Ian Rogersd81871c2011-10-03 13:57:23 -07003918const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3919 size_t num_entries = NumEntries();
3920 // Do linear or binary search?
3921 static const size_t kSearchThreshold = 8;
3922 if (num_entries < kSearchThreshold) {
3923 for (size_t i = 0; i < num_entries; i++) {
3924 if (GetPC(i) == dex_pc) {
3925 return GetBitMap(i);
3926 }
3927 }
3928 } else {
3929 int lo = 0;
3930 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003931 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003932 int mid = (hi + lo) / 2;
3933 int mid_pc = GetPC(mid);
3934 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003935 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003937 hi = mid - 1;
3938 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003939 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003940 }
3941 }
3942 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003943 if (error_if_not_present) {
3944 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3945 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003946 return NULL;
3947}
3948
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003949Mutex DexVerifier::gc_maps_lock_("verifier gc maps lock");
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003950DexVerifier::GcMapTable DexVerifier::gc_maps_;
3951
3952void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003953 MutexLock mu(gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003954 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3955 if (existing_gc_map != NULL) {
3956 CHECK(*existing_gc_map == gc_map);
3957 delete existing_gc_map;
3958 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003959 gc_maps_[ref] = &gc_map;
3960 CHECK(GetGcMap(ref) != NULL);
3961}
3962
3963const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003964 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003965 GcMapTable::const_iterator it = gc_maps_.find(ref);
3966 if (it == gc_maps_.end()) {
3967 return NULL;
3968 }
3969 CHECK(it->second != NULL);
3970 return it->second;
3971}
3972
3973void DexVerifier::DeleteGcMaps() {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003974 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003975 STLDeleteValues(&gc_maps_);
3976}
3977
jeffhaod1224c72012-02-29 13:43:08 -08003978Mutex DexVerifier::rejected_classes_lock_("verifier rejected classes lock");
3979std::set<Compiler::ClassReference> DexVerifier::rejected_classes_;
3980
3981void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) {
3982 MutexLock mu(rejected_classes_lock_);
3983 rejected_classes_.insert(ref);
3984 CHECK(IsClassRejected(ref));
3985}
3986
3987bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) {
3988 MutexLock mu(rejected_classes_lock_);
3989 return (rejected_classes_.find(ref) != rejected_classes_.end());
3990}
3991
Logan Chienfca7e872011-12-20 20:08:22 +08003992#if defined(ART_USE_LLVM_COMPILER)
3993InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
3994 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3995 uint16_t regs_size = code_item_->registers_size_;
3996
3997 UniquePtr<InferredRegCategoryMap> table(
3998 new InferredRegCategoryMap(insns_size, regs_size));
3999
4000 for (size_t i = 0; i < insns_size; ++i) {
4001 if (RegisterLine* line = reg_table_.GetLine(i)) {
4002 for (size_t r = 0; r < regs_size; ++r) {
4003 RegType const &rt = line->GetRegisterType(r);
4004
4005 if (rt.IsZero()) {
4006 table->SetRegCategory(i, r, kRegZero);
4007 } else if (rt.IsCategory1Types()) {
4008 table->SetRegCategory(i, r, kRegCat1nr);
4009 } else if (rt.IsCategory2Types()) {
4010 table->SetRegCategory(i, r, kRegCat2);
4011 } else if (rt.IsReferenceTypes()) {
4012 table->SetRegCategory(i, r, kRegObject);
4013 } else {
4014 table->SetRegCategory(i, r, kRegUnknown);
4015 }
4016 }
4017 }
4018 }
4019
4020 return table.release();
4021}
4022#endif
4023
Ian Rogersd81871c2011-10-03 13:57:23 -07004024} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004025} // namespace art