blob: fb68470f09afd8dfc0817e87ea26e85526f44a65 [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) {
1583 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1584 << " insns_size=" << insns_size << ")";
1585 }
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_);
2171 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2172 // can't create an instance of an interface or abstract class */
2173 if (!res_type.IsInstantiableTypes()) {
2174 Fail(VERIFY_ERROR_INSTANTIATION)
2175 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002177 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2178 // Any registers holding previous allocations from this address that have not yet been
2179 // initialized must be marked invalid.
2180 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2181 // add the new uninitialized reference to the register state
2182 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 }
2184 break;
2185 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002186 case Instruction::NEW_ARRAY:
2187 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002190 VerifyNewArray(dec_insn, true, false);
2191 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002193 case Instruction::FILLED_NEW_ARRAY_RANGE:
2194 VerifyNewArray(dec_insn, true, true);
2195 just_set_result = true; // Filled new array range sets result register
2196 break;
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::CMPL_FLOAT:
2198 case Instruction::CMPG_FLOAT:
jeffhao457cc512012-02-02 16:55:13 -08002199 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float())) {
2200 break;
2201 }
2202 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float())) {
2203 break;
2204 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002206 break;
2207 case Instruction::CMPL_DOUBLE:
2208 case Instruction::CMPG_DOUBLE:
jeffhao457cc512012-02-02 16:55:13 -08002209 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double())) {
2210 break;
2211 }
2212 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double())) {
2213 break;
2214 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::CMP_LONG:
jeffhao457cc512012-02-02 16:55:13 -08002218 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long())) {
2219 break;
2220 }
2221 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long())) {
2222 break;
2223 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002227 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2228 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2229 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002230 }
2231 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 }
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::GOTO:
2234 case Instruction::GOTO_16:
2235 case Instruction::GOTO_32:
2236 /* no effect on or use of registers */
2237 break;
2238
2239 case Instruction::PACKED_SWITCH:
2240 case Instruction::SPARSE_SWITCH:
2241 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 case Instruction::FILL_ARRAY_DATA: {
2246 /* Similar to the verification done for APUT */
Ian Rogers89310de2012-02-01 13:47:30 -08002247 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA_);
2248 /* array_type can be null if the reg type is Zero */
2249 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002250 if (!array_type.IsArrayTypes()) {
2251 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002252 } else {
jeffhao457cc512012-02-02 16:55:13 -08002253 const RegType& component_type = reg_types_.GetComponentType(array_type,
2254 method_->GetDeclaringClass()->GetClassLoader());
2255 DCHECK(!component_type.IsUnknown());
2256 if (component_type.IsNonZeroReferenceTypes()) {
2257 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with component type "
2258 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 } else {
jeffhao457cc512012-02-02 16:55:13 -08002260 // Now verify if the element width in the table matches the element width declared in
2261 // the array
2262 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2263 if (array_data[0] != Instruction::kArrayDataSignature) {
2264 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2265 } else {
2266 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2267 // Since we don't compress the data in Dex, expect to see equal width of data stored
2268 // in the table and expected from the array class.
2269 if (array_data[1] != elem_width) {
2270 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2271 << " vs " << elem_width << ")";
2272 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 }
2274 }
jeffhaobdb76512011-09-07 11:43:16 -07002275 }
2276 }
2277 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 }
jeffhaobdb76512011-09-07 11:43:16 -07002279 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 case Instruction::IF_NE: {
2281 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2282 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2283 bool mismatch = false;
2284 if (reg_type1.IsZero()) { // zero then integral or reference expected
2285 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2286 } else if (reg_type1.IsReferenceTypes()) { // both references?
2287 mismatch = !reg_type2.IsReferenceTypes();
2288 } else { // both integral?
2289 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2290 }
2291 if (mismatch) {
2292 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2293 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002294 }
2295 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 }
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::IF_LT:
2298 case Instruction::IF_GE:
2299 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 case Instruction::IF_LE: {
2301 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2302 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2303 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2304 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2305 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002306 }
2307 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 }
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 case Instruction::IF_NEZ: {
2311 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2312 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2313 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2314 }
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::IF_LTZ:
2318 case Instruction::IF_GEZ:
2319 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 case Instruction::IF_LEZ: {
2321 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2322 if (!reg_type.IsIntegralTypes()) {
2323 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2324 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2325 }
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 }
jeffhaobdb76512011-09-07 11:43:16 -07002328 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2330 break;
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2333 break;
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 VerifyAGet(dec_insn, reg_types_.Char(), true);
2336 break;
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 case Instruction::AGET:
2341 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2342 break;
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 VerifyAGet(dec_insn, reg_types_.Long(), true);
2345 break;
2346 case Instruction::AGET_OBJECT:
2347 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 case Instruction::APUT_BOOLEAN:
2351 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2352 break;
2353 case Instruction::APUT_BYTE:
2354 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2355 break;
2356 case Instruction::APUT_CHAR:
2357 VerifyAPut(dec_insn, reg_types_.Char(), true);
2358 break;
2359 case Instruction::APUT_SHORT:
2360 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371
jeffhaobdb76512011-09-07 11:43:16 -07002372 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002373 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 break;
jeffhaobdb76512011-09-07 11:43:16 -07002375 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002376 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
jeffhaobdb76512011-09-07 11:43:16 -07002378 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002379 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 break;
jeffhaobdb76512011-09-07 11:43:16 -07002381 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002382 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 break;
2384 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002385 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002388 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002391 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 break;
jeffhaobdb76512011-09-07 11:43:16 -07002393
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002395 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 break;
2397 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002398 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 break;
2400 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002401 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 break;
2403 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002404 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002405 break;
2406 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002407 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002410 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002413 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415
jeffhaobdb76512011-09-07 11:43:16 -07002416 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002417 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 break;
jeffhaobdb76512011-09-07 11:43:16 -07002419 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002420 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 break;
jeffhaobdb76512011-09-07 11:43:16 -07002422 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002423 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 break;
jeffhaobdb76512011-09-07 11:43:16 -07002425 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002426 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 break;
2428 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002429 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002432 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002435 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002436 break;
2437
2438 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002439 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 break;
2441 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002442 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 break;
2444 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002445 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 break;
2447 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002448 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002451 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002454 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002457 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459
2460 case Instruction::INVOKE_VIRTUAL:
2461 case Instruction::INVOKE_VIRTUAL_RANGE:
2462 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 case Instruction::INVOKE_SUPER_RANGE: {
2464 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2465 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2466 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2467 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2468 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2469 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002470 const char* descriptor;
2471 if (called_method == NULL) {
2472 uint32_t method_idx = dec_insn.vB_;
2473 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2474 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002475 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002476 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002477 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002478 }
Ian Rogers9074b992011-10-26 17:41:55 -07002479 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002480 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002482 just_set_result = true;
2483 }
2484 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 }
jeffhaobdb76512011-09-07 11:43:16 -07002486 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 case Instruction::INVOKE_DIRECT_RANGE: {
2488 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2489 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2490 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002491 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 * Some additional checks when calling a constructor. We know from the invocation arg check
2493 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2494 * that to require that called_method->klass is the same as this->klass or this->super,
2495 * allowing the latter only if the "this" argument is the same as the "this" argument to
2496 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002497 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002498 bool is_constructor;
2499 if (called_method != NULL) {
2500 is_constructor = called_method->IsConstructor();
2501 } else {
2502 uint32_t method_idx = dec_insn.vB_;
2503 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2504 const char* name = dex_file_->GetMethodName(method_id);
2505 is_constructor = strcmp(name, "<init>") == 0;
2506 }
2507 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2509 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511
2512 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 if (this_type.IsZero()) {
2514 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002517 if (called_method != NULL) {
2518 Class* this_class = this_type.GetClass();
2519 DCHECK(this_class != NULL);
2520 /* must be in same class or in superclass */
2521 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2522 if (this_class != method_->GetDeclaringClass()) {
2523 Fail(VERIFY_ERROR_GENERIC)
2524 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2525 break;
2526 }
2527 } else if (called_method->GetDeclaringClass() != this_class) {
2528 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 }
jeffhaobdb76512011-09-07 11:43:16 -07002531 }
2532
2533 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002534 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2536 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 }
2539
2540 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002541 * Replace the uninitialized reference with an initialized one. We need to do this for all
2542 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002543 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 work_line_->MarkRefsAsInitialized(this_type);
2545 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002547 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002548 const char* descriptor;
2549 if (called_method == NULL) {
2550 uint32_t method_idx = dec_insn.vB_;
2551 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2552 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002553 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002554 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002555 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002556 }
Ian Rogers9074b992011-10-26 17:41:55 -07002557 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002560 just_set_result = true;
2561 }
2562 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 }
jeffhaobdb76512011-09-07 11:43:16 -07002564 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 case Instruction::INVOKE_STATIC_RANGE: {
2566 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2567 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2568 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002569 const char* descriptor;
2570 if (called_method == NULL) {
2571 uint32_t method_idx = dec_insn.vB_;
2572 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2573 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002574 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002575 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002576 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002577 }
Ian Rogers9074b992011-10-26 17:41:55 -07002578 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 work_line_->SetResultRegisterType(return_type);
2581 just_set_result = true;
2582 }
jeffhaobdb76512011-09-07 11:43:16 -07002583 }
2584 break;
2585 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 case Instruction::INVOKE_INTERFACE_RANGE: {
2587 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2588 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2589 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002590 if (abs_method != NULL) {
2591 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002592 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002593 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2594 << PrettyMethod(abs_method) << "'";
2595 break;
2596 }
2597 }
2598 /* Get the type of the "this" arg, which should either be a sub-interface of called
2599 * interface or Object (see comments in RegType::JoinClass).
2600 */
2601 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2602 if (failure_ == VERIFY_ERROR_NONE) {
2603 if (this_type.IsZero()) {
2604 /* null pointer always passes (and always fails at runtime) */
2605 } else {
2606 if (this_type.IsUninitializedTypes()) {
2607 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2608 << this_type;
2609 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002611 // In the past we have tried to assert that "called_interface" is assignable
2612 // from "this_type.GetClass()", however, as we do an imprecise Join
2613 // (RegType::JoinClass) we don't have full information on what interfaces are
2614 // implemented by "this_type". For example, two classes may implement the same
2615 // interfaces and have a common parent that doesn't implement the interface. The
2616 // join will set "this_type" to the parent class and a test that this implements
2617 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002618 }
2619 }
jeffhaobdb76512011-09-07 11:43:16 -07002620 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 * We don't have an object instance, so we can't find the concrete method. However, all of
2622 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002623 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002624 const char* descriptor;
2625 if (abs_method == NULL) {
2626 uint32_t method_idx = dec_insn.vB_;
2627 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2628 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002629 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002630 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002631 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002632 }
Ian Rogers9074b992011-10-26 17:41:55 -07002633 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002634 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2635 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002637 just_set_result = true;
2638 }
2639 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 }
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::NEG_INT:
2642 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::NEG_LONG:
2646 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
2682 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002684 break;
2685 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
2688 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
2697 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700
2701 case Instruction::ADD_INT:
2702 case Instruction::SUB_INT:
2703 case Instruction::MUL_INT:
2704 case Instruction::REM_INT:
2705 case Instruction::DIV_INT:
2706 case Instruction::SHL_INT:
2707 case Instruction::SHR_INT:
2708 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
2711 case Instruction::AND_INT:
2712 case Instruction::OR_INT:
2713 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::ADD_LONG:
2717 case Instruction::SUB_LONG:
2718 case Instruction::MUL_LONG:
2719 case Instruction::DIV_LONG:
2720 case Instruction::REM_LONG:
2721 case Instruction::AND_LONG:
2722 case Instruction::OR_LONG:
2723 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::SHL_LONG:
2727 case Instruction::SHR_LONG:
2728 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 /* shift distance is Int, making these different from other binary operations */
2730 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::ADD_FLOAT:
2733 case Instruction::SUB_FLOAT:
2734 case Instruction::MUL_FLOAT:
2735 case Instruction::DIV_FLOAT:
2736 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::ADD_DOUBLE:
2740 case Instruction::SUB_DOUBLE:
2741 case Instruction::MUL_DOUBLE:
2742 case Instruction::DIV_DOUBLE:
2743 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::ADD_INT_2ADDR:
2747 case Instruction::SUB_INT_2ADDR:
2748 case Instruction::MUL_INT_2ADDR:
2749 case Instruction::REM_INT_2ADDR:
2750 case Instruction::SHL_INT_2ADDR:
2751 case Instruction::SHR_INT_2ADDR:
2752 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::AND_INT_2ADDR:
2756 case Instruction::OR_INT_2ADDR:
2757 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::ADD_LONG_2ADDR:
2764 case Instruction::SUB_LONG_2ADDR:
2765 case Instruction::MUL_LONG_2ADDR:
2766 case Instruction::DIV_LONG_2ADDR:
2767 case Instruction::REM_LONG_2ADDR:
2768 case Instruction::AND_LONG_2ADDR:
2769 case Instruction::OR_LONG_2ADDR:
2770 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::SHL_LONG_2ADDR:
2774 case Instruction::SHR_LONG_2ADDR:
2775 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::ADD_FLOAT_2ADDR:
2779 case Instruction::SUB_FLOAT_2ADDR:
2780 case Instruction::MUL_FLOAT_2ADDR:
2781 case Instruction::DIV_FLOAT_2ADDR:
2782 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::ADD_DOUBLE_2ADDR:
2786 case Instruction::SUB_DOUBLE_2ADDR:
2787 case Instruction::MUL_DOUBLE_2ADDR:
2788 case Instruction::DIV_DOUBLE_2ADDR:
2789 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::ADD_INT_LIT16:
2793 case Instruction::RSUB_INT:
2794 case Instruction::MUL_INT_LIT16:
2795 case Instruction::DIV_INT_LIT16:
2796 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799 case Instruction::AND_INT_LIT16:
2800 case Instruction::OR_INT_LIT16:
2801 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::ADD_INT_LIT8:
2805 case Instruction::RSUB_INT_LIT8:
2806 case Instruction::MUL_INT_LIT8:
2807 case Instruction::DIV_INT_LIT8:
2808 case Instruction::REM_INT_LIT8:
2809 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002810 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002811 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814 case Instruction::AND_INT_LIT8:
2815 case Instruction::OR_INT_LIT8:
2816 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819
2820 /*
2821 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002822 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002823 * inserted in the course of verification, we can expect to see it here.
2824 */
jeffhaob4df5142011-09-19 20:25:32 -07002825 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002829 case Instruction::UNUSED_EE:
2830 case Instruction::UNUSED_EF:
2831 case Instruction::UNUSED_F2:
2832 case Instruction::UNUSED_F3:
2833 case Instruction::UNUSED_F4:
2834 case Instruction::UNUSED_F5:
2835 case Instruction::UNUSED_F6:
2836 case Instruction::UNUSED_F7:
2837 case Instruction::UNUSED_F8:
2838 case Instruction::UNUSED_F9:
2839 case Instruction::UNUSED_FA:
2840 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002841 case Instruction::UNUSED_F0:
2842 case Instruction::UNUSED_F1:
2843 case Instruction::UNUSED_E3:
2844 case Instruction::UNUSED_E8:
2845 case Instruction::UNUSED_E7:
2846 case Instruction::UNUSED_E4:
2847 case Instruction::UNUSED_E9:
2848 case Instruction::UNUSED_FC:
2849 case Instruction::UNUSED_E5:
2850 case Instruction::UNUSED_EA:
2851 case Instruction::UNUSED_FD:
2852 case Instruction::UNUSED_E6:
2853 case Instruction::UNUSED_EB:
2854 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002855 case Instruction::UNUSED_3E:
2856 case Instruction::UNUSED_3F:
2857 case Instruction::UNUSED_40:
2858 case Instruction::UNUSED_41:
2859 case Instruction::UNUSED_42:
2860 case Instruction::UNUSED_43:
2861 case Instruction::UNUSED_73:
2862 case Instruction::UNUSED_79:
2863 case Instruction::UNUSED_7A:
2864 case Instruction::UNUSED_EC:
2865 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002866 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002867 break;
2868
2869 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002870 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002871 * complain if an instruction is missing (which is desirable).
2872 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002874
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 if (failure_ != VERIFY_ERROR_NONE) {
2876 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002877 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002878 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002879 return false;
2880 } else {
2881 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002882 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002884 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002886 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002888 opcode_flag = Instruction::kThrow;
2889 }
2890 }
jeffhaobdb76512011-09-07 11:43:16 -07002891 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 * If we didn't just set the result register, clear it out. This ensures that you can only use
2893 * "move-result" immediately after the result is set. (We could check this statically, but it's
2894 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002895 */
2896 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002898 }
2899
jeffhaoa0a764a2011-09-16 10:43:38 -07002900 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002901 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2903 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2904 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002905 return false;
2906 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2908 // next instruction isn't one.
2909 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002910 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 }
2912 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2913 if (next_line != NULL) {
2914 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2915 // needed.
2916 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002917 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 }
jeffhaobdb76512011-09-07 11:43:16 -07002919 } else {
2920 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 * We're not recording register data for the next instruction, so we don't know what the prior
2922 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002923 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002925 }
2926 }
2927
2928 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002929 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002930 *
2931 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002932 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002933 * somebody could get a reference field, check it for zero, and if the
2934 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002935 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002936 * that, and will reject the code.
2937 *
2938 * TODO: avoid re-fetching the branch target
2939 */
2940 if ((opcode_flag & Instruction::kBranch) != 0) {
2941 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002943 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002945 return false;
2946 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002947 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002949 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 }
jeffhaobdb76512011-09-07 11:43:16 -07002951 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
jeffhaobdb76512011-09-07 11:43:16 -07002955 }
2956
2957 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002958 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002959 *
2960 * We've already verified that the table is structurally sound, so we
2961 * just need to walk through and tag the targets.
2962 */
2963 if ((opcode_flag & Instruction::kSwitch) != 0) {
2964 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2965 const uint16_t* switch_insns = insns + offset_to_switch;
2966 int switch_count = switch_insns[1];
2967 int offset_to_targets, targ;
2968
2969 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2970 /* 0 = sig, 1 = count, 2/3 = first key */
2971 offset_to_targets = 4;
2972 } else {
2973 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002974 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002975 offset_to_targets = 2 + 2 * switch_count;
2976 }
2977
2978 /* verify each switch target */
2979 for (targ = 0; targ < switch_count; targ++) {
2980 int offset;
2981 uint32_t abs_offset;
2982
2983 /* offsets are 32-bit, and only partly endian-swapped */
2984 offset = switch_insns[offset_to_targets + targ * 2] |
2985 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 abs_offset = work_insn_idx_ + offset;
2987 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2988 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002989 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 }
2991 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002992 return false;
2993 }
2994 }
2995
2996 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2998 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002999 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
3001 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003002 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003003
Ian Rogers0571d352011-11-03 19:51:38 -07003004 for (; iterator.HasNext(); iterator.Next()) {
3005 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 within_catch_all = true;
3007 }
jeffhaobdb76512011-09-07 11:43:16 -07003008 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3010 * "work_regs", because at runtime the exception will be thrown before the instruction
3011 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003012 */
Ian Rogers0571d352011-11-03 19:51:38 -07003013 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003014 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 }
jeffhaobdb76512011-09-07 11:43:16 -07003016 }
3017
3018 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3020 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003021 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003023 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 * The state in work_line reflects the post-execution state. If the current instruction is a
3025 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003026 * it will do so before grabbing the lock).
3027 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
3029 Fail(VERIFY_ERROR_GENERIC)
3030 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003031 return false;
3032 }
3033 }
3034 }
3035
jeffhaod1f0fde2011-09-08 17:25:33 -07003036 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 if ((opcode_flag & Instruction::kReturn) != 0) {
3038 if(!work_line_->VerifyMonitorStackEmpty()) {
3039 return false;
3040 }
jeffhaobdb76512011-09-07 11:43:16 -07003041 }
3042
3043 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003044 * Update start_guess. Advance to the next instruction of that's
3045 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003046 * neither of those exists we're in a return or throw; leave start_guess
3047 * alone and let the caller sort it out.
3048 */
3049 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003051 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3052 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003054 }
3055
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3057 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003058
3059 return true;
3060}
3061
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003063 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003064 Class* referrer = method_->GetDeclaringClass();
3065 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3066 const RegType& result =
3067 klass != NULL ? reg_types_.FromClass(klass)
3068 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3069 if (klass == NULL && !result.IsUnresolvedTypes()) {
3070 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003072 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3073 // check at runtime if access is allowed and so pass here.
3074 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3075 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003076 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003077 << result << "'";
3078 return reg_types_.Unknown();
3079 } else {
3080 return result;
3081 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003082}
3083
Ian Rogers28ad40d2011-10-27 15:19:26 -07003084const RegType& DexVerifier::GetCaughtExceptionType() {
3085 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003087 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3089 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003090 CatchHandlerIterator iterator(handlers_ptr);
3091 for (; iterator.HasNext(); iterator.Next()) {
3092 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3093 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003094 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003096 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003097 if (common_super == NULL) {
3098 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3099 // as that is caught at runtime
3100 common_super = &exception;
3101 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3102 // We don't know enough about the type and the common path merge will result in
3103 // Conflict. Fail here knowing the correct thing can be done at runtime.
Ian Rogers28ad40d2011-10-27 15:19:26 -07003104 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3105 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003106 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003107 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003109 common_super = &common_super->Merge(exception, &reg_types_);
3110 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 }
3112 }
3113 }
3114 }
Ian Rogers0571d352011-11-03 19:51:38 -07003115 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 }
3117 }
3118 if (common_super == NULL) {
3119 /* no catch blocks, or no catches with classes we can find */
3120 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003121 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003123 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003124}
3125
3126Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003127 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3128 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3129 if (failure_ != VERIFY_ERROR_NONE) {
3130 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3131 return NULL;
3132 }
3133 if(klass_type.IsUnresolvedTypes()) {
3134 return NULL; // Can't resolve Class so no more to do here
3135 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 Class* referrer = method_->GetDeclaringClass();
3137 DexCache* dex_cache = referrer->GetDexCache();
3138 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3139 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003140 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003141 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003142 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 if (is_direct) {
3144 res_method = klass->FindDirectMethod(name, signature);
3145 } else if (klass->IsInterface()) {
3146 res_method = klass->FindInterfaceMethod(name, signature);
3147 } else {
3148 res_method = klass->FindVirtualMethod(name, signature);
3149 }
3150 if (res_method != NULL) {
3151 dex_cache->SetResolvedMethod(method_idx, res_method);
3152 } else {
3153 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003154 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 << " " << signature;
3156 return NULL;
3157 }
3158 }
3159 /* Check if access is allowed. */
3160 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3161 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003162 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003163 return NULL;
3164 }
3165 return res_method;
3166}
3167
3168Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3169 MethodType method_type, bool is_range, bool is_super) {
3170 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3171 // we're making.
3172 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3173 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003174 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 return NULL;
3176 }
3177 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3178 // enforce them here.
3179 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3180 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3181 << PrettyMethod(res_method);
3182 return NULL;
3183 }
3184 // See if the method type implied by the invoke instruction matches the access flags for the
3185 // target method.
3186 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3187 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3188 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3189 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003190 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3191 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 return NULL;
3193 }
3194 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3195 // has a vtable entry for the target method.
3196 if (is_super) {
3197 DCHECK(method_type == METHOD_VIRTUAL);
3198 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003199 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 if (super == NULL) { // Only Object has no super class
3201 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3202 << " to super " << PrettyMethod(res_method);
3203 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003204 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003206 << " to super " << PrettyDescriptor(super)
3207 << "." << mh.GetName()
3208 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 }
3210 return NULL;
3211 }
3212 }
3213 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3214 // match the call to the signature. Also, we might might be calling through an abstract method
3215 // definition (which doesn't have register count values).
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003216 size_t expected_args = dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 /* caught by static verifier */
3218 DCHECK(is_range || expected_args <= 5);
3219 if (expected_args > code_item_->outs_size_) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003220 Fail(VERIFY_ERROR_GENERIC) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3222 return NULL;
3223 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003224
jeffhaobdb76512011-09-07 11:43:16 -07003225 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 * Check the "this" argument, which must be an instance of the class
3227 * that declared the method. For an interface class, we don't do the
3228 * full interface merge, so we can't do a rigorous check here (which
3229 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003230 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003231 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003232 if (!res_method->IsStatic()) {
3233 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3234 if (failure_ != VERIFY_ERROR_NONE) {
3235 return NULL;
3236 }
3237 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3238 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3239 return NULL;
3240 }
3241 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003242 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3243 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003244 Fail(VERIFY_ERROR_GENERIC) << "'this' argument '" << actual_arg_type
3245 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 return NULL;
3247 }
3248 }
3249 actual_args++;
3250 }
3251 /*
3252 * Process the target method's signature. This signature may or may not
3253 * have been verified, so we can't assume it's properly formed.
3254 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003255 MethodHelper mh(res_method);
3256 const DexFile::TypeList* params = mh.GetParameterTypeList();
3257 size_t params_size = params == NULL ? 0 : params->Size();
3258 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 if (actual_args >= expected_args) {
3260 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003261 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3262 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 return NULL;
3264 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003265 const char* descriptor =
3266 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3267 if (descriptor == NULL) {
3268 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3269 << " missing signature component";
3270 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 }
3272 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003273 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07003274 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3275 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3276 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 }
3278 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3279 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 if (actual_args != expected_args) {
3281 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003282 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003283 return NULL;
3284 } else {
3285 return res_method;
3286 }
3287}
3288
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003289const RegType& DexVerifier::GetMethodReturnType() {
3290 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3291 MethodHelper(method_).GetReturnTypeDescriptor());
3292}
3293
Ian Rogers0c4a5062012-02-03 15:18:59 -08003294void DexVerifier::VerifyNewArray(const Instruction::DecodedInstruction& dec_insn, bool is_filled,
3295 bool is_range) {
3296 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB_ : dec_insn.vC_);
3297 if (res_type.IsUnknown()) {
3298 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3299 } else {
3300 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3301 if (!res_type.IsArrayTypes()) {
3302 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
3303 } else if (!is_filled) {
3304 /* make sure "size" register is valid type */
3305 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
3306 /* set register type to array class */
3307 work_line_->SetRegisterType(dec_insn.vA_, res_type);
3308 } else {
3309 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3310 // the list and fail. It's legal, if silly, for arg_count to be zero.
3311 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3312 method_->GetDeclaringClass()->GetClassLoader());
3313 uint32_t arg_count = dec_insn.vA_;
3314 for (size_t ui = 0; ui < arg_count; ui++) {
3315 uint32_t get_reg = is_range ? dec_insn.vC_ + ui : dec_insn.arg_[ui];
3316 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3317 work_line_->SetResultRegisterType(reg_types_.Unknown());
3318 return;
3319 }
3320 }
3321 // filled-array result goes into "result" register
3322 work_line_->SetResultRegisterType(res_type);
3323 }
3324 }
3325}
3326
Ian Rogersd81871c2011-10-03 13:57:23 -07003327void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3328 const RegType& insn_type, bool is_primitive) {
3329 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3330 if (!index_type.IsArrayIndexTypes()) {
3331 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3332 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003333 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3334 if (array_type.IsZero()) {
3335 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3336 // instruction type. TODO: have a proper notion of bottom here.
3337 if (!is_primitive || insn_type.IsCategory1Types()) {
3338 // Reference or category 1
3339 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003341 // Category 2
3342 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3343 }
jeffhaofc3144e2012-02-01 17:21:15 -08003344 } else if (!array_type.IsArrayTypes()) {
3345 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003346 } else {
3347 /* verify the class */
3348 const RegType& component_type = reg_types_.GetComponentType(array_type,
3349 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003350 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003351 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3352 << " source for aget-object";
3353 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3354 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3355 << " source for category 1 aget";
3356 } else if (is_primitive && !insn_type.Equals(component_type) &&
3357 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3358 (insn_type.IsLong() && component_type.IsDouble()))) {
3359 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003361 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 // Use knowledge of the field type which is stronger than the type inferred from the
3363 // instruction, which can't differentiate object types and ints from floats, longs from
3364 // doubles.
3365 work_line_->SetRegisterType(dec_insn.vA_, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 }
3367 }
3368 }
3369}
3370
3371void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3372 const RegType& insn_type, bool is_primitive) {
3373 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3374 if (!index_type.IsArrayIndexTypes()) {
3375 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3376 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003377 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3378 if (array_type.IsZero()) {
3379 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3380 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003381 } else if (!array_type.IsArrayTypes()) {
3382 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003383 } else {
3384 /* verify the class */
3385 const RegType& component_type = reg_types_.GetComponentType(array_type,
3386 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003387 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003388 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3389 << " source for aput-object";
3390 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3391 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3392 << " source for category 1 aput";
3393 } else if (is_primitive && !insn_type.Equals(component_type) &&
3394 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3395 (insn_type.IsLong() && component_type.IsDouble()))) {
3396 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
3397 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003399 // The instruction agrees with the type of array, confirm the value to be stored does too
3400 // Note: we use the instruction type (rather than the component type) for aput-object as
3401 // incompatible classes will be caught at runtime as an array store exception
3402 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 }
3404 }
3405 }
3406}
3407
3408Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003409 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3410 // Check access to class
3411 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3412 if (failure_ != VERIFY_ERROR_NONE) {
3413 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3414 << dex_file_->GetFieldName(field_id) << ") in "
3415 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3416 return NULL;
3417 }
3418 if(klass_type.IsUnresolvedTypes()) {
3419 return NULL; // Can't resolve Class so no more to do here
3420 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003421 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003423 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3424 << dex_file_->GetFieldName(field_id) << ") in "
3425 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 DCHECK(Thread::Current()->IsExceptionPending());
3427 Thread::Current()->ClearException();
3428 return NULL;
3429 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3430 field->GetAccessFlags())) {
3431 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3432 << " from " << PrettyClass(method_->GetDeclaringClass());
3433 return NULL;
3434 } else if (!field->IsStatic()) {
3435 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3436 return NULL;
3437 } else {
3438 return field;
3439 }
3440}
3441
Ian Rogersd81871c2011-10-03 13:57:23 -07003442Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003443 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3444 // Check access to class
3445 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3446 if (failure_ != VERIFY_ERROR_NONE) {
3447 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3448 << dex_file_->GetFieldName(field_id) << ") in "
3449 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3450 return NULL;
3451 }
3452 if(klass_type.IsUnresolvedTypes()) {
3453 return NULL; // Can't resolve Class so no more to do here
3454 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003455 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003457 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3458 << dex_file_->GetFieldName(field_id) << ") in "
3459 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 DCHECK(Thread::Current()->IsExceptionPending());
3461 Thread::Current()->ClearException();
3462 return NULL;
3463 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3464 field->GetAccessFlags())) {
3465 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3466 << " from " << PrettyClass(method_->GetDeclaringClass());
3467 return NULL;
3468 } else if (field->IsStatic()) {
3469 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3470 << " to not be static";
3471 return NULL;
3472 } else if (obj_type.IsZero()) {
3473 // Cannot infer and check type, however, access will cause null pointer exception
3474 return field;
3475 } else if(obj_type.IsUninitializedReference() &&
3476 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3477 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3478 // Field accesses through uninitialized references are only allowable for constructors where
3479 // the field is declared in this class
3480 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3481 << " of a not fully initialized object within the context of "
3482 << PrettyMethod(method_);
3483 return NULL;
3484 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3485 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3486 // of C1. For resolution to occur the declared class of the field must be compatible with
3487 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3488 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3489 << " from object of type " << PrettyClass(obj_type.GetClass());
3490 return NULL;
3491 } else {
3492 return field;
3493 }
3494}
3495
Ian Rogersb94a27b2011-10-26 00:33:41 -07003496void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3497 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003498 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003499 Field* field;
3500 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003501 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003502 } else {
3503 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003504 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003506 if (failure_ != VERIFY_ERROR_NONE) {
3507 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3508 } else {
3509 const char* descriptor;
3510 const ClassLoader* loader;
3511 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003512 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003513 loader = field->GetDeclaringClass()->GetClassLoader();
3514 } else {
3515 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3516 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3517 loader = method_->GetDeclaringClass()->GetClassLoader();
3518 }
3519 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003520 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003521 if (field_type.Equals(insn_type) ||
3522 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3523 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 // expected that read is of the correct primitive type or that int reads are reading
3525 // floats or long reads are reading doubles
3526 } else {
3527 // This is a global failure rather than a class change failure as the instructions and
3528 // the descriptors for the type should have been consistent within the same file at
3529 // compile time
3530 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003531 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003532 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003533 return;
3534 }
3535 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003536 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003538 << " to be compatible with type '" << insn_type
3539 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003540 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 return;
3542 }
3543 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003544 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 }
3546}
3547
Ian Rogersb94a27b2011-10-26 00:33:41 -07003548void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3549 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003550 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003551 Field* field;
3552 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003553 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003554 } else {
3555 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003556 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003557 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003558 if (failure_ != VERIFY_ERROR_NONE) {
3559 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3560 } else {
3561 const char* descriptor;
3562 const ClassLoader* loader;
3563 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003564 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003565 loader = field->GetDeclaringClass()->GetClassLoader();
3566 } else {
3567 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3568 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3569 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003570 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003571 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3572 if (field != NULL) {
3573 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3574 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3575 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3576 return;
3577 }
3578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003580 // Primitive field assignability rules are weaker than regular assignability rules
3581 bool instruction_compatible;
3582 bool value_compatible;
3583 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3584 if (field_type.IsIntegralTypes()) {
3585 instruction_compatible = insn_type.IsIntegralTypes();
3586 value_compatible = value_type.IsIntegralTypes();
3587 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003588 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003589 value_compatible = value_type.IsFloatTypes();
3590 } else if (field_type.IsLong()) {
3591 instruction_compatible = insn_type.IsLong();
3592 value_compatible = value_type.IsLongTypes();
3593 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003594 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003595 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003597 instruction_compatible = false; // reference field with primitive store
3598 value_compatible = false; // unused
3599 }
3600 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 // This is a global failure rather than a class change failure as the instructions and
3602 // the descriptors for the type should have been consistent within the same file at
3603 // compile time
3604 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003605 << " to be of type '" << insn_type
3606 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003607 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003608 return;
3609 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003610 if (!value_compatible) {
3611 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3612 << " of type " << value_type
3613 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003614 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003615 return;
3616 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003618 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003620 << " to be compatible with type '" << insn_type
3621 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003622 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 return;
3624 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003625 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 }
3628}
3629
3630bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3631 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3632 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3633 return false;
3634 }
3635 return true;
3636}
3637
Ian Rogersd81871c2011-10-03 13:57:23 -07003638void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003639 if (Runtime::Current()->IsStarted()) {
3640 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3641 << " " << fail_messages_.str();
3642 return;
3643 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3645 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3646 VerifyErrorRefType ref_type;
3647 switch (inst->Opcode()) {
3648 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003649 case Instruction::CHECK_CAST:
3650 case Instruction::INSTANCE_OF:
3651 case Instruction::NEW_INSTANCE:
3652 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003654 case Instruction::FILLED_NEW_ARRAY_RANGE:
3655 ref_type = VERIFY_ERROR_REF_CLASS;
3656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003657 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003658 case Instruction::IGET_BOOLEAN:
3659 case Instruction::IGET_BYTE:
3660 case Instruction::IGET_CHAR:
3661 case Instruction::IGET_SHORT:
3662 case Instruction::IGET_WIDE:
3663 case Instruction::IGET_OBJECT:
3664 case Instruction::IPUT:
3665 case Instruction::IPUT_BOOLEAN:
3666 case Instruction::IPUT_BYTE:
3667 case Instruction::IPUT_CHAR:
3668 case Instruction::IPUT_SHORT:
3669 case Instruction::IPUT_WIDE:
3670 case Instruction::IPUT_OBJECT:
3671 case Instruction::SGET:
3672 case Instruction::SGET_BOOLEAN:
3673 case Instruction::SGET_BYTE:
3674 case Instruction::SGET_CHAR:
3675 case Instruction::SGET_SHORT:
3676 case Instruction::SGET_WIDE:
3677 case Instruction::SGET_OBJECT:
3678 case Instruction::SPUT:
3679 case Instruction::SPUT_BOOLEAN:
3680 case Instruction::SPUT_BYTE:
3681 case Instruction::SPUT_CHAR:
3682 case Instruction::SPUT_SHORT:
3683 case Instruction::SPUT_WIDE:
3684 case Instruction::SPUT_OBJECT:
3685 ref_type = VERIFY_ERROR_REF_FIELD;
3686 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003688 case Instruction::INVOKE_VIRTUAL_RANGE:
3689 case Instruction::INVOKE_SUPER:
3690 case Instruction::INVOKE_SUPER_RANGE:
3691 case Instruction::INVOKE_DIRECT:
3692 case Instruction::INVOKE_DIRECT_RANGE:
3693 case Instruction::INVOKE_STATIC:
3694 case Instruction::INVOKE_STATIC_RANGE:
3695 case Instruction::INVOKE_INTERFACE:
3696 case Instruction::INVOKE_INTERFACE_RANGE:
3697 ref_type = VERIFY_ERROR_REF_METHOD;
3698 break;
jeffhaobdb76512011-09-07 11:43:16 -07003699 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003700 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003701 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003702 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3704 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3705 // instruction, so assert it.
3706 size_t width = inst->SizeInCodeUnits();
3707 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003708 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003709 // NOPs
3710 for (size_t i = 2; i < width; i++) {
3711 insns[work_insn_idx_ + i] = Instruction::NOP;
3712 }
3713 // Encode the opcode, with the failure code in the high byte
3714 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3715 (failure_ << 8) | // AA - component
3716 (ref_type << (8 + kVerifyErrorRefTypeShift));
3717 insns[work_insn_idx_] = new_instruction;
3718 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3719 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003720 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3721 << fail_messages_.str();
3722 if (gDebugVerify) {
3723 std::cout << std::endl << info_messages_.str();
3724 Dump(std::cout);
3725 }
jeffhaobdb76512011-09-07 11:43:16 -07003726}
jeffhaoba5ebb92011-08-25 17:24:37 -07003727
Ian Rogersd81871c2011-10-03 13:57:23 -07003728bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003729 bool changed = true;
3730 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3731 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003732 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 * We haven't processed this instruction before, and we haven't touched the registers here, so
3734 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3735 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003736 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003738 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003739 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3740 if (gDebugVerify) {
3741 copy->CopyFromLine(target_line);
3742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 changed = target_line->MergeRegisters(merge_line);
3744 if (failure_ != VERIFY_ERROR_NONE) {
3745 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003746 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003747 if (gDebugVerify && changed) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003748 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "]"
3749 " to [" <<(void*)next_insn << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 << *copy.get() << " MERGE" << std::endl
3751 << *merge_line << " ==" << std::endl
3752 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003753 }
3754 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 if (changed) {
3756 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003757 }
3758 return true;
3759}
3760
Ian Rogersd81871c2011-10-03 13:57:23 -07003761void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3762 size_t* log2_max_gc_pc) {
3763 size_t local_gc_points = 0;
3764 size_t max_insn = 0;
3765 size_t max_ref_reg = -1;
3766 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3767 if (insn_flags_[i].IsGcPoint()) {
3768 local_gc_points++;
3769 max_insn = i;
3770 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003771 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003772 }
3773 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 *gc_points = local_gc_points;
3775 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3776 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003777 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003778 i++;
3779 }
3780 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003781}
3782
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003783const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 size_t num_entries, ref_bitmap_bits, pc_bits;
3785 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3786 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003787 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 // TODO: either a better GC map format or per method failures
3789 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3790 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003791 return NULL;
3792 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3794 // There are 2 bytes to encode the number of entries
3795 if (num_entries >= 65536) {
3796 // TODO: either a better GC map format or per method failures
3797 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3798 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003799 return NULL;
3800 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003801 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003802 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003803 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003804 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003806 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003807 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003809 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003810 // TODO: either a better GC map format or per method failures
3811 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3812 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3813 return NULL;
3814 }
3815 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003816 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 if (table == NULL) {
3818 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3819 return NULL;
3820 }
3821 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003822 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3823 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003824 table->push_back(num_entries & 0xFF);
3825 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003827 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3828 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003829 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003831 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 }
3833 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003834 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 }
3836 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003837 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 return table;
3839}
jeffhaoa0a764a2011-09-16 10:43:38 -07003840
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003841void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3843 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003844 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 size_t map_index = 0;
3846 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3847 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3848 if (insn_flags_[i].IsGcPoint()) {
3849 CHECK_LT(map_index, map.NumEntries());
3850 CHECK_EQ(map.GetPC(map_index), i);
3851 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3852 map_index++;
3853 RegisterLine* line = reg_table_.GetLine(i);
3854 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003855 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003856 CHECK_LT(j / 8, map.RegWidth());
3857 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3858 } else if ((j / 8) < map.RegWidth()) {
3859 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3860 } else {
3861 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3862 }
3863 }
3864 } else {
3865 CHECK(reg_bitmap == NULL);
3866 }
3867 }
3868}
jeffhaoa0a764a2011-09-16 10:43:38 -07003869
Ian Rogersd81871c2011-10-03 13:57:23 -07003870const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3871 size_t num_entries = NumEntries();
3872 // Do linear or binary search?
3873 static const size_t kSearchThreshold = 8;
3874 if (num_entries < kSearchThreshold) {
3875 for (size_t i = 0; i < num_entries; i++) {
3876 if (GetPC(i) == dex_pc) {
3877 return GetBitMap(i);
3878 }
3879 }
3880 } else {
3881 int lo = 0;
3882 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003883 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003884 int mid = (hi + lo) / 2;
3885 int mid_pc = GetPC(mid);
3886 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003887 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003889 hi = mid - 1;
3890 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003892 }
3893 }
3894 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003895 if (error_if_not_present) {
3896 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3897 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003898 return NULL;
3899}
3900
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003901Mutex DexVerifier::gc_maps_lock_("verifier gc maps lock");
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003902DexVerifier::GcMapTable DexVerifier::gc_maps_;
3903
3904void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003905 MutexLock mu(gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003906 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3907 if (existing_gc_map != NULL) {
3908 CHECK(*existing_gc_map == gc_map);
3909 delete existing_gc_map;
3910 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003911 gc_maps_[ref] = &gc_map;
3912 CHECK(GetGcMap(ref) != NULL);
3913}
3914
3915const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003916 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003917 GcMapTable::const_iterator it = gc_maps_.find(ref);
3918 if (it == gc_maps_.end()) {
3919 return NULL;
3920 }
3921 CHECK(it->second != NULL);
3922 return it->second;
3923}
3924
3925void DexVerifier::DeleteGcMaps() {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003926 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003927 STLDeleteValues(&gc_maps_);
3928}
3929
Logan Chienfca7e872011-12-20 20:08:22 +08003930#if defined(ART_USE_LLVM_COMPILER)
3931InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
3932 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3933 uint16_t regs_size = code_item_->registers_size_;
3934
3935 UniquePtr<InferredRegCategoryMap> table(
3936 new InferredRegCategoryMap(insns_size, regs_size));
3937
3938 for (size_t i = 0; i < insns_size; ++i) {
3939 if (RegisterLine* line = reg_table_.GetLine(i)) {
3940 for (size_t r = 0; r < regs_size; ++r) {
3941 RegType const &rt = line->GetRegisterType(r);
3942
3943 if (rt.IsZero()) {
3944 table->SetRegCategory(i, r, kRegZero);
3945 } else if (rt.IsCategory1Types()) {
3946 table->SetRegCategory(i, r, kRegCat1nr);
3947 } else if (rt.IsCategory2Types()) {
3948 table->SetRegCategory(i, r, kRegCat2);
3949 } else if (rt.IsReferenceTypes()) {
3950 table->SetRegCategory(i, r, kRegObject);
3951 } else {
3952 table->SetRegCategory(i, r, kRegUnknown);
3953 }
3954 }
3955 }
3956 }
3957
3958 return table.release();
3959}
3960#endif
3961
Ian Rogersd81871c2011-10-03 13:57:23 -07003962} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003963} // namespace art