blob: 2b90089bf00336e8a6dd014fb647af3993e6c02c [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 Rogers9ada79c2012-02-03 14:29:52 -08001046 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
1047 // errors into generic errors so that we re-verify at runtime. We may fail to find or to agree
1048 // on access because of not yet available class loaders, or class loaders that will differ at
1049 // runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001050 if (Runtime::Current()->IsCompiler()) {
1051 switch(error) {
1052 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;
1060 default:
1061 break;
1062 }
1063 }
1064 failure_ = error;
1065 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1066 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1067}
1068
Ian Rogersd81871c2011-10-03 13:57:23 -07001069bool DexVerifier::ComputeWidthsAndCountOps() {
1070 const uint16_t* insns = code_item_->insns_;
1071 size_t insns_size = code_item_->insns_size_in_code_units_;
1072 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001073 size_t new_instance_count = 0;
1074 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001076
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001078 Instruction::Code opcode = inst->Opcode();
1079 if (opcode == Instruction::NEW_INSTANCE) {
1080 new_instance_count++;
1081 } else if (opcode == Instruction::MONITOR_ENTER) {
1082 monitor_enter_count++;
1083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 size_t inst_size = inst->SizeInCodeUnits();
1085 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1086 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001087 inst = inst->Next();
1088 }
1089
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 if (dex_pc != insns_size) {
1091 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1092 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001093 return false;
1094 }
1095
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 new_instance_count_ = new_instance_count;
1097 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001098 return true;
1099}
1100
Ian Rogersd81871c2011-10-03 13:57:23 -07001101bool DexVerifier::ScanTryCatchBlocks() {
1102 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001103 if (tries_size == 0) {
1104 return true;
1105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001107 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001108
1109 for (uint32_t idx = 0; idx < tries_size; idx++) {
1110 const DexFile::TryItem* try_item = &tries[idx];
1111 uint32_t start = try_item->start_addr_;
1112 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001113 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1115 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001116 return false;
1117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 if (!insn_flags_[start].IsOpcode()) {
1119 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001120 return false;
1121 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 for (uint32_t dex_pc = start; dex_pc < end;
1123 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1124 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001125 }
1126 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001127 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001128 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001129 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001130 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001131 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001132 CatchHandlerIterator iterator(handlers_ptr);
1133 for (; iterator.HasNext(); iterator.Next()) {
1134 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 if (!insn_flags_[dex_pc].IsOpcode()) {
1136 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001137 return false;
1138 }
jeffhao60f83e32012-02-13 17:16:30 -08001139 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1140 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
1141 Fail(VERIFY_ERROR_GENERIC) << "exception handler doesn't start with move-exception ("
1142 << dex_pc << ")";
1143 return false;
1144 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001146 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1147 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001148 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1149 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001150 if (exception_type == NULL) {
1151 DCHECK(Thread::Current()->IsExceptionPending());
1152 Thread::Current()->ClearException();
1153 }
1154 }
jeffhaobdb76512011-09-07 11:43:16 -07001155 }
Ian Rogers0571d352011-11-03 19:51:38 -07001156 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001157 }
jeffhaobdb76512011-09-07 11:43:16 -07001158 return true;
1159}
1160
Ian Rogersd81871c2011-10-03 13:57:23 -07001161bool DexVerifier::VerifyInstructions() {
1162 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001163
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 /* Flag the start of the method as a branch target. */
1165 insn_flags_[0].SetBranchTarget();
1166
1167 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1168 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1169 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001170 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1171 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 /* Flag instructions that are garbage collection points */
1175 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1176 insn_flags_[dex_pc].SetGcPoint();
1177 }
1178 dex_pc += inst->SizeInCodeUnits();
1179 inst = inst->Next();
1180 }
1181 return true;
1182}
1183
1184bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1185 Instruction::DecodedInstruction dec_insn(inst);
1186 bool result = true;
1187 switch (inst->GetVerifyTypeArgumentA()) {
1188 case Instruction::kVerifyRegA:
1189 result = result && CheckRegisterIndex(dec_insn.vA_);
1190 break;
1191 case Instruction::kVerifyRegAWide:
1192 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1193 break;
1194 }
1195 switch (inst->GetVerifyTypeArgumentB()) {
1196 case Instruction::kVerifyRegB:
1197 result = result && CheckRegisterIndex(dec_insn.vB_);
1198 break;
1199 case Instruction::kVerifyRegBField:
1200 result = result && CheckFieldIndex(dec_insn.vB_);
1201 break;
1202 case Instruction::kVerifyRegBMethod:
1203 result = result && CheckMethodIndex(dec_insn.vB_);
1204 break;
1205 case Instruction::kVerifyRegBNewInstance:
1206 result = result && CheckNewInstance(dec_insn.vB_);
1207 break;
1208 case Instruction::kVerifyRegBString:
1209 result = result && CheckStringIndex(dec_insn.vB_);
1210 break;
1211 case Instruction::kVerifyRegBType:
1212 result = result && CheckTypeIndex(dec_insn.vB_);
1213 break;
1214 case Instruction::kVerifyRegBWide:
1215 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1216 break;
1217 }
1218 switch (inst->GetVerifyTypeArgumentC()) {
1219 case Instruction::kVerifyRegC:
1220 result = result && CheckRegisterIndex(dec_insn.vC_);
1221 break;
1222 case Instruction::kVerifyRegCField:
1223 result = result && CheckFieldIndex(dec_insn.vC_);
1224 break;
1225 case Instruction::kVerifyRegCNewArray:
1226 result = result && CheckNewArray(dec_insn.vC_);
1227 break;
1228 case Instruction::kVerifyRegCType:
1229 result = result && CheckTypeIndex(dec_insn.vC_);
1230 break;
1231 case Instruction::kVerifyRegCWide:
1232 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1233 break;
1234 }
1235 switch (inst->GetVerifyExtraFlags()) {
1236 case Instruction::kVerifyArrayData:
1237 result = result && CheckArrayData(code_offset);
1238 break;
1239 case Instruction::kVerifyBranchTarget:
1240 result = result && CheckBranchTarget(code_offset);
1241 break;
1242 case Instruction::kVerifySwitchTargets:
1243 result = result && CheckSwitchTargets(code_offset);
1244 break;
1245 case Instruction::kVerifyVarArg:
1246 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1247 break;
1248 case Instruction::kVerifyVarArgRange:
1249 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1250 break;
1251 case Instruction::kVerifyError:
1252 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1253 result = false;
1254 break;
1255 }
1256 return result;
1257}
1258
1259bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1260 if (idx >= code_item_->registers_size_) {
1261 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1262 << code_item_->registers_size_ << ")";
1263 return false;
1264 }
1265 return true;
1266}
1267
1268bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1269 if (idx + 1 >= code_item_->registers_size_) {
1270 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1271 << "+1 >= " << code_item_->registers_size_ << ")";
1272 return false;
1273 }
1274 return true;
1275}
1276
1277bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1278 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1279 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1280 << dex_file_->GetHeader().field_ids_size_ << ")";
1281 return false;
1282 }
1283 return true;
1284}
1285
1286bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1287 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1288 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1289 << dex_file_->GetHeader().method_ids_size_ << ")";
1290 return false;
1291 }
1292 return true;
1293}
1294
1295bool DexVerifier::CheckNewInstance(uint32_t idx) {
1296 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1297 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1298 << dex_file_->GetHeader().type_ids_size_ << ")";
1299 return false;
1300 }
1301 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001302 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 if (descriptor[0] != 'L') {
1304 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1305 return false;
1306 }
1307 return true;
1308}
1309
1310bool DexVerifier::CheckStringIndex(uint32_t idx) {
1311 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1312 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1313 << dex_file_->GetHeader().string_ids_size_ << ")";
1314 return false;
1315 }
1316 return true;
1317}
1318
1319bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1320 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1321 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1322 << dex_file_->GetHeader().type_ids_size_ << ")";
1323 return false;
1324 }
1325 return true;
1326}
1327
1328bool DexVerifier::CheckNewArray(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 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001335 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 const char* cp = descriptor;
1337 while (*cp++ == '[') {
1338 bracket_count++;
1339 }
1340 if (bracket_count == 0) {
1341 /* The given class must be an array type. */
1342 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1343 return false;
1344 } else if (bracket_count > 255) {
1345 /* It is illegal to create an array of more than 255 dimensions. */
1346 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1347 return false;
1348 }
1349 return true;
1350}
1351
1352bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1353 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1354 const uint16_t* insns = code_item_->insns_ + cur_offset;
1355 const uint16_t* array_data;
1356 int32_t array_data_offset;
1357
1358 DCHECK_LT(cur_offset, insn_count);
1359 /* make sure the start of the array data table is in range */
1360 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1361 if ((int32_t) cur_offset + array_data_offset < 0 ||
1362 cur_offset + array_data_offset + 2 >= insn_count) {
1363 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1364 << ", data offset " << array_data_offset << ", count " << insn_count;
1365 return false;
1366 }
1367 /* offset to array data table is a relative branch-style offset */
1368 array_data = insns + array_data_offset;
1369 /* make sure the table is 32-bit aligned */
1370 if ((((uint32_t) array_data) & 0x03) != 0) {
1371 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1372 << ", data offset " << array_data_offset;
1373 return false;
1374 }
1375 uint32_t value_width = array_data[1];
1376 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1377 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1378 /* make sure the end of the switch is in range */
1379 if (cur_offset + array_data_offset + table_size > insn_count) {
1380 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1381 << ", data offset " << array_data_offset << ", end "
1382 << cur_offset + array_data_offset + table_size
1383 << ", count " << insn_count;
1384 return false;
1385 }
1386 return true;
1387}
1388
1389bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1390 int32_t offset;
1391 bool isConditional, selfOkay;
1392 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1393 return false;
1394 }
1395 if (!selfOkay && offset == 0) {
1396 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1397 return false;
1398 }
1399 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1400 // identical "wrap-around" behavior, but it's unwise to depend on that.
1401 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1402 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1403 return false;
1404 }
1405 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1406 int32_t abs_offset = cur_offset + offset;
1407 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1408 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1409 << (void*) abs_offset << ") at " << (void*) cur_offset;
1410 return false;
1411 }
1412 insn_flags_[abs_offset].SetBranchTarget();
1413 return true;
1414}
1415
1416bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1417 bool* selfOkay) {
1418 const uint16_t* insns = code_item_->insns_ + cur_offset;
1419 *pConditional = false;
1420 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 switch (*insns & 0xff) {
1422 case Instruction::GOTO:
1423 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 break;
1425 case Instruction::GOTO_32:
1426 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001427 *selfOkay = true;
1428 break;
1429 case Instruction::GOTO_16:
1430 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 break;
1432 case Instruction::IF_EQ:
1433 case Instruction::IF_NE:
1434 case Instruction::IF_LT:
1435 case Instruction::IF_GE:
1436 case Instruction::IF_GT:
1437 case Instruction::IF_LE:
1438 case Instruction::IF_EQZ:
1439 case Instruction::IF_NEZ:
1440 case Instruction::IF_LTZ:
1441 case Instruction::IF_GEZ:
1442 case Instruction::IF_GTZ:
1443 case Instruction::IF_LEZ:
1444 *pOffset = (int16_t) insns[1];
1445 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001446 break;
1447 default:
1448 return false;
1449 break;
1450 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001451 return true;
1452}
1453
Ian Rogersd81871c2011-10-03 13:57:23 -07001454bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1455 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001456 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1460 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1461 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1462 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 return false;
1464 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 /* make sure the table is 32-bit aligned */
1468 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1470 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 return false;
1472 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 uint32_t switch_count = switch_insns[1];
1474 int32_t keys_offset, targets_offset;
1475 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1477 /* 0=sig, 1=count, 2/3=firstKey */
1478 targets_offset = 4;
1479 keys_offset = -1;
1480 expected_signature = Instruction::kPackedSwitchSignature;
1481 } else {
1482 /* 0=sig, 1=count, 2..count*2 = keys */
1483 keys_offset = 2;
1484 targets_offset = 2 + 2 * switch_count;
1485 expected_signature = Instruction::kSparseSwitchSignature;
1486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001489 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1490 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001491 return false;
1492 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 /* make sure the end of the switch is in range */
1494 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1496 << switch_offset << ", end "
1497 << (cur_offset + switch_offset + table_size)
1498 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 return false;
1500 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 /* for a sparse switch, verify the keys are in ascending order */
1502 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1504 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1506 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1507 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1509 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001510 return false;
1511 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 last_key = key;
1513 }
1514 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001515 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 for (uint32_t targ = 0; targ < switch_count; targ++) {
1517 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1518 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1519 int32_t abs_offset = cur_offset + offset;
1520 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1521 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1522 << (void*) abs_offset << ") at "
1523 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 return false;
1525 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 insn_flags_[abs_offset].SetBranchTarget();
1527 }
1528 return true;
1529}
1530
1531bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1532 if (vA > 5) {
1533 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1534 return false;
1535 }
1536 uint16_t registers_size = code_item_->registers_size_;
1537 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001538 if (arg[idx] >= registers_size) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
jeffhao457cc512012-02-02 16:55:13 -08001540 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 return false;
1542 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 }
1544
1545 return true;
1546}
1547
Ian Rogersd81871c2011-10-03 13:57:23 -07001548bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1549 uint16_t registers_size = code_item_->registers_size_;
1550 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1551 // integer overflow when adding them here.
1552 if (vA + vC > registers_size) {
1553 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1554 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 return false;
1556 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001557 return true;
1558}
1559
Brian Carlstrom75412882012-01-18 01:26:54 -08001560const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1561 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1562 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1563 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1564 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1565 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1566 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1567 gc_map.begin(),
1568 gc_map.end());
1569 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1570 DCHECK_EQ(gc_map.size(),
1571 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1572 (length_prefixed_gc_map->at(1) << 16) |
1573 (length_prefixed_gc_map->at(2) << 8) |
1574 (length_prefixed_gc_map->at(3) << 0)));
1575 return length_prefixed_gc_map;
1576}
1577
Ian Rogersd81871c2011-10-03 13:57:23 -07001578bool DexVerifier::VerifyCodeFlow() {
1579 uint16_t registers_size = code_item_->registers_size_;
1580 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001581
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001583 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1584 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 }
1586 /* Create and initialize table holding register status */
1587 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1588 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001589
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 work_line_.reset(new RegisterLine(registers_size, this));
1591 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001592
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 /* Initialize register types of method arguments. */
1594 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001595 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1596 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 return false;
1598 }
1599 /* Perform code flow verification. */
1600 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001601 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001603 }
1604
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001606 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1607 if (map.get() == NULL) {
1608 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 return false; // Not a real failure, but a failure to encode
1610 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001611#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001612 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001613#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001614 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1615 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1616 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001617
1618#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom75412882012-01-18 01:26:54 -08001619 method_->SetGcMap(&gc_map->at(0));
Logan Chienfca7e872011-12-20 20:08:22 +08001620#else
1621 /* Generate Inferred Register Category for LLVM-based Code Generator */
1622 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
1623 method_->SetInferredRegCategoryMap(table);
1624#endif
1625
jeffhaobdb76512011-09-07 11:43:16 -07001626 return true;
1627}
1628
Ian Rogersd81871c2011-10-03 13:57:23 -07001629void DexVerifier::Dump(std::ostream& os) {
1630 if (method_->IsNative()) {
1631 os << "Native method" << std::endl;
1632 return;
jeffhaobdb76512011-09-07 11:43:16 -07001633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 DCHECK(code_item_ != NULL);
1635 const Instruction* inst = Instruction::At(code_item_->insns_);
1636 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1637 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001638 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001639 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1641 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001642 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001643 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001644 inst = inst->Next();
1645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646}
1647
Ian Rogersd81871c2011-10-03 13:57:23 -07001648static bool IsPrimitiveDescriptor(char descriptor) {
1649 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001650 case 'I':
1651 case 'C':
1652 case 'S':
1653 case 'B':
1654 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001655 case 'F':
1656 case 'D':
1657 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001659 default:
1660 return false;
1661 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001662}
1663
Ian Rogersd81871c2011-10-03 13:57:23 -07001664bool DexVerifier::SetTypesFromSignature() {
1665 RegisterLine* reg_line = reg_table_.GetLine(0);
1666 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1667 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001668
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1670 //Include the "this" pointer.
1671 size_t cur_arg = 0;
1672 if (!method_->IsStatic()) {
1673 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1674 // argument as uninitialized. This restricts field access until the superclass constructor is
1675 // called.
1676 Class* declaring_class = method_->GetDeclaringClass();
1677 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1678 reg_line->SetRegisterType(arg_start + cur_arg,
1679 reg_types_.UninitializedThisArgument(declaring_class));
1680 } else {
1681 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001682 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001684 }
1685
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001686 const DexFile::ProtoId& proto_id =
1687 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001688 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689
1690 for (; iterator.HasNext(); iterator.Next()) {
1691 const char* descriptor = iterator.GetDescriptor();
1692 if (descriptor == NULL) {
1693 LOG(FATAL) << "Null descriptor";
1694 }
1695 if (cur_arg >= expected_args) {
1696 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1697 << " args, found more (" << descriptor << ")";
1698 return false;
1699 }
1700 switch (descriptor[0]) {
1701 case 'L':
1702 case '[':
1703 // We assume that reference arguments are initialized. The only way it could be otherwise
1704 // (assuming the caller was verified) is if the current method is <init>, but in that case
1705 // it's effectively considered initialized the instant we reach here (in the sense that we
1706 // can return without doing anything or call virtual methods).
1707 {
1708 const RegType& reg_type =
1709 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001710 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 }
1712 break;
1713 case 'Z':
1714 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1715 break;
1716 case 'C':
1717 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1718 break;
1719 case 'B':
1720 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1721 break;
1722 case 'I':
1723 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1724 break;
1725 case 'S':
1726 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1727 break;
1728 case 'F':
1729 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1730 break;
1731 case 'J':
1732 case 'D': {
1733 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1734 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1735 cur_arg++;
1736 break;
1737 }
1738 default:
1739 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1740 return false;
1741 }
1742 cur_arg++;
1743 }
1744 if (cur_arg != expected_args) {
1745 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1746 return false;
1747 }
1748 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1749 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1750 // format. Only major difference from the method argument format is that 'V' is supported.
1751 bool result;
1752 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1753 result = descriptor[1] == '\0';
1754 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1755 size_t i = 0;
1756 do {
1757 i++;
1758 } while (descriptor[i] == '['); // process leading [
1759 if (descriptor[i] == 'L') { // object array
1760 do {
1761 i++; // find closing ;
1762 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1763 result = descriptor[i] == ';';
1764 } else { // primitive array
1765 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1766 }
1767 } else if (descriptor[0] == 'L') {
1768 // could be more thorough here, but shouldn't be required
1769 size_t i = 0;
1770 do {
1771 i++;
1772 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1773 result = descriptor[i] == ';';
1774 } else {
1775 result = false;
1776 }
1777 if (!result) {
1778 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1779 << descriptor << "'";
1780 }
1781 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001782}
1783
Ian Rogersd81871c2011-10-03 13:57:23 -07001784bool DexVerifier::CodeFlowVerifyMethod() {
1785 const uint16_t* insns = code_item_->insns_;
1786 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001787
jeffhaobdb76512011-09-07 11:43:16 -07001788 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 insn_flags_[0].SetChanged();
1790 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001791
jeffhaobdb76512011-09-07 11:43:16 -07001792 /* Continue until no instructions are marked "changed". */
1793 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1795 uint32_t insn_idx = start_guess;
1796 for (; insn_idx < insns_size; insn_idx++) {
1797 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
1799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 if (insn_idx == insns_size) {
1801 if (start_guess != 0) {
1802 /* try again, starting from the top */
1803 start_guess = 0;
1804 continue;
1805 } else {
1806 /* all flags are clear */
1807 break;
1808 }
1809 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 // We carry the working set of registers from instruction to instruction. If this address can
1811 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1812 // "changed" flags, we need to load the set of registers from the table.
1813 // Because we always prefer to continue on to the next instruction, we should never have a
1814 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1815 // target.
1816 work_insn_idx_ = insn_idx;
1817 if (insn_flags_[insn_idx].IsBranchTarget()) {
1818 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001819 } else {
1820#ifndef NDEBUG
1821 /*
1822 * Sanity check: retrieve the stored register line (assuming
1823 * a full table) and make sure it actually matches.
1824 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1826 if (register_line != NULL) {
1827 if (work_line_->CompareLine(register_line) != 0) {
1828 Dump(std::cout);
1829 std::cout << info_messages_.str();
1830 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1831 << "@" << (void*)work_insn_idx_ << std::endl
1832 << " work_line=" << *work_line_ << std::endl
1833 << " expected=" << *register_line;
1834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 }
1836#endif
1837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 if (!CodeFlowVerifyInstruction(&start_guess)) {
1839 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001840 return false;
1841 }
jeffhaobdb76512011-09-07 11:43:16 -07001842 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 insn_flags_[insn_idx].SetVisited();
1844 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001845 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001846
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001848 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001849 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001850 * (besides the wasted space), but it indicates a flaw somewhere
1851 * down the line, possibly in the verifier.
1852 *
1853 * If we've substituted "always throw" instructions into the stream,
1854 * we are almost certainly going to have some dead code.
1855 */
1856 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 uint32_t insn_idx = 0;
1858 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001859 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001860 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001861 * may or may not be preceded by a padding NOP (for alignment).
1862 */
1863 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1864 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1865 insns[insn_idx] == Instruction::kArrayDataSignature ||
1866 (insns[insn_idx] == Instruction::NOP &&
1867 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1868 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1869 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001871 }
1872
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001874 if (dead_start < 0)
1875 dead_start = insn_idx;
1876 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001878 dead_start = -1;
1879 }
1880 }
1881 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001883 }
1884 }
jeffhaobdb76512011-09-07 11:43:16 -07001885 return true;
1886}
1887
Ian Rogersd81871c2011-10-03 13:57:23 -07001888bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001889#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001891 gDvm.verifierStats.instrsReexamined++;
1892 } else {
1893 gDvm.verifierStats.instrsExamined++;
1894 }
1895#endif
1896
1897 /*
1898 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001899 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001900 * control to another statement:
1901 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001902 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001903 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001904 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001905 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001906 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001907 * throw an exception that is handled by an encompassing "try"
1908 * block.
1909 *
1910 * We can also return, in which case there is no successor instruction
1911 * from this point.
1912 *
1913 * The behavior can be determined from the OpcodeFlags.
1914 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1916 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001917 Instruction::DecodedInstruction dec_insn(inst);
1918 int opcode_flag = inst->Flag();
1919
jeffhaobdb76512011-09-07 11:43:16 -07001920 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001921 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001922 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001924 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1925 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927
1928 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001929 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001930 * can throw an exception, we will copy/merge this into the "catch"
1931 * address rather than work_line, because we don't want the result
1932 * from the "successful" code path (e.g. a check-cast that "improves"
1933 * a type) to be visible to the exception handler.
1934 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1936 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001937 } else {
1938#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001940#endif
1941 }
1942
1943 switch (dec_insn.opcode_) {
1944 case Instruction::NOP:
1945 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001947 * a signature that looks like a NOP; if we see one of these in
1948 * the course of executing code then we have a problem.
1949 */
1950 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001952 }
1953 break;
1954
1955 case Instruction::MOVE:
1956 case Instruction::MOVE_FROM16:
1957 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001959 break;
1960 case Instruction::MOVE_WIDE:
1961 case Instruction::MOVE_WIDE_FROM16:
1962 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001964 break;
1965 case Instruction::MOVE_OBJECT:
1966 case Instruction::MOVE_OBJECT_FROM16:
1967 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
1970
1971 /*
1972 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001973 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001974 * might want to hold the result in an actual CPU register, so the
1975 * Dalvik spec requires that these only appear immediately after an
1976 * invoke or filled-new-array.
1977 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001978 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001979 * redundant with the reset done below, but it can make the debug info
1980 * easier to read in some cases.)
1981 */
1982 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001984 break;
1985 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
1988 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
1991
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001993 /*
jeffhao60f83e32012-02-13 17:16:30 -08001994 * This statement can only appear as the first instruction in an exception handler. We verify
1995 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001996 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001997 const RegType& res_type = GetCaughtExceptionType();
1998 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001999 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2003 if (!GetMethodReturnType().IsUnknown()) {
2004 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
2005 }
jeffhaobdb76512011-09-07 11:43:16 -07002006 }
2007 break;
2008 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002010 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 const RegType& return_type = GetMethodReturnType();
2012 if (!return_type.IsCategory1Types()) {
2013 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
2014 } else {
2015 // Compilers may generate synthetic functions that write byte values into boolean fields.
2016 // Also, it may use integer values for boolean, byte, short, and character return types.
2017 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
2018 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2019 ((return_type.IsBoolean() || return_type.IsByte() ||
2020 return_type.IsShort() || return_type.IsChar()) &&
2021 src_type.IsInteger()));
2022 /* check the register contents */
2023 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
2024 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002025 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 }
jeffhaobdb76512011-09-07 11:43:16 -07002027 }
2028 }
2029 break;
2030 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002032 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 const RegType& return_type = GetMethodReturnType();
2034 if (!return_type.IsCategory2Types()) {
2035 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
2036 } else {
2037 /* check the register contents */
2038 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
2039 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002040 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
jeffhaobdb76512011-09-07 11:43:16 -07002042 }
2043 }
2044 break;
2045 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2047 const RegType& return_type = GetMethodReturnType();
2048 if (!return_type.IsReferenceTypes()) {
2049 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2050 } else {
2051 /* return_type is the *expected* return type, not register value */
2052 DCHECK(!return_type.IsZero());
2053 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002054 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2055 // Disallow returning uninitialized values and verify that the reference in vAA is an
2056 // instance of the "return_type"
2057 if (reg_type.IsUninitializedTypes()) {
2058 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2059 } else if (!return_type.IsAssignableFrom(reg_type)) {
2060 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2061 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002062 }
2063 }
2064 }
2065 break;
2066
2067 case Instruction::CONST_4:
2068 case Instruction::CONST_16:
2069 case Instruction::CONST:
2070 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073 case Instruction::CONST_HIGH16:
2074 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 work_line_->SetRegisterType(dec_insn.vA_,
2076 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::CONST_WIDE_16:
2079 case Instruction::CONST_WIDE_32:
2080 case Instruction::CONST_WIDE:
2081 case Instruction::CONST_WIDE_HIGH16:
2082 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::CONST_STRING:
2086 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002090 // Get type from instruction if unresolved then we need an access check
2091 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2092 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2093 // Register holds class, ie its type is class, but on error we keep it Unknown
2094 work_line_->SetRegisterType(dec_insn.vA_,
2095 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 }
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::MONITOR_EXIT:
2102 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002103 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002104 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002105 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002106 * to the need to handle asynchronous exceptions, a now-deprecated
2107 * feature that Dalvik doesn't support.)
2108 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002109 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002110 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002111 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002112 * structured locking checks are working, the former would have
2113 * failed on the -enter instruction, and the latter is impossible.
2114 *
2115 * This is fortunate, because issue 3221411 prevents us from
2116 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002117 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002118 * some catch blocks (which will show up as "dead" code when
2119 * we skip them here); if we can't, then the code path could be
2120 * "live" so we still need to check it.
2121 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 opcode_flag &= ~Instruction::kThrow;
2123 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125
Ian Rogers28ad40d2011-10-27 15:19:26 -07002126 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002128 /*
2129 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2130 * could be a "upcast" -- not expected, so we don't try to address it.)
2131 *
2132 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2133 * dec_insn.vA_ when branching to a handler.
2134 */
2135 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2136 const RegType& res_type =
2137 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002138 if (res_type.IsUnknown()) {
2139 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2140 break; // couldn't resolve class
2141 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002142 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2143 const RegType& orig_type =
2144 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2145 if (!res_type.IsNonZeroReferenceTypes()) {
2146 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2147 } else if (!orig_type.IsReferenceTypes()) {
2148 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002149 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002150 if (is_checkcast) {
2151 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002154 }
jeffhaobdb76512011-09-07 11:43:16 -07002155 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002156 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 }
2158 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002159 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2160 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002161 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002162 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 } else {
2164 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2165 }
2166 }
2167 break;
2168 }
2169 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002170 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
jeffhao8cd6dda2012-02-22 10:15:34 -08002171 if (res_type.IsUnknown()) {
2172 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2173 break; // couldn't resolve class
2174 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002175 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2176 // can't create an instance of an interface or abstract class */
2177 if (!res_type.IsInstantiableTypes()) {
2178 Fail(VERIFY_ERROR_INSTANTIATION)
2179 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002181 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2182 // Any registers holding previous allocations from this address that have not yet been
2183 // initialized must be marked invalid.
2184 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2185 // add the new uninitialized reference to the register state
2186 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 }
2188 break;
2189 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002190 case Instruction::NEW_ARRAY:
2191 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
2193 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002194 VerifyNewArray(dec_insn, true, false);
2195 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002197 case Instruction::FILLED_NEW_ARRAY_RANGE:
2198 VerifyNewArray(dec_insn, true, true);
2199 just_set_result = true; // Filled new array range sets result register
2200 break;
jeffhaobdb76512011-09-07 11:43:16 -07002201 case Instruction::CMPL_FLOAT:
2202 case Instruction::CMPG_FLOAT:
jeffhao457cc512012-02-02 16:55:13 -08002203 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float())) {
2204 break;
2205 }
2206 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float())) {
2207 break;
2208 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::CMPL_DOUBLE:
2212 case Instruction::CMPG_DOUBLE:
jeffhao457cc512012-02-02 16:55:13 -08002213 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double())) {
2214 break;
2215 }
2216 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double())) {
2217 break;
2218 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221 case Instruction::CMP_LONG:
jeffhao457cc512012-02-02 16:55:13 -08002222 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long())) {
2223 break;
2224 }
2225 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long())) {
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;
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002231 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2232 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2233 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002234 }
2235 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 }
jeffhaobdb76512011-09-07 11:43:16 -07002237 case Instruction::GOTO:
2238 case Instruction::GOTO_16:
2239 case Instruction::GOTO_32:
2240 /* no effect on or use of registers */
2241 break;
2242
2243 case Instruction::PACKED_SWITCH:
2244 case Instruction::SPARSE_SWITCH:
2245 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 case Instruction::FILL_ARRAY_DATA: {
2250 /* Similar to the verification done for APUT */
Ian Rogers89310de2012-02-01 13:47:30 -08002251 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA_);
2252 /* array_type can be null if the reg type is Zero */
2253 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002254 if (!array_type.IsArrayTypes()) {
2255 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002256 } else {
jeffhao457cc512012-02-02 16:55:13 -08002257 const RegType& component_type = reg_types_.GetComponentType(array_type,
2258 method_->GetDeclaringClass()->GetClassLoader());
2259 DCHECK(!component_type.IsUnknown());
2260 if (component_type.IsNonZeroReferenceTypes()) {
2261 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with component type "
2262 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 } else {
jeffhao457cc512012-02-02 16:55:13 -08002264 // Now verify if the element width in the table matches the element width declared in
2265 // the array
2266 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2267 if (array_data[0] != Instruction::kArrayDataSignature) {
2268 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2269 } else {
2270 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2271 // Since we don't compress the data in Dex, expect to see equal width of data stored
2272 // in the table and expected from the array class.
2273 if (array_data[1] != elem_width) {
2274 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2275 << " vs " << elem_width << ")";
2276 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 }
2278 }
jeffhaobdb76512011-09-07 11:43:16 -07002279 }
2280 }
2281 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 }
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 case Instruction::IF_NE: {
2285 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2286 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2287 bool mismatch = false;
2288 if (reg_type1.IsZero()) { // zero then integral or reference expected
2289 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2290 } else if (reg_type1.IsReferenceTypes()) { // both references?
2291 mismatch = !reg_type2.IsReferenceTypes();
2292 } else { // both integral?
2293 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2294 }
2295 if (mismatch) {
2296 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2297 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002298 }
2299 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 }
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::IF_LT:
2302 case Instruction::IF_GE:
2303 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 case Instruction::IF_LE: {
2305 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2306 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2307 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2308 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2309 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002310 }
2311 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
jeffhaobdb76512011-09-07 11:43:16 -07002313 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 case Instruction::IF_NEZ: {
2315 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2316 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2317 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2318 }
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 }
jeffhaobdb76512011-09-07 11:43:16 -07002321 case Instruction::IF_LTZ:
2322 case Instruction::IF_GEZ:
2323 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 case Instruction::IF_LEZ: {
2325 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2326 if (!reg_type.IsIntegralTypes()) {
2327 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2328 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2329 }
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 }
jeffhaobdb76512011-09-07 11:43:16 -07002332 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2334 break;
jeffhaobdb76512011-09-07 11:43:16 -07002335 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2337 break;
jeffhaobdb76512011-09-07 11:43:16 -07002338 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 VerifyAGet(dec_insn, reg_types_.Char(), true);
2340 break;
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 case Instruction::AGET:
2345 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 VerifyAGet(dec_insn, reg_types_.Long(), true);
2349 break;
2350 case Instruction::AGET_OBJECT:
2351 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 case Instruction::APUT_BOOLEAN:
2355 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2356 break;
2357 case Instruction::APUT_BYTE:
2358 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2359 break;
2360 case Instruction::APUT_CHAR:
2361 VerifyAPut(dec_insn, reg_types_.Char(), true);
2362 break;
2363 case Instruction::APUT_SHORT:
2364 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002377 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 break;
jeffhaobdb76512011-09-07 11:43:16 -07002379 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002380 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 break;
jeffhaobdb76512011-09-07 11:43:16 -07002382 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002383 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
jeffhaobdb76512011-09-07 11:43:16 -07002385 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002386 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
2388 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002389 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002392 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002395 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 break;
jeffhaobdb76512011-09-07 11:43:16 -07002397
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002399 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
2401 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002402 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 break;
2404 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002405 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 break;
2407 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002408 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002411 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002414 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 break;
jeffhaobdb76512011-09-07 11:43:16 -07002416 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002417 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419
jeffhaobdb76512011-09-07 11:43:16 -07002420 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002421 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 break;
jeffhaobdb76512011-09-07 11:43:16 -07002423 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002424 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 break;
jeffhaobdb76512011-09-07 11:43:16 -07002426 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002427 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 break;
jeffhaobdb76512011-09-07 11:43:16 -07002429 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002430 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 break;
2432 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002433 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002436 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002439 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 break;
2441
2442 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002443 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 break;
2445 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002446 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 break;
2448 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002449 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 break;
2451 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002452 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002455 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002458 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002459 break;
2460 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002461 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463
2464 case Instruction::INVOKE_VIRTUAL:
2465 case Instruction::INVOKE_VIRTUAL_RANGE:
2466 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 case Instruction::INVOKE_SUPER_RANGE: {
2468 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2469 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2470 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2471 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2472 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2473 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002474 const char* descriptor;
2475 if (called_method == NULL) {
2476 uint32_t method_idx = dec_insn.vB_;
2477 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2478 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002479 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002481 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002482 }
Ian Rogers9074b992011-10-26 17:41:55 -07002483 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002486 just_set_result = true;
2487 }
2488 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 }
jeffhaobdb76512011-09-07 11:43:16 -07002490 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 case Instruction::INVOKE_DIRECT_RANGE: {
2492 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2493 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2494 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002495 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 * Some additional checks when calling a constructor. We know from the invocation arg check
2497 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2498 * that to require that called_method->klass is the same as this->klass or this->super,
2499 * allowing the latter only if the "this" argument is the same as the "this" argument to
2500 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002501 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002502 bool is_constructor;
2503 if (called_method != NULL) {
2504 is_constructor = called_method->IsConstructor();
2505 } else {
2506 uint32_t method_idx = dec_insn.vB_;
2507 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2508 const char* name = dex_file_->GetMethodName(method_id);
2509 is_constructor = strcmp(name, "<init>") == 0;
2510 }
2511 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2513 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515
2516 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 if (this_type.IsZero()) {
2518 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002521 if (called_method != NULL) {
2522 Class* this_class = this_type.GetClass();
2523 DCHECK(this_class != NULL);
2524 /* must be in same class or in superclass */
2525 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2526 if (this_class != method_->GetDeclaringClass()) {
2527 Fail(VERIFY_ERROR_GENERIC)
2528 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2529 break;
2530 }
2531 } else if (called_method->GetDeclaringClass() != this_class) {
2532 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 }
jeffhaobdb76512011-09-07 11:43:16 -07002535 }
2536
2537 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002538 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2540 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 }
2543
2544 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002545 * Replace the uninitialized reference with an initialized one. We need to do this for all
2546 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002547 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 work_line_->MarkRefsAsInitialized(this_type);
2549 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002551 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002552 const char* descriptor;
2553 if (called_method == NULL) {
2554 uint32_t method_idx = dec_insn.vB_;
2555 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2556 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002557 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002559 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002560 }
Ian Rogers9074b992011-10-26 17:41:55 -07002561 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002562 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002564 just_set_result = true;
2565 }
2566 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 }
jeffhaobdb76512011-09-07 11:43:16 -07002568 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 case Instruction::INVOKE_STATIC_RANGE: {
2570 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2571 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2572 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 const char* descriptor;
2574 if (called_method == NULL) {
2575 uint32_t method_idx = dec_insn.vB_;
2576 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2577 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002578 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002580 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002581 }
Ian Rogers9074b992011-10-26 17:41:55 -07002582 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002583 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 work_line_->SetResultRegisterType(return_type);
2585 just_set_result = true;
2586 }
jeffhaobdb76512011-09-07 11:43:16 -07002587 }
2588 break;
2589 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 case Instruction::INVOKE_INTERFACE_RANGE: {
2591 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2592 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2593 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002594 if (abs_method != NULL) {
2595 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002596 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002597 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2598 << PrettyMethod(abs_method) << "'";
2599 break;
2600 }
2601 }
2602 /* Get the type of the "this" arg, which should either be a sub-interface of called
2603 * interface or Object (see comments in RegType::JoinClass).
2604 */
2605 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2606 if (failure_ == VERIFY_ERROR_NONE) {
2607 if (this_type.IsZero()) {
2608 /* null pointer always passes (and always fails at runtime) */
2609 } else {
2610 if (this_type.IsUninitializedTypes()) {
2611 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2612 << this_type;
2613 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002615 // In the past we have tried to assert that "called_interface" is assignable
2616 // from "this_type.GetClass()", however, as we do an imprecise Join
2617 // (RegType::JoinClass) we don't have full information on what interfaces are
2618 // implemented by "this_type". For example, two classes may implement the same
2619 // interfaces and have a common parent that doesn't implement the interface. The
2620 // join will set "this_type" to the parent class and a test that this implements
2621 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002622 }
2623 }
jeffhaobdb76512011-09-07 11:43:16 -07002624 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 * We don't have an object instance, so we can't find the concrete method. However, all of
2626 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002627 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002628 const char* descriptor;
2629 if (abs_method == NULL) {
2630 uint32_t method_idx = dec_insn.vB_;
2631 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2632 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002633 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002634 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002635 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002636 }
Ian Rogers9074b992011-10-26 17:41:55 -07002637 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002638 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2639 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002641 just_set_result = true;
2642 }
2643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 }
jeffhaobdb76512011-09-07 11:43:16 -07002645 case Instruction::NEG_INT:
2646 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::NEG_LONG:
2650 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002691 break;
2692 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704
2705 case Instruction::ADD_INT:
2706 case Instruction::SUB_INT:
2707 case Instruction::MUL_INT:
2708 case Instruction::REM_INT:
2709 case Instruction::DIV_INT:
2710 case Instruction::SHL_INT:
2711 case Instruction::SHR_INT:
2712 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::AND_INT:
2716 case Instruction::OR_INT:
2717 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002719 break;
2720 case Instruction::ADD_LONG:
2721 case Instruction::SUB_LONG:
2722 case Instruction::MUL_LONG:
2723 case Instruction::DIV_LONG:
2724 case Instruction::REM_LONG:
2725 case Instruction::AND_LONG:
2726 case Instruction::OR_LONG:
2727 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730 case Instruction::SHL_LONG:
2731 case Instruction::SHR_LONG:
2732 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 /* shift distance is Int, making these different from other binary operations */
2734 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002735 break;
2736 case Instruction::ADD_FLOAT:
2737 case Instruction::SUB_FLOAT:
2738 case Instruction::MUL_FLOAT:
2739 case Instruction::DIV_FLOAT:
2740 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743 case Instruction::ADD_DOUBLE:
2744 case Instruction::SUB_DOUBLE:
2745 case Instruction::MUL_DOUBLE:
2746 case Instruction::DIV_DOUBLE:
2747 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::ADD_INT_2ADDR:
2751 case Instruction::SUB_INT_2ADDR:
2752 case Instruction::MUL_INT_2ADDR:
2753 case Instruction::REM_INT_2ADDR:
2754 case Instruction::SHL_INT_2ADDR:
2755 case Instruction::SHR_INT_2ADDR:
2756 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::AND_INT_2ADDR:
2760 case Instruction::OR_INT_2ADDR:
2761 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::ADD_LONG_2ADDR:
2768 case Instruction::SUB_LONG_2ADDR:
2769 case Instruction::MUL_LONG_2ADDR:
2770 case Instruction::DIV_LONG_2ADDR:
2771 case Instruction::REM_LONG_2ADDR:
2772 case Instruction::AND_LONG_2ADDR:
2773 case Instruction::OR_LONG_2ADDR:
2774 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::SHL_LONG_2ADDR:
2778 case Instruction::SHR_LONG_2ADDR:
2779 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::ADD_FLOAT_2ADDR:
2783 case Instruction::SUB_FLOAT_2ADDR:
2784 case Instruction::MUL_FLOAT_2ADDR:
2785 case Instruction::DIV_FLOAT_2ADDR:
2786 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
2789 case Instruction::ADD_DOUBLE_2ADDR:
2790 case Instruction::SUB_DOUBLE_2ADDR:
2791 case Instruction::MUL_DOUBLE_2ADDR:
2792 case Instruction::DIV_DOUBLE_2ADDR:
2793 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002795 break;
2796 case Instruction::ADD_INT_LIT16:
2797 case Instruction::RSUB_INT:
2798 case Instruction::MUL_INT_LIT16:
2799 case Instruction::DIV_INT_LIT16:
2800 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002802 break;
2803 case Instruction::AND_INT_LIT16:
2804 case Instruction::OR_INT_LIT16:
2805 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808 case Instruction::ADD_INT_LIT8:
2809 case Instruction::RSUB_INT_LIT8:
2810 case Instruction::MUL_INT_LIT8:
2811 case Instruction::DIV_INT_LIT8:
2812 case Instruction::REM_INT_LIT8:
2813 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002814 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002815 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002817 break;
2818 case Instruction::AND_INT_LIT8:
2819 case Instruction::OR_INT_LIT8:
2820 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002822 break;
2823
2824 /*
2825 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002826 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002827 * inserted in the course of verification, we can expect to see it here.
2828 */
jeffhaob4df5142011-09-19 20:25:32 -07002829 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002833 case Instruction::UNUSED_EE:
2834 case Instruction::UNUSED_EF:
2835 case Instruction::UNUSED_F2:
2836 case Instruction::UNUSED_F3:
2837 case Instruction::UNUSED_F4:
2838 case Instruction::UNUSED_F5:
2839 case Instruction::UNUSED_F6:
2840 case Instruction::UNUSED_F7:
2841 case Instruction::UNUSED_F8:
2842 case Instruction::UNUSED_F9:
2843 case Instruction::UNUSED_FA:
2844 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::UNUSED_F0:
2846 case Instruction::UNUSED_F1:
2847 case Instruction::UNUSED_E3:
2848 case Instruction::UNUSED_E8:
2849 case Instruction::UNUSED_E7:
2850 case Instruction::UNUSED_E4:
2851 case Instruction::UNUSED_E9:
2852 case Instruction::UNUSED_FC:
2853 case Instruction::UNUSED_E5:
2854 case Instruction::UNUSED_EA:
2855 case Instruction::UNUSED_FD:
2856 case Instruction::UNUSED_E6:
2857 case Instruction::UNUSED_EB:
2858 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002859 case Instruction::UNUSED_3E:
2860 case Instruction::UNUSED_3F:
2861 case Instruction::UNUSED_40:
2862 case Instruction::UNUSED_41:
2863 case Instruction::UNUSED_42:
2864 case Instruction::UNUSED_43:
2865 case Instruction::UNUSED_73:
2866 case Instruction::UNUSED_79:
2867 case Instruction::UNUSED_7A:
2868 case Instruction::UNUSED_EC:
2869 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002870 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872
2873 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002874 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002875 * complain if an instruction is missing (which is desirable).
2876 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002878
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 if (failure_ != VERIFY_ERROR_NONE) {
2880 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002881 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002882 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002883 return false;
2884 } else {
2885 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002886 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002888 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002890 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002892 opcode_flag = Instruction::kThrow;
2893 }
2894 }
jeffhaobdb76512011-09-07 11:43:16 -07002895 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 * If we didn't just set the result register, clear it out. This ensures that you can only use
2897 * "move-result" immediately after the result is set. (We could check this statically, but it's
2898 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002899 */
2900 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002902 }
2903
jeffhaoa0a764a2011-09-16 10:43:38 -07002904 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002905 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2907 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2908 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002909 return false;
2910 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2912 // next instruction isn't one.
2913 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002914 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 }
2916 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2917 if (next_line != NULL) {
2918 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2919 // needed.
2920 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002921 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 }
jeffhaobdb76512011-09-07 11:43:16 -07002923 } else {
2924 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 * We're not recording register data for the next instruction, so we don't know what the prior
2926 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002927 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002929 }
2930 }
2931
2932 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002933 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002934 *
2935 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002936 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002937 * somebody could get a reference field, check it for zero, and if the
2938 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002939 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002940 * that, and will reject the code.
2941 *
2942 * TODO: avoid re-fetching the branch target
2943 */
2944 if ((opcode_flag & Instruction::kBranch) != 0) {
2945 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002947 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002949 return false;
2950 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002951 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
jeffhaobdb76512011-09-07 11:43:16 -07002955 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002957 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 }
jeffhaobdb76512011-09-07 11:43:16 -07002959 }
2960
2961 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002962 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002963 *
2964 * We've already verified that the table is structurally sound, so we
2965 * just need to walk through and tag the targets.
2966 */
2967 if ((opcode_flag & Instruction::kSwitch) != 0) {
2968 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2969 const uint16_t* switch_insns = insns + offset_to_switch;
2970 int switch_count = switch_insns[1];
2971 int offset_to_targets, targ;
2972
2973 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2974 /* 0 = sig, 1 = count, 2/3 = first key */
2975 offset_to_targets = 4;
2976 } else {
2977 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002978 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002979 offset_to_targets = 2 + 2 * switch_count;
2980 }
2981
2982 /* verify each switch target */
2983 for (targ = 0; targ < switch_count; targ++) {
2984 int offset;
2985 uint32_t abs_offset;
2986
2987 /* offsets are 32-bit, and only partly endian-swapped */
2988 offset = switch_insns[offset_to_targets + targ * 2] |
2989 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 abs_offset = work_insn_idx_ + offset;
2991 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2992 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002993 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 }
2995 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002996 return false;
2997 }
2998 }
2999
3000 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3002 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003003 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
3005 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003006 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003007
Ian Rogers0571d352011-11-03 19:51:38 -07003008 for (; iterator.HasNext(); iterator.Next()) {
3009 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 within_catch_all = true;
3011 }
jeffhaobdb76512011-09-07 11:43:16 -07003012 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3014 * "work_regs", because at runtime the exception will be thrown before the instruction
3015 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003016 */
Ian Rogers0571d352011-11-03 19:51:38 -07003017 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003018 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
jeffhaobdb76512011-09-07 11:43:16 -07003020 }
3021
3022 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3024 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003025 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003027 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 * The state in work_line reflects the post-execution state. If the current instruction is a
3029 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003030 * it will do so before grabbing the lock).
3031 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
3033 Fail(VERIFY_ERROR_GENERIC)
3034 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003035 return false;
3036 }
3037 }
3038 }
3039
jeffhaod1f0fde2011-09-08 17:25:33 -07003040 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 if ((opcode_flag & Instruction::kReturn) != 0) {
3042 if(!work_line_->VerifyMonitorStackEmpty()) {
3043 return false;
3044 }
jeffhaobdb76512011-09-07 11:43:16 -07003045 }
3046
3047 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003048 * Update start_guess. Advance to the next instruction of that's
3049 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003050 * neither of those exists we're in a return or throw; leave start_guess
3051 * alone and let the caller sort it out.
3052 */
3053 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003055 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3056 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003058 }
3059
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3061 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003062
3063 return true;
3064}
3065
Ian Rogers28ad40d2011-10-27 15:19:26 -07003066const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003067 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003068 Class* referrer = method_->GetDeclaringClass();
3069 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3070 const RegType& result =
3071 klass != NULL ? reg_types_.FromClass(klass)
3072 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3073 if (klass == NULL && !result.IsUnresolvedTypes()) {
3074 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003076 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3077 // check at runtime if access is allowed and so pass here.
3078 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3079 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003080 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003081 << result << "'";
3082 return reg_types_.Unknown();
3083 } else {
3084 return result;
3085 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003086}
3087
Ian Rogers28ad40d2011-10-27 15:19:26 -07003088const RegType& DexVerifier::GetCaughtExceptionType() {
3089 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003090 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003091 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3093 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003094 CatchHandlerIterator iterator(handlers_ptr);
3095 for (; iterator.HasNext(); iterator.Next()) {
3096 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3097 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003098 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003100 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003101 if (common_super == NULL) {
3102 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3103 // as that is caught at runtime
3104 common_super = &exception;
3105 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3106 // We don't know enough about the type and the common path merge will result in
3107 // Conflict. Fail here knowing the correct thing can be done at runtime.
Ian Rogers28ad40d2011-10-27 15:19:26 -07003108 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3109 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003110 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003111 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003113 common_super = &common_super->Merge(exception, &reg_types_);
3114 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 }
3116 }
3117 }
3118 }
Ian Rogers0571d352011-11-03 19:51:38 -07003119 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003120 }
3121 }
3122 if (common_super == NULL) {
3123 /* no catch blocks, or no catches with classes we can find */
3124 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003125 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003127 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003128}
3129
jeffhao8cd6dda2012-02-22 10:15:34 -08003130Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003131 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3132 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3133 if (failure_ != VERIFY_ERROR_NONE) {
3134 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3135 return NULL;
3136 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003137 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003138 return NULL; // Can't resolve Class so no more to do here
3139 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003140 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 Class* referrer = method_->GetDeclaringClass();
3142 DexCache* dex_cache = referrer->GetDexCache();
3143 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3144 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003145 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003146 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003147
3148 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003150 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 res_method = klass->FindInterfaceMethod(name, signature);
3152 } else {
3153 res_method = klass->FindVirtualMethod(name, signature);
3154 }
3155 if (res_method != NULL) {
3156 dex_cache->SetResolvedMethod(method_idx, res_method);
3157 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003158 // If a virtual or interface method wasn't found with the expected type, look in
3159 // the direct methods. This can happen when the wrong invoke type is used or when
3160 // a class has changed, and will be flagged as an error in later checks.
3161 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3162 res_method = klass->FindDirectMethod(name, signature);
3163 }
3164 if (res_method == NULL) {
3165 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3166 << PrettyDescriptor(klass) << "." << name
3167 << " " << signature;
3168 return NULL;
3169 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 }
3171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3173 // enforce them here.
3174 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3175 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3176 << PrettyMethod(res_method);
3177 return NULL;
3178 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003179 // Disallow any calls to class initializers.
3180 if (MethodHelper(res_method).IsClassInitializer()) {
3181 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to class initializer "
3182 << PrettyMethod(res_method);
3183 return NULL;
3184 }
3185 // Check that invoke-virtual and invoke-super are not used on private methods.
3186 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
3187 Fail(VERIFY_ERROR_GENERIC) << "invoke-super/virtual can't be used on private method "
3188 << PrettyMethod(res_method);
3189 return NULL;
3190 }
3191 // Check if access is allowed.
3192 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3193 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3194 << " from " << PrettyDescriptor(referrer) << ")";
3195 return NULL;
3196 }
3197 // Check that interface methods match interface classes.
3198 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3199 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3200 << " is in an interface class " << PrettyClass(klass);
3201 return NULL;
3202 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3203 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3204 << " is in a non-interface class " << PrettyClass(klass);
3205 return NULL;
3206 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 // See if the method type implied by the invoke instruction matches the access flags for the
3208 // target method.
3209 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3210 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3211 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3212 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003213 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3214 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 return NULL;
3216 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003217 return res_method;
3218}
3219
3220Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3221 MethodType method_type, bool is_range, bool is_super) {
3222 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3223 // we're making.
3224 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_, method_type);
3225 if (res_method == NULL) { // error or class is unresolved
3226 return NULL;
3227 }
3228
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3230 // has a vtable entry for the target method.
3231 if (is_super) {
3232 DCHECK(method_type == METHOD_VIRTUAL);
3233 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003234 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003235 if (super == NULL) { // Only Object has no super class
3236 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3237 << " to super " << PrettyMethod(res_method);
3238 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003239 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003241 << " to super " << PrettyDescriptor(super)
3242 << "." << mh.GetName()
3243 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003244 }
3245 return NULL;
3246 }
3247 }
3248 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3249 // match the call to the signature. Also, we might might be calling through an abstract method
3250 // definition (which doesn't have register count values).
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003251 size_t expected_args = dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 /* caught by static verifier */
3253 DCHECK(is_range || expected_args <= 5);
3254 if (expected_args > code_item_->outs_size_) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003255 Fail(VERIFY_ERROR_GENERIC) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3257 return NULL;
3258 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003259
jeffhaobdb76512011-09-07 11:43:16 -07003260 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 * Check the "this" argument, which must be an instance of the class
3262 * that declared the method. For an interface class, we don't do the
3263 * full interface merge, so we can't do a rigorous check here (which
3264 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003265 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003266 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 if (!res_method->IsStatic()) {
3268 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3269 if (failure_ != VERIFY_ERROR_NONE) {
3270 return NULL;
3271 }
3272 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3273 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3274 return NULL;
3275 }
3276 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003277 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3278 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003279 Fail(VERIFY_ERROR_GENERIC) << "'this' argument '" << actual_arg_type
3280 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 return NULL;
3282 }
3283 }
3284 actual_args++;
3285 }
3286 /*
3287 * Process the target method's signature. This signature may or may not
3288 * have been verified, so we can't assume it's properly formed.
3289 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003290 MethodHelper mh(res_method);
3291 const DexFile::TypeList* params = mh.GetParameterTypeList();
3292 size_t params_size = params == NULL ? 0 : params->Size();
3293 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 if (actual_args >= expected_args) {
3295 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003296 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3297 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 return NULL;
3299 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003300 const char* descriptor =
3301 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3302 if (descriptor == NULL) {
3303 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3304 << " missing signature component";
3305 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 }
3307 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003308 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003309 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3310 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3311 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 }
3313 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3314 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 if (actual_args != expected_args) {
3316 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003317 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 return NULL;
3319 } else {
3320 return res_method;
3321 }
3322}
3323
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003324const RegType& DexVerifier::GetMethodReturnType() {
3325 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3326 MethodHelper(method_).GetReturnTypeDescriptor());
3327}
3328
Ian Rogers0c4a5062012-02-03 15:18:59 -08003329void DexVerifier::VerifyNewArray(const Instruction::DecodedInstruction& dec_insn, bool is_filled,
3330 bool is_range) {
3331 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB_ : dec_insn.vC_);
3332 if (res_type.IsUnknown()) {
3333 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3334 } else {
3335 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3336 if (!res_type.IsArrayTypes()) {
3337 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
3338 } else if (!is_filled) {
3339 /* make sure "size" register is valid type */
3340 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
3341 /* set register type to array class */
3342 work_line_->SetRegisterType(dec_insn.vA_, res_type);
3343 } else {
3344 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3345 // the list and fail. It's legal, if silly, for arg_count to be zero.
3346 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3347 method_->GetDeclaringClass()->GetClassLoader());
3348 uint32_t arg_count = dec_insn.vA_;
3349 for (size_t ui = 0; ui < arg_count; ui++) {
3350 uint32_t get_reg = is_range ? dec_insn.vC_ + ui : dec_insn.arg_[ui];
3351 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3352 work_line_->SetResultRegisterType(reg_types_.Unknown());
3353 return;
3354 }
3355 }
3356 // filled-array result goes into "result" register
3357 work_line_->SetResultRegisterType(res_type);
3358 }
3359 }
3360}
3361
Ian Rogersd81871c2011-10-03 13:57:23 -07003362void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3363 const RegType& insn_type, bool is_primitive) {
3364 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3365 if (!index_type.IsArrayIndexTypes()) {
3366 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3367 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003368 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3369 if (array_type.IsZero()) {
3370 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3371 // instruction type. TODO: have a proper notion of bottom here.
3372 if (!is_primitive || insn_type.IsCategory1Types()) {
3373 // Reference or category 1
3374 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003376 // Category 2
3377 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3378 }
jeffhaofc3144e2012-02-01 17:21:15 -08003379 } else if (!array_type.IsArrayTypes()) {
3380 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003381 } else {
3382 /* verify the class */
3383 const RegType& component_type = reg_types_.GetComponentType(array_type,
3384 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003385 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003386 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3387 << " source for aget-object";
3388 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3389 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3390 << " source for category 1 aget";
3391 } else if (is_primitive && !insn_type.Equals(component_type) &&
3392 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3393 (insn_type.IsLong() && component_type.IsDouble()))) {
3394 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003396 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 // Use knowledge of the field type which is stronger than the type inferred from the
3398 // instruction, which can't differentiate object types and ints from floats, longs from
3399 // doubles.
3400 work_line_->SetRegisterType(dec_insn.vA_, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 }
3402 }
3403 }
3404}
3405
3406void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3407 const RegType& insn_type, bool is_primitive) {
3408 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3409 if (!index_type.IsArrayIndexTypes()) {
3410 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3411 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003412 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3413 if (array_type.IsZero()) {
3414 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3415 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003416 } else if (!array_type.IsArrayTypes()) {
3417 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003418 } else {
3419 /* verify the class */
3420 const RegType& component_type = reg_types_.GetComponentType(array_type,
3421 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003422 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003423 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3424 << " source for aput-object";
3425 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3426 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3427 << " source for category 1 aput";
3428 } else if (is_primitive && !insn_type.Equals(component_type) &&
3429 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3430 (insn_type.IsLong() && component_type.IsDouble()))) {
3431 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
3432 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003434 // The instruction agrees with the type of array, confirm the value to be stored does too
3435 // Note: we use the instruction type (rather than the component type) for aput-object as
3436 // incompatible classes will be caught at runtime as an array store exception
3437 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 }
3439 }
3440 }
3441}
3442
3443Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003444 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3445 // Check access to class
3446 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3447 if (failure_ != VERIFY_ERROR_NONE) {
3448 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3449 << dex_file_->GetFieldName(field_id) << ") in "
3450 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3451 return NULL;
3452 }
3453 if(klass_type.IsUnresolvedTypes()) {
3454 return NULL; // Can't resolve Class so no more to do here
3455 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003456 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003458 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3459 << dex_file_->GetFieldName(field_id) << ") in "
3460 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 DCHECK(Thread::Current()->IsExceptionPending());
3462 Thread::Current()->ClearException();
3463 return NULL;
3464 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3465 field->GetAccessFlags())) {
3466 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3467 << " from " << PrettyClass(method_->GetDeclaringClass());
3468 return NULL;
3469 } else if (!field->IsStatic()) {
3470 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3471 return NULL;
3472 } else {
3473 return field;
3474 }
3475}
3476
Ian Rogersd81871c2011-10-03 13:57:23 -07003477Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003478 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3479 // Check access to class
3480 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3481 if (failure_ != VERIFY_ERROR_NONE) {
3482 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3483 << dex_file_->GetFieldName(field_id) << ") in "
3484 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3485 return NULL;
3486 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003487 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003488 return NULL; // Can't resolve Class so no more to do here
3489 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003490 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003492 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3493 << dex_file_->GetFieldName(field_id) << ") in "
3494 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 DCHECK(Thread::Current()->IsExceptionPending());
3496 Thread::Current()->ClearException();
3497 return NULL;
3498 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3499 field->GetAccessFlags())) {
3500 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3501 << " from " << PrettyClass(method_->GetDeclaringClass());
3502 return NULL;
3503 } else if (field->IsStatic()) {
3504 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3505 << " to not be static";
3506 return NULL;
3507 } else if (obj_type.IsZero()) {
3508 // Cannot infer and check type, however, access will cause null pointer exception
3509 return field;
jeffhao8cd6dda2012-02-22 10:15:34 -08003510 } else if(obj_type.IsUninitializedTypes() &&
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3512 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3513 // Field accesses through uninitialized references are only allowable for constructors where
3514 // the field is declared in this class
3515 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3516 << " of a not fully initialized object within the context of "
3517 << PrettyMethod(method_);
3518 return NULL;
3519 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3520 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3521 // of C1. For resolution to occur the declared class of the field must be compatible with
3522 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3523 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3524 << " from object of type " << PrettyClass(obj_type.GetClass());
3525 return NULL;
3526 } else {
3527 return field;
3528 }
3529}
3530
Ian Rogersb94a27b2011-10-26 00:33:41 -07003531void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3532 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003533 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003534 Field* field;
3535 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003536 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003537 } else {
3538 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003539 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003540 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003541 if (failure_ != VERIFY_ERROR_NONE) {
3542 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3543 } else {
3544 const char* descriptor;
3545 const ClassLoader* loader;
3546 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003547 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003548 loader = field->GetDeclaringClass()->GetClassLoader();
3549 } else {
3550 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3551 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3552 loader = method_->GetDeclaringClass()->GetClassLoader();
3553 }
3554 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003556 if (field_type.Equals(insn_type) ||
3557 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3558 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003559 // expected that read is of the correct primitive type or that int reads are reading
3560 // floats or long reads are reading doubles
3561 } else {
3562 // This is a global failure rather than a class change failure as the instructions and
3563 // the descriptors for the type should have been consistent within the same file at
3564 // compile time
3565 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003566 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003567 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003568 return;
3569 }
3570 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003571 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003573 << " to be compatible with type '" << insn_type
3574 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003575 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 return;
3577 }
3578 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003579 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 }
3581}
3582
Ian Rogersb94a27b2011-10-26 00:33:41 -07003583void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3584 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003585 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003586 Field* field;
3587 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003588 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003589 } else {
3590 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003591 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003592 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003593 if (failure_ != VERIFY_ERROR_NONE) {
3594 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3595 } else {
3596 const char* descriptor;
3597 const ClassLoader* loader;
3598 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003599 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003600 loader = field->GetDeclaringClass()->GetClassLoader();
3601 } else {
3602 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3603 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3604 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003605 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003606 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3607 if (field != NULL) {
3608 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3609 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3610 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3611 return;
3612 }
3613 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003615 // Primitive field assignability rules are weaker than regular assignability rules
3616 bool instruction_compatible;
3617 bool value_compatible;
3618 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3619 if (field_type.IsIntegralTypes()) {
3620 instruction_compatible = insn_type.IsIntegralTypes();
3621 value_compatible = value_type.IsIntegralTypes();
3622 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003623 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003624 value_compatible = value_type.IsFloatTypes();
3625 } else if (field_type.IsLong()) {
3626 instruction_compatible = insn_type.IsLong();
3627 value_compatible = value_type.IsLongTypes();
3628 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003629 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003630 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003632 instruction_compatible = false; // reference field with primitive store
3633 value_compatible = false; // unused
3634 }
3635 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 // This is a global failure rather than a class change failure as the instructions and
3637 // the descriptors for the type should have been consistent within the same file at
3638 // compile time
3639 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003640 << " to be of type '" << insn_type
3641 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003642 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 return;
3644 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003645 if (!value_compatible) {
3646 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3647 << " of type " << value_type
3648 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003649 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003650 return;
3651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003653 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003655 << " to be compatible with type '" << insn_type
3656 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003657 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 return;
3659 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003660 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 }
3663}
3664
3665bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3666 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3667 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3668 return false;
3669 }
3670 return true;
3671}
3672
Ian Rogersd81871c2011-10-03 13:57:23 -07003673void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003674 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003675 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003676 << " " << fail_messages_.str();
3677 return;
3678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3680 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3681 VerifyErrorRefType ref_type;
3682 switch (inst->Opcode()) {
3683 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003684 case Instruction::CHECK_CAST:
3685 case Instruction::INSTANCE_OF:
3686 case Instruction::NEW_INSTANCE:
3687 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003688 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003689 case Instruction::FILLED_NEW_ARRAY_RANGE:
3690 ref_type = VERIFY_ERROR_REF_CLASS;
3691 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003693 case Instruction::IGET_BOOLEAN:
3694 case Instruction::IGET_BYTE:
3695 case Instruction::IGET_CHAR:
3696 case Instruction::IGET_SHORT:
3697 case Instruction::IGET_WIDE:
3698 case Instruction::IGET_OBJECT:
3699 case Instruction::IPUT:
3700 case Instruction::IPUT_BOOLEAN:
3701 case Instruction::IPUT_BYTE:
3702 case Instruction::IPUT_CHAR:
3703 case Instruction::IPUT_SHORT:
3704 case Instruction::IPUT_WIDE:
3705 case Instruction::IPUT_OBJECT:
3706 case Instruction::SGET:
3707 case Instruction::SGET_BOOLEAN:
3708 case Instruction::SGET_BYTE:
3709 case Instruction::SGET_CHAR:
3710 case Instruction::SGET_SHORT:
3711 case Instruction::SGET_WIDE:
3712 case Instruction::SGET_OBJECT:
3713 case Instruction::SPUT:
3714 case Instruction::SPUT_BOOLEAN:
3715 case Instruction::SPUT_BYTE:
3716 case Instruction::SPUT_CHAR:
3717 case Instruction::SPUT_SHORT:
3718 case Instruction::SPUT_WIDE:
3719 case Instruction::SPUT_OBJECT:
3720 ref_type = VERIFY_ERROR_REF_FIELD;
3721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003722 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003723 case Instruction::INVOKE_VIRTUAL_RANGE:
3724 case Instruction::INVOKE_SUPER:
3725 case Instruction::INVOKE_SUPER_RANGE:
3726 case Instruction::INVOKE_DIRECT:
3727 case Instruction::INVOKE_DIRECT_RANGE:
3728 case Instruction::INVOKE_STATIC:
3729 case Instruction::INVOKE_STATIC_RANGE:
3730 case Instruction::INVOKE_INTERFACE:
3731 case Instruction::INVOKE_INTERFACE_RANGE:
3732 ref_type = VERIFY_ERROR_REF_METHOD;
3733 break;
jeffhaobdb76512011-09-07 11:43:16 -07003734 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003735 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003736 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003737 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003738 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3739 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3740 // instruction, so assert it.
3741 size_t width = inst->SizeInCodeUnits();
3742 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003743 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003744 // NOPs
3745 for (size_t i = 2; i < width; i++) {
3746 insns[work_insn_idx_ + i] = Instruction::NOP;
3747 }
3748 // Encode the opcode, with the failure code in the high byte
3749 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3750 (failure_ << 8) | // AA - component
3751 (ref_type << (8 + kVerifyErrorRefTypeShift));
3752 insns[work_insn_idx_] = new_instruction;
3753 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3754 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003755 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3756 << fail_messages_.str();
3757 if (gDebugVerify) {
3758 std::cout << std::endl << info_messages_.str();
3759 Dump(std::cout);
3760 }
jeffhaobdb76512011-09-07 11:43:16 -07003761}
jeffhaoba5ebb92011-08-25 17:24:37 -07003762
Ian Rogersd81871c2011-10-03 13:57:23 -07003763bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 bool changed = true;
3765 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3766 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003767 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 * We haven't processed this instruction before, and we haven't touched the registers here, so
3769 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3770 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003771 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003773 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003774 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3775 if (gDebugVerify) {
3776 copy->CopyFromLine(target_line);
3777 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003778 changed = target_line->MergeRegisters(merge_line);
3779 if (failure_ != VERIFY_ERROR_NONE) {
3780 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003781 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003782 if (gDebugVerify && changed) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003783 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "]"
3784 " to [" <<(void*)next_insn << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 << *copy.get() << " MERGE" << std::endl
3786 << *merge_line << " ==" << std::endl
3787 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003788 }
3789 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 if (changed) {
3791 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003792 }
3793 return true;
3794}
3795
Ian Rogersd81871c2011-10-03 13:57:23 -07003796void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3797 size_t* log2_max_gc_pc) {
3798 size_t local_gc_points = 0;
3799 size_t max_insn = 0;
3800 size_t max_ref_reg = -1;
3801 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3802 if (insn_flags_[i].IsGcPoint()) {
3803 local_gc_points++;
3804 max_insn = i;
3805 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003806 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003807 }
3808 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 *gc_points = local_gc_points;
3810 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3811 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003812 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003813 i++;
3814 }
3815 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003816}
3817
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003818const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 size_t num_entries, ref_bitmap_bits, pc_bits;
3820 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3821 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003822 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 // TODO: either a better GC map format or per method failures
3824 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3825 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003826 return NULL;
3827 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3829 // There are 2 bytes to encode the number of entries
3830 if (num_entries >= 65536) {
3831 // TODO: either a better GC map format or per method failures
3832 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3833 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003834 return NULL;
3835 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003837 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003838 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003839 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003840 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003841 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003842 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003844 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 // TODO: either a better GC map format or per method failures
3846 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3847 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3848 return NULL;
3849 }
3850 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003851 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 if (table == NULL) {
3853 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3854 return NULL;
3855 }
3856 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003857 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3858 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003859 table->push_back(num_entries & 0xFF);
3860 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3863 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003864 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003866 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003867 }
3868 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003869 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003870 }
3871 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003872 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 return table;
3874}
jeffhaoa0a764a2011-09-16 10:43:38 -07003875
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003876void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3878 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003879 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 size_t map_index = 0;
3881 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3882 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3883 if (insn_flags_[i].IsGcPoint()) {
3884 CHECK_LT(map_index, map.NumEntries());
3885 CHECK_EQ(map.GetPC(map_index), i);
3886 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3887 map_index++;
3888 RegisterLine* line = reg_table_.GetLine(i);
3889 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003890 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 CHECK_LT(j / 8, map.RegWidth());
3892 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3893 } else if ((j / 8) < map.RegWidth()) {
3894 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3895 } else {
3896 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3897 }
3898 }
3899 } else {
3900 CHECK(reg_bitmap == NULL);
3901 }
3902 }
3903}
jeffhaoa0a764a2011-09-16 10:43:38 -07003904
Ian Rogersd81871c2011-10-03 13:57:23 -07003905const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3906 size_t num_entries = NumEntries();
3907 // Do linear or binary search?
3908 static const size_t kSearchThreshold = 8;
3909 if (num_entries < kSearchThreshold) {
3910 for (size_t i = 0; i < num_entries; i++) {
3911 if (GetPC(i) == dex_pc) {
3912 return GetBitMap(i);
3913 }
3914 }
3915 } else {
3916 int lo = 0;
3917 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003918 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003919 int mid = (hi + lo) / 2;
3920 int mid_pc = GetPC(mid);
3921 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003922 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003923 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003924 hi = mid - 1;
3925 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003926 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003927 }
3928 }
3929 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003930 if (error_if_not_present) {
3931 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3932 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003933 return NULL;
3934}
3935
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003936Mutex DexVerifier::gc_maps_lock_("verifier gc maps lock");
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003937DexVerifier::GcMapTable DexVerifier::gc_maps_;
3938
3939void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003940 MutexLock mu(gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003941 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3942 if (existing_gc_map != NULL) {
3943 CHECK(*existing_gc_map == gc_map);
3944 delete existing_gc_map;
3945 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003946 gc_maps_[ref] = &gc_map;
3947 CHECK(GetGcMap(ref) != NULL);
3948}
3949
3950const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003951 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003952 GcMapTable::const_iterator it = gc_maps_.find(ref);
3953 if (it == gc_maps_.end()) {
3954 return NULL;
3955 }
3956 CHECK(it->second != NULL);
3957 return it->second;
3958}
3959
3960void DexVerifier::DeleteGcMaps() {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003961 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003962 STLDeleteValues(&gc_maps_);
3963}
3964
Logan Chienfca7e872011-12-20 20:08:22 +08003965#if defined(ART_USE_LLVM_COMPILER)
3966InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
3967 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3968 uint16_t regs_size = code_item_->registers_size_;
3969
3970 UniquePtr<InferredRegCategoryMap> table(
3971 new InferredRegCategoryMap(insns_size, regs_size));
3972
3973 for (size_t i = 0; i < insns_size; ++i) {
3974 if (RegisterLine* line = reg_table_.GetLine(i)) {
3975 for (size_t r = 0; r < regs_size; ++r) {
3976 RegType const &rt = line->GetRegisterType(r);
3977
3978 if (rt.IsZero()) {
3979 table->SetRegCategory(i, r, kRegZero);
3980 } else if (rt.IsCategory1Types()) {
3981 table->SetRegCategory(i, r, kRegCat1nr);
3982 } else if (rt.IsCategory2Types()) {
3983 table->SetRegCategory(i, r, kRegCat2);
3984 } else if (rt.IsReferenceTypes()) {
3985 table->SetRegCategory(i, r, kRegObject);
3986 } else {
3987 table->SetRegCategory(i, r, kRegUnknown);
3988 }
3989 }
3990 }
3991 }
3992
3993 return table.release();
3994}
3995#endif
3996
Ian Rogersd81871c2011-10-03 13:57:23 -07003997} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003998} // namespace art