blob: 8776458b67a12f27a82c3b4935b7824b3a3ae235 [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
35namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070036namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
Ian Rogers2c8a8572011-10-24 17:11:36 -070038static const bool gDebugVerify = false;
39
Ian Rogersd81871c2011-10-03 13:57:23 -070040std::ostream& operator<<(std::ostream& os, const VerifyError& rhs) {
Brian Carlstrom75412882012-01-18 01:26:54 -080041 switch (rhs) {
42 case VERIFY_ERROR_NONE: os << "VERIFY_ERROR_NONE"; break;
43 case VERIFY_ERROR_GENERIC: os << "VERIFY_ERROR_GENERIC"; break;
44 case VERIFY_ERROR_NO_CLASS: os << "VERIFY_ERROR_NO_CLASS"; break;
45 case VERIFY_ERROR_NO_FIELD: os << "VERIFY_ERROR_NO_FIELD"; break;
46 case VERIFY_ERROR_NO_METHOD: os << "VERIFY_ERROR_NO_METHOD"; break;
47 case VERIFY_ERROR_ACCESS_CLASS: os << "VERIFY_ERROR_ACCESS_CLASS"; break;
48 case VERIFY_ERROR_ACCESS_FIELD: os << "VERIFY_ERROR_ACCESS_FIELD"; break;
49 case VERIFY_ERROR_ACCESS_METHOD: os << "VERIFY_ERROR_ACCESS_METHOD"; break;
50 case VERIFY_ERROR_CLASS_CHANGE: os << "VERIFY_ERROR_CLASS_CHANGE"; break;
51 case VERIFY_ERROR_INSTANTIATION: os << "VERIFY_ERROR_INSTANTIATION"; break;
52 default:
53 os << "VerifyError[" << static_cast<int>(rhs) << "]";
54 break;
55 }
56 return os;
Ian Rogersd81871c2011-10-03 13:57:23 -070057}
jeffhaobdb76512011-09-07 11:43:16 -070058
Ian Rogers84fa0742011-10-25 18:13:30 -070059static const char* type_strings[] = {
60 "Unknown",
61 "Conflict",
62 "Boolean",
63 "Byte",
64 "Short",
65 "Char",
66 "Integer",
67 "Float",
68 "Long (Low Half)",
69 "Long (High Half)",
70 "Double (Low Half)",
71 "Double (High Half)",
72 "64-bit Constant (Low Half)",
73 "64-bit Constant (High Half)",
74 "32-bit Constant",
75 "Unresolved Reference",
76 "Uninitialized Reference",
77 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070078 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070079 "Reference",
80};
Ian Rogersd81871c2011-10-03 13:57:23 -070081
Ian Rogers2c8a8572011-10-24 17:11:36 -070082std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070083 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
84 std::string result;
85 if (IsConstant()) {
86 uint32_t val = ConstantValue();
87 if (val == 0) {
88 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070089 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -070090 if(IsConstantShort()) {
91 result = StringPrintf("32-bit Constant: %d", val);
92 } else {
93 result = StringPrintf("32-bit Constant: 0x%x", val);
94 }
95 }
96 } else {
97 result = type_strings[type_];
98 if (IsReferenceTypes()) {
99 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -0700100 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700101 result += PrettyDescriptor(GetDescriptor());
102 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800103 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -0700104 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700105 }
106 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700107 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -0700108}
109
110const RegType& RegType::HighHalf(RegTypeCache* cache) const {
111 CHECK(IsLowHalf());
112 if (type_ == kRegTypeLongLo) {
113 return cache->FromType(kRegTypeLongHi);
114 } else if (type_ == kRegTypeDoubleLo) {
115 return cache->FromType(kRegTypeDoubleHi);
116 } else {
117 return cache->FromType(kRegTypeConstHi);
118 }
119}
120
121/*
122 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
123 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
124 * 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
125 * is the deepest (lowest upper bound) parent of S and T).
126 *
127 * This operation applies for regular classes and arrays, however, for interface types there needn't
128 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
129 * introducing sets of types, however, the only operation permissible on an interface is
130 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
131 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700132 * the perversion of any Object being assignable to an interface type (note, however, that we don't
133 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
134 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700135 */
136Class* RegType::ClassJoin(Class* s, Class* t) {
137 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
138 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
139 if (s == t) {
140 return s;
141 } else if (s->IsAssignableFrom(t)) {
142 return s;
143 } else if (t->IsAssignableFrom(s)) {
144 return t;
145 } else if (s->IsArrayClass() && t->IsArrayClass()) {
146 Class* s_ct = s->GetComponentType();
147 Class* t_ct = t->GetComponentType();
148 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
149 // Given the types aren't the same, if either array is of primitive types then the only
150 // common parent is java.lang.Object
151 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
152 DCHECK(result->IsObjectClass());
153 return result;
154 }
155 Class* common_elem = ClassJoin(s_ct, t_ct);
156 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
157 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800158 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700160 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
161 DCHECK(array_class != NULL);
162 return array_class;
163 } else {
164 size_t s_depth = s->Depth();
165 size_t t_depth = t->Depth();
166 // Get s and t to the same depth in the hierarchy
167 if (s_depth > t_depth) {
168 while (s_depth > t_depth) {
169 s = s->GetSuperClass();
170 s_depth--;
171 }
172 } else {
173 while (t_depth > s_depth) {
174 t = t->GetSuperClass();
175 t_depth--;
176 }
177 }
178 // Go up the hierarchy until we get to the common parent
179 while (s != t) {
180 s = s->GetSuperClass();
181 t = t->GetSuperClass();
182 }
183 return s;
184 }
185}
186
Ian Rogersb5e95b92011-10-25 23:28:55 -0700187bool RegType::IsAssignableFrom(const RegType& src) const {
188 if (Equals(src)) {
189 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700190 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700191 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700192 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
193 case RegType::kRegTypeByte: return src.IsByteTypes();
194 case RegType::kRegTypeShort: return src.IsShortTypes();
195 case RegType::kRegTypeChar: return src.IsCharTypes();
196 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
197 case RegType::kRegTypeFloat: return src.IsFloatTypes();
198 case RegType::kRegTypeLongLo: return src.IsLongTypes();
199 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700200 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700201 if (!IsReferenceTypes()) {
202 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700203 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700204 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700205 return true; // all reference types can be assigned null
206 } else if (!src.IsReferenceTypes()) {
207 return false; // expect src to be a reference type
208 } else if (IsJavaLangObject()) {
209 return true; // all reference types can be assigned to Object
210 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700211 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers9074b992011-10-26 17:41:55 -0700212 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700213 GetClass()->IsAssignableFrom(src.GetClass())) {
214 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700215 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700216 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700217 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700218 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700219 }
220 }
221}
222
Ian Rogers84fa0742011-10-25 18:13:30 -0700223static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
224 return a.IsConstant() ? b : a;
225}
jeffhaobdb76512011-09-07 11:43:16 -0700226
Ian Rogersd81871c2011-10-03 13:57:23 -0700227const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
228 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700229 if (IsUnknown() && incoming_type.IsUnknown()) {
230 return *this; // Unknown MERGE Unknown => Unknown
231 } else if (IsConflict()) {
232 return *this; // Conflict MERGE * => Conflict
233 } else if (incoming_type.IsConflict()) {
234 return incoming_type; // * MERGE Conflict => Conflict
235 } else if (IsUnknown() || incoming_type.IsUnknown()) {
236 return reg_types->Conflict(); // Unknown MERGE * => Conflict
237 } else if(IsConstant() && incoming_type.IsConstant()) {
238 int32_t val1 = ConstantValue();
239 int32_t val2 = incoming_type.ConstantValue();
240 if (val1 >= 0 && val2 >= 0) {
241 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
242 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700243 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700244 } else {
245 return incoming_type;
246 }
247 } else if (val1 < 0 && val2 < 0) {
248 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
249 if (val1 <= val2) {
250 return *this;
251 } else {
252 return incoming_type;
253 }
254 } else {
255 // Values are +ve and -ve, choose smallest signed type in which they both fit
256 if (IsConstantByte()) {
257 if (incoming_type.IsConstantByte()) {
258 return reg_types->ByteConstant();
259 } else if (incoming_type.IsConstantShort()) {
260 return reg_types->ShortConstant();
261 } else {
262 return reg_types->IntConstant();
263 }
264 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700265 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700266 return reg_types->ShortConstant();
267 } else {
268 return reg_types->IntConstant();
269 }
270 } else {
271 return reg_types->IntConstant();
272 }
273 }
274 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
275 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
276 return reg_types->Boolean(); // boolean MERGE boolean => boolean
277 }
278 if (IsByteTypes() && incoming_type.IsByteTypes()) {
279 return reg_types->Byte(); // byte MERGE byte => byte
280 }
281 if (IsShortTypes() && incoming_type.IsShortTypes()) {
282 return reg_types->Short(); // short MERGE short => short
283 }
284 if (IsCharTypes() && incoming_type.IsCharTypes()) {
285 return reg_types->Char(); // char MERGE char => char
286 }
287 return reg_types->Integer(); // int MERGE * => int
288 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
289 (IsLongTypes() && incoming_type.IsLongTypes()) ||
290 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
291 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
292 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
293 // check constant case was handled prior to entry
294 DCHECK(!IsConstant() || !incoming_type.IsConstant());
295 // float/long/double MERGE float/long/double_constant => float/long/double
296 return SelectNonConstant(*this, incoming_type);
297 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700298 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700299 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700300 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
301 return reg_types->JavaLangObject(); // Object MERGE ref => Object
302 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
303 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
304 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
305 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700306 return reg_types->Conflict();
307 } else { // Two reference types, compute Join
308 Class* c1 = GetClass();
309 Class* c2 = incoming_type.GetClass();
310 DCHECK(c1 != NULL && !c1->IsPrimitive());
311 DCHECK(c2 != NULL && !c2->IsPrimitive());
312 Class* join_class = ClassJoin(c1, c2);
313 if (c1 == join_class) {
314 return *this;
315 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700316 return incoming_type;
317 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700318 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700320 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700321 } else {
322 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700323 }
324}
325
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700326static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700328 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
329 case Primitive::kPrimByte: return RegType::kRegTypeByte;
330 case Primitive::kPrimShort: return RegType::kRegTypeShort;
331 case Primitive::kPrimChar: return RegType::kRegTypeChar;
332 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
333 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
334 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
335 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
336 case Primitive::kPrimVoid:
337 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700338 }
339}
340
341static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
342 if (descriptor.length() == 1) {
343 switch (descriptor[0]) {
344 case 'Z': return RegType::kRegTypeBoolean;
345 case 'B': return RegType::kRegTypeByte;
346 case 'S': return RegType::kRegTypeShort;
347 case 'C': return RegType::kRegTypeChar;
348 case 'I': return RegType::kRegTypeInteger;
349 case 'J': return RegType::kRegTypeLongLo;
350 case 'F': return RegType::kRegTypeFloat;
351 case 'D': return RegType::kRegTypeDoubleLo;
352 case 'V':
353 default: return RegType::kRegTypeUnknown;
354 }
355 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
356 return RegType::kRegTypeReference;
357 } else {
358 return RegType::kRegTypeUnknown;
359 }
360}
361
362std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700363 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700364 return os;
365}
366
367const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800368 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
370}
371
372const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800373 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700374 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 // entries should be sized greater than primitive types
376 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
377 RegType* entry = entries_[type];
378 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700379 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800380 if (strlen(descriptor) != 0) {
381 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700382 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700383 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700384 entries_[type] = entry;
385 }
386 return *entry;
387 } else {
388 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700390 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700391 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700392 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 if (cur_entry->IsReference()) {
394 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800395 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800396 return *cur_entry;
397 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700398 } else if (cur_entry->IsUnresolvedReference() &&
399 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 return *cur_entry;
401 }
402 }
Ian Rogers672297c2012-01-10 14:50:55 -0800403 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700404 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700405 // Able to resolve so create resolved register type
406 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700407 entries_.push_back(entry);
408 return *entry;
409 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700410 // TODO: we assume unresolved, but we may be able to do better by validating whether the
411 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700412 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700413 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700414 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800415 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700416 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800417 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700418 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
419 entries_.size());
420 entries_.push_back(entry);
421 return *entry;
422 } else {
423 // The descriptor is broken return the unknown type as there's nothing sensible that
424 // could be done at runtime
425 return Unknown();
426 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700427 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 }
429}
430
431const RegType& RegTypeCache::FromClass(Class* klass) {
432 if (klass->IsPrimitive()) {
433 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
434 // entries should be sized greater than primitive types
435 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
436 RegType* entry = entries_[type];
437 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700438 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 entries_[type] = entry;
440 }
441 return *entry;
442 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700443 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700444 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700445 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700446 return *cur_entry;
447 }
448 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700449 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700450 entries_.push_back(entry);
451 return *entry;
452 }
453}
454
Ian Rogers28ad40d2011-10-27 15:19:26 -0700455const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
456 RegType* entry;
457 if (type.IsUnresolvedTypes()) {
458 String* descriptor = type.GetDescriptor();
459 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
460 RegType* cur_entry = entries_[i];
461 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
462 cur_entry->GetAllocationPc() == allocation_pc &&
463 cur_entry->GetDescriptor() == descriptor) {
464 return *cur_entry;
465 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700467 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
468 descriptor, allocation_pc, entries_.size());
469 } else {
470 Class* klass = type.GetClass();
471 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
472 RegType* cur_entry = entries_[i];
473 if (cur_entry->IsUninitializedReference() &&
474 cur_entry->GetAllocationPc() == allocation_pc &&
475 cur_entry->GetClass() == klass) {
476 return *cur_entry;
477 }
478 }
479 entry = new RegType(RegType::kRegTypeUninitializedReference,
480 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700481 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700482 entries_.push_back(entry);
483 return *entry;
484}
485
486const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
487 RegType* entry;
488 if (uninit_type.IsUnresolvedTypes()) {
489 String* descriptor = uninit_type.GetDescriptor();
490 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
491 RegType* cur_entry = entries_[i];
492 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
493 return *cur_entry;
494 }
495 }
496 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
497 } else {
498 Class* klass = uninit_type.GetClass();
499 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
500 RegType* cur_entry = entries_[i];
501 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
502 return *cur_entry;
503 }
504 }
505 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
506 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 entries_.push_back(entry);
508 return *entry;
509}
510
511const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700512 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 RegType* cur_entry = entries_[i];
514 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
515 return *cur_entry;
516 }
517 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700518 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 entries_.size());
520 entries_.push_back(entry);
521 return *entry;
522}
523
524const RegType& RegTypeCache::FromType(RegType::Type type) {
525 CHECK(type < RegType::kRegTypeReference);
526 switch (type) {
527 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
528 case RegType::kRegTypeByte: return From(type, NULL, "B");
529 case RegType::kRegTypeShort: return From(type, NULL, "S");
530 case RegType::kRegTypeChar: return From(type, NULL, "C");
531 case RegType::kRegTypeInteger: return From(type, NULL, "I");
532 case RegType::kRegTypeFloat: return From(type, NULL, "F");
533 case RegType::kRegTypeLongLo:
534 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
535 case RegType::kRegTypeDoubleLo:
536 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
537 default: return From(type, NULL, "");
538 }
539}
540
541const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700542 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
543 RegType* cur_entry = entries_[i];
544 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
545 return *cur_entry;
546 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700548 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
549 entries_.push_back(entry);
550 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700551}
552
Ian Rogers28ad40d2011-10-27 15:19:26 -0700553const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
554 CHECK(array.IsArrayClass());
555 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800556 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
557 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800558 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700559 } else {
560 return FromClass(array.GetClass()->GetComponentType());
561 }
562}
563
564
Ian Rogersd81871c2011-10-03 13:57:23 -0700565bool RegisterLine::CheckConstructorReturn() const {
566 for (size_t i = 0; i < num_regs_; i++) {
567 if (GetRegisterType(i).IsUninitializedThisReference()) {
568 verifier_->Fail(VERIFY_ERROR_GENERIC)
569 << "Constructor returning without calling superclass constructor";
570 return false;
571 }
572 }
573 return true;
574}
575
576void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
577 DCHECK(vdst < num_regs_);
578 if (new_type.IsLowHalf()) {
579 line_[vdst] = new_type.GetId();
580 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
581 } else if (new_type.IsHighHalf()) {
582 /* should never set these explicitly */
583 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
584 } else if (new_type.IsConflict()) { // should only be set during a merge
585 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
586 } else {
587 line_[vdst] = new_type.GetId();
588 }
589 // Clear the monitor entry bits for this register.
590 ClearAllRegToLockDepths(vdst);
591}
592
593void RegisterLine::SetResultTypeToUnknown() {
594 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
595 result_[0] = unknown_id;
596 result_[1] = unknown_id;
597}
598
599void RegisterLine::SetResultRegisterType(const RegType& new_type) {
600 result_[0] = new_type.GetId();
601 if(new_type.IsLowHalf()) {
602 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
603 result_[1] = new_type.GetId() + 1;
604 } else {
605 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
606 }
607}
608
609const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
610 // The register index was validated during the static pass, so we don't need to check it here.
611 DCHECK_LT(vsrc, num_regs_);
612 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
613}
614
615const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
616 if (dec_insn.vA_ < 1) {
617 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
618 return verifier_->GetRegTypeCache()->Unknown();
619 }
620 /* get the element type of the array held in vsrc */
621 const RegType& this_type = GetRegisterType(dec_insn.vC_);
622 if (!this_type.IsReferenceTypes()) {
623 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
624 << dec_insn.vC_ << " (type=" << this_type << ")";
625 return verifier_->GetRegTypeCache()->Unknown();
626 }
627 return this_type;
628}
629
630Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
631 /* get the element type of the array held in vsrc */
632 const RegType& type = GetRegisterType(vsrc);
633 /* if "always zero", we allow it to fail at runtime */
634 if (type.IsZero()) {
635 return NULL;
636 } else if (!type.IsReferenceTypes()) {
637 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
638 << " (type=" << type << ")";
639 return NULL;
640 } else if (type.IsUninitializedReference()) {
641 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
642 return NULL;
643 } else {
644 return type.GetClass();
645 }
646}
647
648bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
649 // Verify the src register type against the check type refining the type of the register
650 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700651 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
653 << " but expected " << check_type;
654 return false;
655 }
656 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
657 // precise than the subtype in vsrc so leave it for reference types. For primitive types
658 // if they are a defined type then they are as precise as we can get, however, for constant
659 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
660 return true;
661}
662
663void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700664 DCHECK(uninit_type.IsUninitializedTypes());
665 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
666 size_t changed = 0;
667 for (size_t i = 0; i < num_regs_; i++) {
668 if (GetRegisterType(i).Equals(uninit_type)) {
669 line_[i] = init_type.GetId();
670 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700673 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674}
675
676void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
677 for (size_t i = 0; i < num_regs_; i++) {
678 if (GetRegisterType(i).Equals(uninit_type)) {
679 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
680 ClearAllRegToLockDepths(i);
681 }
682 }
683}
684
685void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
686 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
687 const RegType& type = GetRegisterType(vsrc);
688 SetRegisterType(vdst, type);
689 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
690 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
691 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
692 << " cat=" << static_cast<int>(cat);
693 } else if (cat == kTypeCategoryRef) {
694 CopyRegToLockDepth(vdst, vsrc);
695 }
696}
697
698void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
699 const RegType& type_l = GetRegisterType(vsrc);
700 const RegType& type_h = GetRegisterType(vsrc + 1);
701
702 if (!type_l.CheckWidePair(type_h)) {
703 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
704 << " type=" << type_l << "/" << type_h;
705 } else {
706 SetRegisterType(vdst, type_l); // implicitly sets the second half
707 }
708}
709
710void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
711 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
712 if ((!is_reference && !type.IsCategory1Types()) ||
713 (is_reference && !type.IsReferenceTypes())) {
714 verifier_->Fail(VERIFY_ERROR_GENERIC)
715 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
716 } else {
717 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
718 SetRegisterType(vdst, type);
719 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
720 }
721}
722
723/*
724 * Implement "move-result-wide". Copy the category-2 value from the result
725 * register to another register, and reset the result register.
726 */
727void RegisterLine::CopyResultRegister2(uint32_t vdst) {
728 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
729 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
730 if (!type_l.IsCategory2Types()) {
731 verifier_->Fail(VERIFY_ERROR_GENERIC)
732 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
733 } else {
734 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
735 SetRegisterType(vdst, type_l); // also sets the high
736 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
737 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
738 }
739}
740
741void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
742 const RegType& dst_type, const RegType& src_type) {
743 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
744 SetRegisterType(dec_insn.vA_, dst_type);
745 }
746}
747
748void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
749 const RegType& dst_type,
750 const RegType& src_type1, const RegType& src_type2,
751 bool check_boolean_op) {
752 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
753 VerifyRegisterType(dec_insn.vC_, src_type2)) {
754 if (check_boolean_op) {
755 DCHECK(dst_type.IsInteger());
756 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
757 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
758 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
759 return;
760 }
761 }
762 SetRegisterType(dec_insn.vA_, dst_type);
763 }
764}
765
766void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
767 const RegType& dst_type, const RegType& src_type1,
768 const RegType& src_type2, bool check_boolean_op) {
769 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
770 VerifyRegisterType(dec_insn.vB_, src_type2)) {
771 if (check_boolean_op) {
772 DCHECK(dst_type.IsInteger());
773 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
774 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
775 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
776 return;
777 }
778 }
779 SetRegisterType(dec_insn.vA_, dst_type);
780 }
781}
782
783void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
784 const RegType& dst_type, const RegType& src_type,
785 bool check_boolean_op) {
786 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
787 if (check_boolean_op) {
788 DCHECK(dst_type.IsInteger());
789 /* check vB with the call, then check the constant manually */
790 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
791 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
792 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
793 return;
794 }
795 }
796 SetRegisterType(dec_insn.vA_, dst_type);
797 }
798}
799
800void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
801 const RegType& reg_type = GetRegisterType(reg_idx);
802 if (!reg_type.IsReferenceTypes()) {
803 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800804 } else if (monitors_.size() >= 32) {
805 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 } else {
807 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700808 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 }
810}
811
812void RegisterLine::PopMonitor(uint32_t reg_idx) {
813 const RegType& reg_type = GetRegisterType(reg_idx);
814 if (!reg_type.IsReferenceTypes()) {
815 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
816 } else if (monitors_.empty()) {
817 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
818 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700819 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
821 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
822 // format "036" the constant collector may create unlocks on the same object but referenced
823 // via different registers.
824 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
825 : verifier_->LogVerifyInfo())
826 << "monitor-exit not unlocking the top of the monitor stack";
827 } else {
828 // Record the register was unlocked
829 ClearRegToLockDepth(reg_idx, monitors_.size());
830 }
831 }
832}
833
834bool RegisterLine::VerifyMonitorStackEmpty() {
835 if (MonitorStackDepth() != 0) {
836 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
837 return false;
838 } else {
839 return true;
840 }
841}
842
843bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
844 bool changed = false;
845 for (size_t idx = 0; idx < num_regs_; idx++) {
846 if (line_[idx] != incoming_line->line_[idx]) {
847 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
848 const RegType& cur_type = GetRegisterType(idx);
849 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
850 changed = changed || !cur_type.Equals(new_type);
851 line_[idx] = new_type.GetId();
852 }
853 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700854 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
856 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
857 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
858 for (uint32_t idx = 0; idx < num_regs_; idx++) {
859 size_t depths = reg_to_lock_depths_.count(idx);
860 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
861 if (depths != incoming_depths) {
862 if (depths == 0 || incoming_depths == 0) {
863 reg_to_lock_depths_.erase(idx);
864 } else {
865 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
866 << ": " << depths << " != " << incoming_depths;
867 break;
868 }
869 }
870 }
871 }
872 return changed;
873}
874
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800875void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 for (size_t i = 0; i < num_regs_; i += 8) {
877 uint8_t val = 0;
878 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
879 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700880 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 val |= 1 << j;
882 }
883 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800884 if ((i / 8) >= max_bytes) {
885 DCHECK_EQ(0, val);
886 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800888 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
889 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 }
891}
892
893std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700894 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 return os;
896}
897
898
899void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
900 uint32_t insns_size, uint16_t registers_size,
901 DexVerifier* verifier) {
902 DCHECK_GT(insns_size, 0U);
903
904 for (uint32_t i = 0; i < insns_size; i++) {
905 bool interesting = false;
906 switch (mode) {
907 case kTrackRegsAll:
908 interesting = flags[i].IsOpcode();
909 break;
910 case kTrackRegsGcPoints:
911 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
912 break;
913 case kTrackRegsBranches:
914 interesting = flags[i].IsBranchTarget();
915 break;
916 default:
917 break;
918 }
919 if (interesting) {
920 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
921 }
922 }
923}
924
Ian Rogers1c5eb702012-02-01 09:18:34 -0800925bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700926 if (klass->IsVerified()) {
927 return true;
928 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800930 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800931 error = "Verifier rejected class ";
932 error += PrettyDescriptor(klass);
933 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 return false;
935 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800936 if (super != NULL && super->IsFinal()) {
937 error = "Verifier rejected class ";
938 error += PrettyDescriptor(klass);
939 error += " that attempts to sub-class final class ";
940 error += PrettyDescriptor(super);
941 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 }
jeffhaobdb76512011-09-07 11:43:16 -0700943 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
944 Method* method = klass->GetDirectMethod(i);
945 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800946 error = "Verifier rejected class ";
947 error += PrettyDescriptor(klass);
948 error += " due to bad method ";
949 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700950 return false;
951 }
952 }
953 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
954 Method* method = klass->GetVirtualMethod(i);
955 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800956 error = "Verifier rejected class ";
957 error += PrettyDescriptor(klass);
958 error += " due to bad method ";
959 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700960 return false;
961 }
962 }
963 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700964}
965
jeffhaobdb76512011-09-07 11:43:16 -0700966bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 DexVerifier verifier(method);
968 bool success = verifier.Verify();
Brian Carlstrom75412882012-01-18 01:26:54 -0800969 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
970
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 // We expect either success and no verification error, or failure and a generic failure to
972 // reject the class.
973 if (success) {
974 if (verifier.failure_ != VERIFY_ERROR_NONE) {
975 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
976 << verifier.fail_messages_;
977 }
978 } else {
979 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700980 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700981 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700982 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700983 verifier.Dump(std::cout);
984 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
986 }
987 return success;
988}
989
Shih-wei Liao371814f2011-10-27 16:52:10 -0700990void DexVerifier::VerifyMethodAndDump(Method* method) {
991 DexVerifier verifier(method);
992 verifier.Verify();
993
Elliott Hughese0918552011-10-28 17:18:29 -0700994 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
995 << verifier.fail_messages_.str() << std::endl
996 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700997}
998
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800999DexVerifier::DexVerifier(Method* method)
1000 : work_insn_idx_(-1),
1001 method_(method),
1002 failure_(VERIFY_ERROR_NONE),
1003 new_instance_count_(0),
1004 monitor_enter_count_(0) {
1005 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001006 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001007 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 dex_file_ = &class_linker->FindDexFile(dex_cache);
1009 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001010}
1011
Ian Rogersd81871c2011-10-03 13:57:23 -07001012bool DexVerifier::Verify() {
1013 // If there aren't any instructions, make sure that's expected, then exit successfully.
1014 if (code_item_ == NULL) {
1015 if (!method_->IsNative() && !method_->IsAbstract()) {
1016 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001017 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 } else {
1019 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001020 }
jeffhaobdb76512011-09-07 11:43:16 -07001021 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1023 if (code_item_->ins_size_ > code_item_->registers_size_) {
1024 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1025 << " regs=" << code_item_->registers_size_;
1026 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001027 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 // Allocate and initialize an array to hold instruction data.
1029 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1030 // Run through the instructions and see if the width checks out.
1031 bool result = ComputeWidthsAndCountOps();
1032 // Flag instructions guarded by a "try" block and check exception handlers.
1033 result = result && ScanTryCatchBlocks();
1034 // Perform static instruction verification.
1035 result = result && VerifyInstructions();
1036 // Perform code flow analysis.
1037 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001038 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001039}
1040
Ian Rogersd81871c2011-10-03 13:57:23 -07001041bool DexVerifier::ComputeWidthsAndCountOps() {
1042 const uint16_t* insns = code_item_->insns_;
1043 size_t insns_size = code_item_->insns_size_in_code_units_;
1044 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001045 size_t new_instance_count = 0;
1046 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001048
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001050 Instruction::Code opcode = inst->Opcode();
1051 if (opcode == Instruction::NEW_INSTANCE) {
1052 new_instance_count++;
1053 } else if (opcode == Instruction::MONITOR_ENTER) {
1054 monitor_enter_count++;
1055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 size_t inst_size = inst->SizeInCodeUnits();
1057 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1058 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001059 inst = inst->Next();
1060 }
1061
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 if (dex_pc != insns_size) {
1063 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1064 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001065 return false;
1066 }
1067
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 new_instance_count_ = new_instance_count;
1069 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001070 return true;
1071}
1072
Ian Rogersd81871c2011-10-03 13:57:23 -07001073bool DexVerifier::ScanTryCatchBlocks() {
1074 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001075 if (tries_size == 0) {
1076 return true;
1077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001079 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001080
1081 for (uint32_t idx = 0; idx < tries_size; idx++) {
1082 const DexFile::TryItem* try_item = &tries[idx];
1083 uint32_t start = try_item->start_addr_;
1084 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001085 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1087 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001088 return false;
1089 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 if (!insn_flags_[start].IsOpcode()) {
1091 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001092 return false;
1093 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 for (uint32_t dex_pc = start; dex_pc < end;
1095 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1096 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001097 }
1098 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001099 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001100 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001101 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001102 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001103 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001104 CatchHandlerIterator iterator(handlers_ptr);
1105 for (; iterator.HasNext(); iterator.Next()) {
1106 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 if (!insn_flags_[dex_pc].IsOpcode()) {
1108 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001109 return false;
1110 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001112 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1113 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001114 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1115 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001116 if (exception_type == NULL) {
1117 DCHECK(Thread::Current()->IsExceptionPending());
1118 Thread::Current()->ClearException();
1119 }
1120 }
jeffhaobdb76512011-09-07 11:43:16 -07001121 }
Ian Rogers0571d352011-11-03 19:51:38 -07001122 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001123 }
jeffhaobdb76512011-09-07 11:43:16 -07001124 return true;
1125}
1126
Ian Rogersd81871c2011-10-03 13:57:23 -07001127bool DexVerifier::VerifyInstructions() {
1128 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001129
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 /* Flag the start of the method as a branch target. */
1131 insn_flags_[0].SetBranchTarget();
1132
1133 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1134 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1135 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001136 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1137 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return false;
1139 }
1140 /* Flag instructions that are garbage collection points */
1141 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1142 insn_flags_[dex_pc].SetGcPoint();
1143 }
1144 dex_pc += inst->SizeInCodeUnits();
1145 inst = inst->Next();
1146 }
1147 return true;
1148}
1149
1150bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1151 Instruction::DecodedInstruction dec_insn(inst);
1152 bool result = true;
1153 switch (inst->GetVerifyTypeArgumentA()) {
1154 case Instruction::kVerifyRegA:
1155 result = result && CheckRegisterIndex(dec_insn.vA_);
1156 break;
1157 case Instruction::kVerifyRegAWide:
1158 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1159 break;
1160 }
1161 switch (inst->GetVerifyTypeArgumentB()) {
1162 case Instruction::kVerifyRegB:
1163 result = result && CheckRegisterIndex(dec_insn.vB_);
1164 break;
1165 case Instruction::kVerifyRegBField:
1166 result = result && CheckFieldIndex(dec_insn.vB_);
1167 break;
1168 case Instruction::kVerifyRegBMethod:
1169 result = result && CheckMethodIndex(dec_insn.vB_);
1170 break;
1171 case Instruction::kVerifyRegBNewInstance:
1172 result = result && CheckNewInstance(dec_insn.vB_);
1173 break;
1174 case Instruction::kVerifyRegBString:
1175 result = result && CheckStringIndex(dec_insn.vB_);
1176 break;
1177 case Instruction::kVerifyRegBType:
1178 result = result && CheckTypeIndex(dec_insn.vB_);
1179 break;
1180 case Instruction::kVerifyRegBWide:
1181 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1182 break;
1183 }
1184 switch (inst->GetVerifyTypeArgumentC()) {
1185 case Instruction::kVerifyRegC:
1186 result = result && CheckRegisterIndex(dec_insn.vC_);
1187 break;
1188 case Instruction::kVerifyRegCField:
1189 result = result && CheckFieldIndex(dec_insn.vC_);
1190 break;
1191 case Instruction::kVerifyRegCNewArray:
1192 result = result && CheckNewArray(dec_insn.vC_);
1193 break;
1194 case Instruction::kVerifyRegCType:
1195 result = result && CheckTypeIndex(dec_insn.vC_);
1196 break;
1197 case Instruction::kVerifyRegCWide:
1198 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1199 break;
1200 }
1201 switch (inst->GetVerifyExtraFlags()) {
1202 case Instruction::kVerifyArrayData:
1203 result = result && CheckArrayData(code_offset);
1204 break;
1205 case Instruction::kVerifyBranchTarget:
1206 result = result && CheckBranchTarget(code_offset);
1207 break;
1208 case Instruction::kVerifySwitchTargets:
1209 result = result && CheckSwitchTargets(code_offset);
1210 break;
1211 case Instruction::kVerifyVarArg:
1212 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1213 break;
1214 case Instruction::kVerifyVarArgRange:
1215 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1216 break;
1217 case Instruction::kVerifyError:
1218 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1219 result = false;
1220 break;
1221 }
1222 return result;
1223}
1224
1225bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1226 if (idx >= code_item_->registers_size_) {
1227 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1228 << code_item_->registers_size_ << ")";
1229 return false;
1230 }
1231 return true;
1232}
1233
1234bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1235 if (idx + 1 >= code_item_->registers_size_) {
1236 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1237 << "+1 >= " << code_item_->registers_size_ << ")";
1238 return false;
1239 }
1240 return true;
1241}
1242
1243bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1244 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1245 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1246 << dex_file_->GetHeader().field_ids_size_ << ")";
1247 return false;
1248 }
1249 return true;
1250}
1251
1252bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1253 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1254 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1255 << dex_file_->GetHeader().method_ids_size_ << ")";
1256 return false;
1257 }
1258 return true;
1259}
1260
1261bool DexVerifier::CheckNewInstance(uint32_t idx) {
1262 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1263 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1264 << dex_file_->GetHeader().type_ids_size_ << ")";
1265 return false;
1266 }
1267 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001268 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 if (descriptor[0] != 'L') {
1270 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1271 return false;
1272 }
1273 return true;
1274}
1275
1276bool DexVerifier::CheckStringIndex(uint32_t idx) {
1277 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1278 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1279 << dex_file_->GetHeader().string_ids_size_ << ")";
1280 return false;
1281 }
1282 return true;
1283}
1284
1285bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1286 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1287 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1288 << dex_file_->GetHeader().type_ids_size_ << ")";
1289 return false;
1290 }
1291 return true;
1292}
1293
1294bool DexVerifier::CheckNewArray(uint32_t idx) {
1295 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1296 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1297 << dex_file_->GetHeader().type_ids_size_ << ")";
1298 return false;
1299 }
1300 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001301 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 const char* cp = descriptor;
1303 while (*cp++ == '[') {
1304 bracket_count++;
1305 }
1306 if (bracket_count == 0) {
1307 /* The given class must be an array type. */
1308 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1309 return false;
1310 } else if (bracket_count > 255) {
1311 /* It is illegal to create an array of more than 255 dimensions. */
1312 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1313 return false;
1314 }
1315 return true;
1316}
1317
1318bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1319 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1320 const uint16_t* insns = code_item_->insns_ + cur_offset;
1321 const uint16_t* array_data;
1322 int32_t array_data_offset;
1323
1324 DCHECK_LT(cur_offset, insn_count);
1325 /* make sure the start of the array data table is in range */
1326 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1327 if ((int32_t) cur_offset + array_data_offset < 0 ||
1328 cur_offset + array_data_offset + 2 >= insn_count) {
1329 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1330 << ", data offset " << array_data_offset << ", count " << insn_count;
1331 return false;
1332 }
1333 /* offset to array data table is a relative branch-style offset */
1334 array_data = insns + array_data_offset;
1335 /* make sure the table is 32-bit aligned */
1336 if ((((uint32_t) array_data) & 0x03) != 0) {
1337 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1338 << ", data offset " << array_data_offset;
1339 return false;
1340 }
1341 uint32_t value_width = array_data[1];
1342 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1343 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1344 /* make sure the end of the switch is in range */
1345 if (cur_offset + array_data_offset + table_size > insn_count) {
1346 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1347 << ", data offset " << array_data_offset << ", end "
1348 << cur_offset + array_data_offset + table_size
1349 << ", count " << insn_count;
1350 return false;
1351 }
1352 return true;
1353}
1354
1355bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1356 int32_t offset;
1357 bool isConditional, selfOkay;
1358 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1359 return false;
1360 }
1361 if (!selfOkay && offset == 0) {
1362 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1363 return false;
1364 }
1365 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1366 // identical "wrap-around" behavior, but it's unwise to depend on that.
1367 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1368 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1369 return false;
1370 }
1371 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1372 int32_t abs_offset = cur_offset + offset;
1373 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1374 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1375 << (void*) abs_offset << ") at " << (void*) cur_offset;
1376 return false;
1377 }
1378 insn_flags_[abs_offset].SetBranchTarget();
1379 return true;
1380}
1381
1382bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1383 bool* selfOkay) {
1384 const uint16_t* insns = code_item_->insns_ + cur_offset;
1385 *pConditional = false;
1386 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 switch (*insns & 0xff) {
1388 case Instruction::GOTO:
1389 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001390 break;
1391 case Instruction::GOTO_32:
1392 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 *selfOkay = true;
1394 break;
1395 case Instruction::GOTO_16:
1396 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 break;
1398 case Instruction::IF_EQ:
1399 case Instruction::IF_NE:
1400 case Instruction::IF_LT:
1401 case Instruction::IF_GE:
1402 case Instruction::IF_GT:
1403 case Instruction::IF_LE:
1404 case Instruction::IF_EQZ:
1405 case Instruction::IF_NEZ:
1406 case Instruction::IF_LTZ:
1407 case Instruction::IF_GEZ:
1408 case Instruction::IF_GTZ:
1409 case Instruction::IF_LEZ:
1410 *pOffset = (int16_t) insns[1];
1411 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 break;
1413 default:
1414 return false;
1415 break;
1416 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 return true;
1418}
1419
Ian Rogersd81871c2011-10-03 13:57:23 -07001420bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1421 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001422 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1426 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1427 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1428 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001429 return false;
1430 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 /* make sure the table is 32-bit aligned */
1434 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1436 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001437 return false;
1438 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 uint32_t switch_count = switch_insns[1];
1440 int32_t keys_offset, targets_offset;
1441 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001442 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1443 /* 0=sig, 1=count, 2/3=firstKey */
1444 targets_offset = 4;
1445 keys_offset = -1;
1446 expected_signature = Instruction::kPackedSwitchSignature;
1447 } else {
1448 /* 0=sig, 1=count, 2..count*2 = keys */
1449 keys_offset = 2;
1450 targets_offset = 2 + 2 * switch_count;
1451 expected_signature = Instruction::kSparseSwitchSignature;
1452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001454 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001455 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1456 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 return false;
1458 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 /* make sure the end of the switch is in range */
1460 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1462 << switch_offset << ", end "
1463 << (cur_offset + switch_offset + table_size)
1464 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 return false;
1466 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 /* for a sparse switch, verify the keys are in ascending order */
1468 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1470 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1472 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1473 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1475 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 return false;
1477 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 last_key = key;
1479 }
1480 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 for (uint32_t targ = 0; targ < switch_count; targ++) {
1483 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1484 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1485 int32_t abs_offset = cur_offset + offset;
1486 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1487 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1488 << (void*) abs_offset << ") at "
1489 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 return false;
1491 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 insn_flags_[abs_offset].SetBranchTarget();
1493 }
1494 return true;
1495}
1496
1497bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1498 if (vA > 5) {
1499 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1500 return false;
1501 }
1502 uint16_t registers_size = code_item_->registers_size_;
1503 for (uint32_t idx = 0; idx < vA; idx++) {
1504 if (arg[idx] > registers_size) {
1505 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1506 << ") in non-range invoke (> " << registers_size << ")";
1507 return false;
1508 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 }
1510
1511 return true;
1512}
1513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1515 uint16_t registers_size = code_item_->registers_size_;
1516 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1517 // integer overflow when adding them here.
1518 if (vA + vC > registers_size) {
1519 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1520 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001521 return false;
1522 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 return true;
1524}
1525
Brian Carlstrom75412882012-01-18 01:26:54 -08001526const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1527 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1528 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1529 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1530 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1531 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1532 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1533 gc_map.begin(),
1534 gc_map.end());
1535 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1536 DCHECK_EQ(gc_map.size(),
1537 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1538 (length_prefixed_gc_map->at(1) << 16) |
1539 (length_prefixed_gc_map->at(2) << 8) |
1540 (length_prefixed_gc_map->at(3) << 0)));
1541 return length_prefixed_gc_map;
1542}
1543
Ian Rogersd81871c2011-10-03 13:57:23 -07001544bool DexVerifier::VerifyCodeFlow() {
1545 uint16_t registers_size = code_item_->registers_size_;
1546 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001547
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 if (registers_size * insns_size > 4*1024*1024) {
1549 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1550 << " insns_size=" << insns_size << ")";
1551 }
1552 /* Create and initialize table holding register status */
1553 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1554 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001555
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 work_line_.reset(new RegisterLine(registers_size, this));
1557 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001558
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 /* Initialize register types of method arguments. */
1560 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001561 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1562 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 return false;
1564 }
1565 /* Perform code flow verification. */
1566 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001567 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001569 }
1570
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001572 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1573 if (map.get() == NULL) {
1574 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 return false; // Not a real failure, but a failure to encode
1576 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001577#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001578 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001580 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1581 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1582 verifier::DexVerifier::SetGcMap(ref, *gc_map);
1583 method_->SetGcMap(&gc_map->at(0));
jeffhaobdb76512011-09-07 11:43:16 -07001584 return true;
1585}
1586
Ian Rogersd81871c2011-10-03 13:57:23 -07001587void DexVerifier::Dump(std::ostream& os) {
1588 if (method_->IsNative()) {
1589 os << "Native method" << std::endl;
1590 return;
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 DCHECK(code_item_ != NULL);
1593 const Instruction* inst = Instruction::At(code_item_->insns_);
1594 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1595 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001596 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001597 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1599 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001600 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001601 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001602 inst = inst->Next();
1603 }
jeffhaobdb76512011-09-07 11:43:16 -07001604}
1605
Ian Rogersd81871c2011-10-03 13:57:23 -07001606static bool IsPrimitiveDescriptor(char descriptor) {
1607 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001608 case 'I':
1609 case 'C':
1610 case 'S':
1611 case 'B':
1612 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001613 case 'F':
1614 case 'D':
1615 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001617 default:
1618 return false;
1619 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001620}
1621
Ian Rogersd81871c2011-10-03 13:57:23 -07001622bool DexVerifier::SetTypesFromSignature() {
1623 RegisterLine* reg_line = reg_table_.GetLine(0);
1624 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1625 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001626
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1628 //Include the "this" pointer.
1629 size_t cur_arg = 0;
1630 if (!method_->IsStatic()) {
1631 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1632 // argument as uninitialized. This restricts field access until the superclass constructor is
1633 // called.
1634 Class* declaring_class = method_->GetDeclaringClass();
1635 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1636 reg_line->SetRegisterType(arg_start + cur_arg,
1637 reg_types_.UninitializedThisArgument(declaring_class));
1638 } else {
1639 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001640 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
1643
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001644 const DexFile::ProtoId& proto_id =
1645 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001646 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001647
1648 for (; iterator.HasNext(); iterator.Next()) {
1649 const char* descriptor = iterator.GetDescriptor();
1650 if (descriptor == NULL) {
1651 LOG(FATAL) << "Null descriptor";
1652 }
1653 if (cur_arg >= expected_args) {
1654 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1655 << " args, found more (" << descriptor << ")";
1656 return false;
1657 }
1658 switch (descriptor[0]) {
1659 case 'L':
1660 case '[':
1661 // We assume that reference arguments are initialized. The only way it could be otherwise
1662 // (assuming the caller was verified) is if the current method is <init>, but in that case
1663 // it's effectively considered initialized the instant we reach here (in the sense that we
1664 // can return without doing anything or call virtual methods).
1665 {
1666 const RegType& reg_type =
1667 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001668 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 }
1670 break;
1671 case 'Z':
1672 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1673 break;
1674 case 'C':
1675 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1676 break;
1677 case 'B':
1678 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1679 break;
1680 case 'I':
1681 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1682 break;
1683 case 'S':
1684 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1685 break;
1686 case 'F':
1687 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1688 break;
1689 case 'J':
1690 case 'D': {
1691 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1692 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1693 cur_arg++;
1694 break;
1695 }
1696 default:
1697 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1698 return false;
1699 }
1700 cur_arg++;
1701 }
1702 if (cur_arg != expected_args) {
1703 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1704 return false;
1705 }
1706 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1707 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1708 // format. Only major difference from the method argument format is that 'V' is supported.
1709 bool result;
1710 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1711 result = descriptor[1] == '\0';
1712 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1713 size_t i = 0;
1714 do {
1715 i++;
1716 } while (descriptor[i] == '['); // process leading [
1717 if (descriptor[i] == 'L') { // object array
1718 do {
1719 i++; // find closing ;
1720 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1721 result = descriptor[i] == ';';
1722 } else { // primitive array
1723 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1724 }
1725 } else if (descriptor[0] == 'L') {
1726 // could be more thorough here, but shouldn't be required
1727 size_t i = 0;
1728 do {
1729 i++;
1730 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1731 result = descriptor[i] == ';';
1732 } else {
1733 result = false;
1734 }
1735 if (!result) {
1736 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1737 << descriptor << "'";
1738 }
1739 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001740}
1741
Ian Rogersd81871c2011-10-03 13:57:23 -07001742bool DexVerifier::CodeFlowVerifyMethod() {
1743 const uint16_t* insns = code_item_->insns_;
1744 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001745
jeffhaobdb76512011-09-07 11:43:16 -07001746 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 insn_flags_[0].SetChanged();
1748 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001749
jeffhaobdb76512011-09-07 11:43:16 -07001750 /* Continue until no instructions are marked "changed". */
1751 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1753 uint32_t insn_idx = start_guess;
1754 for (; insn_idx < insns_size; insn_idx++) {
1755 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001756 break;
1757 }
jeffhaobdb76512011-09-07 11:43:16 -07001758 if (insn_idx == insns_size) {
1759 if (start_guess != 0) {
1760 /* try again, starting from the top */
1761 start_guess = 0;
1762 continue;
1763 } else {
1764 /* all flags are clear */
1765 break;
1766 }
1767 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 // We carry the working set of registers from instruction to instruction. If this address can
1769 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1770 // "changed" flags, we need to load the set of registers from the table.
1771 // Because we always prefer to continue on to the next instruction, we should never have a
1772 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1773 // target.
1774 work_insn_idx_ = insn_idx;
1775 if (insn_flags_[insn_idx].IsBranchTarget()) {
1776 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001777 } else {
1778#ifndef NDEBUG
1779 /*
1780 * Sanity check: retrieve the stored register line (assuming
1781 * a full table) and make sure it actually matches.
1782 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1784 if (register_line != NULL) {
1785 if (work_line_->CompareLine(register_line) != 0) {
1786 Dump(std::cout);
1787 std::cout << info_messages_.str();
1788 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1789 << "@" << (void*)work_insn_idx_ << std::endl
1790 << " work_line=" << *work_line_ << std::endl
1791 << " expected=" << *register_line;
1792 }
jeffhaobdb76512011-09-07 11:43:16 -07001793 }
1794#endif
1795 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 if (!CodeFlowVerifyInstruction(&start_guess)) {
1797 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001798 return false;
1799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 insn_flags_[insn_idx].SetVisited();
1802 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001803 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001804
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001806 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001807 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001808 * (besides the wasted space), but it indicates a flaw somewhere
1809 * down the line, possibly in the verifier.
1810 *
1811 * If we've substituted "always throw" instructions into the stream,
1812 * we are almost certainly going to have some dead code.
1813 */
1814 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 uint32_t insn_idx = 0;
1816 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001817 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001818 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001819 * may or may not be preceded by a padding NOP (for alignment).
1820 */
1821 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1822 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1823 insns[insn_idx] == Instruction::kArrayDataSignature ||
1824 (insns[insn_idx] == Instruction::NOP &&
1825 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1826 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1827 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001829 }
1830
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001832 if (dead_start < 0)
1833 dead_start = insn_idx;
1834 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001836 dead_start = -1;
1837 }
1838 }
1839 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001841 }
1842 }
jeffhaobdb76512011-09-07 11:43:16 -07001843 return true;
1844}
1845
Ian Rogersd81871c2011-10-03 13:57:23 -07001846bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001847#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001849 gDvm.verifierStats.instrsReexamined++;
1850 } else {
1851 gDvm.verifierStats.instrsExamined++;
1852 }
1853#endif
1854
1855 /*
1856 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001857 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001858 * control to another statement:
1859 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001860 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001861 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001862 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001863 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001864 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001865 * throw an exception that is handled by an encompassing "try"
1866 * block.
1867 *
1868 * We can also return, in which case there is no successor instruction
1869 * from this point.
1870 *
1871 * The behavior can be determined from the OpcodeFlags.
1872 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1874 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001875 Instruction::DecodedInstruction dec_insn(inst);
1876 int opcode_flag = inst->Flag();
1877
jeffhaobdb76512011-09-07 11:43:16 -07001878 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001879 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001880 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001882 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1883 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 }
jeffhaobdb76512011-09-07 11:43:16 -07001885
1886 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001887 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001888 * can throw an exception, we will copy/merge this into the "catch"
1889 * address rather than work_line, because we don't want the result
1890 * from the "successful" code path (e.g. a check-cast that "improves"
1891 * a type) to be visible to the exception handler.
1892 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1894 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001895 } else {
1896#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001898#endif
1899 }
1900
1901 switch (dec_insn.opcode_) {
1902 case Instruction::NOP:
1903 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001904 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001905 * a signature that looks like a NOP; if we see one of these in
1906 * the course of executing code then we have a problem.
1907 */
1908 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
1911 break;
1912
1913 case Instruction::MOVE:
1914 case Instruction::MOVE_FROM16:
1915 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918 case Instruction::MOVE_WIDE:
1919 case Instruction::MOVE_WIDE_FROM16:
1920 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
1923 case Instruction::MOVE_OBJECT:
1924 case Instruction::MOVE_OBJECT_FROM16:
1925 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001927 break;
1928
1929 /*
1930 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001932 * might want to hold the result in an actual CPU register, so the
1933 * Dalvik spec requires that these only appear immediately after an
1934 * invoke or filled-new-array.
1935 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001936 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001937 * redundant with the reset done below, but it can make the debug info
1938 * easier to read in some cases.)
1939 */
1940 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001942 break;
1943 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
1946 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001951 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 * This statement can only appear as the first instruction in an exception handler (though not
1953 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001954 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001955 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001956 const RegType& res_type = GetCaughtExceptionType();
1957 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001958 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 }
jeffhaobdb76512011-09-07 11:43:16 -07001960 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1962 if (!GetMethodReturnType().IsUnknown()) {
1963 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1964 }
jeffhaobdb76512011-09-07 11:43:16 -07001965 }
1966 break;
1967 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001969 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 const RegType& return_type = GetMethodReturnType();
1971 if (!return_type.IsCategory1Types()) {
1972 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1973 } else {
1974 // Compilers may generate synthetic functions that write byte values into boolean fields.
1975 // Also, it may use integer values for boolean, byte, short, and character return types.
1976 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1977 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1978 ((return_type.IsBoolean() || return_type.IsByte() ||
1979 return_type.IsShort() || return_type.IsChar()) &&
1980 src_type.IsInteger()));
1981 /* check the register contents */
1982 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1983 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001984 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 }
1987 }
1988 break;
1989 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001991 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 const RegType& return_type = GetMethodReturnType();
1993 if (!return_type.IsCategory2Types()) {
1994 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1995 } else {
1996 /* check the register contents */
1997 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
1998 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001999 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 }
2002 }
2003 break;
2004 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2006 const RegType& return_type = GetMethodReturnType();
2007 if (!return_type.IsReferenceTypes()) {
2008 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2009 } else {
2010 /* return_type is the *expected* return type, not register value */
2011 DCHECK(!return_type.IsZero());
2012 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002013 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2014 // Disallow returning uninitialized values and verify that the reference in vAA is an
2015 // instance of the "return_type"
2016 if (reg_type.IsUninitializedTypes()) {
2017 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2018 } else if (!return_type.IsAssignableFrom(reg_type)) {
2019 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2020 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002021 }
2022 }
2023 }
2024 break;
2025
2026 case Instruction::CONST_4:
2027 case Instruction::CONST_16:
2028 case Instruction::CONST:
2029 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
2032 case Instruction::CONST_HIGH16:
2033 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 work_line_->SetRegisterType(dec_insn.vA_,
2035 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::CONST_WIDE_16:
2038 case Instruction::CONST_WIDE_32:
2039 case Instruction::CONST_WIDE:
2040 case Instruction::CONST_WIDE_HIGH16:
2041 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044 case Instruction::CONST_STRING:
2045 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002049 // Get type from instruction if unresolved then we need an access check
2050 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2051 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2052 // Register holds class, ie its type is class, but on error we keep it Unknown
2053 work_line_->SetRegisterType(dec_insn.vA_,
2054 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 }
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::MONITOR_EXIT:
2061 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002062 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002063 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002064 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002065 * to the need to handle asynchronous exceptions, a now-deprecated
2066 * feature that Dalvik doesn't support.)
2067 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002068 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002069 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002070 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002071 * structured locking checks are working, the former would have
2072 * failed on the -enter instruction, and the latter is impossible.
2073 *
2074 * This is fortunate, because issue 3221411 prevents us from
2075 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002076 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002077 * some catch blocks (which will show up as "dead" code when
2078 * we skip them here); if we can't, then the code path could be
2079 * "live" so we still need to check it.
2080 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 opcode_flag &= ~Instruction::kThrow;
2082 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084
Ian Rogers28ad40d2011-10-27 15:19:26 -07002085 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002087 /*
2088 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2089 * could be a "upcast" -- not expected, so we don't try to address it.)
2090 *
2091 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2092 * dec_insn.vA_ when branching to a handler.
2093 */
2094 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2095 const RegType& res_type =
2096 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002097 if (res_type.IsUnknown()) {
2098 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2099 break; // couldn't resolve class
2100 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002101 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2102 const RegType& orig_type =
2103 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2104 if (!res_type.IsNonZeroReferenceTypes()) {
2105 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2106 } else if (!orig_type.IsReferenceTypes()) {
2107 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002108 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002109 if (is_checkcast) {
2110 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002113 }
jeffhaobdb76512011-09-07 11:43:16 -07002114 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002115 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 }
2117 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002118 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2119 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002120 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002121 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 } else {
2123 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2124 }
2125 }
2126 break;
2127 }
2128 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002129 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2130 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2131 // can't create an instance of an interface or abstract class */
2132 if (!res_type.IsInstantiableTypes()) {
2133 Fail(VERIFY_ERROR_INSTANTIATION)
2134 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002136 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2137 // Any registers holding previous allocations from this address that have not yet been
2138 // initialized must be marked invalid.
2139 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2140 // add the new uninitialized reference to the register state
2141 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 }
2143 break;
2144 }
2145 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002146 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2147 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2148 if (!res_type.IsArrayClass()) {
2149 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002150 } else {
2151 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002153 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002154 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002155 }
2156 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 }
jeffhaobdb76512011-09-07 11:43:16 -07002158 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002160 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2161 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2162 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002164 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002165 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002166 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002167 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002168 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002169 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002170 just_set_result = true;
2171 }
2172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::CMPL_FLOAT:
2175 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2177 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2178 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
2180 case Instruction::CMPL_DOUBLE:
2181 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2183 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2184 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
2186 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2188 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2189 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002192 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2193 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2194 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002195 }
2196 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 }
jeffhaobdb76512011-09-07 11:43:16 -07002198 case Instruction::GOTO:
2199 case Instruction::GOTO_16:
2200 case Instruction::GOTO_32:
2201 /* no effect on or use of registers */
2202 break;
2203
2204 case Instruction::PACKED_SWITCH:
2205 case Instruction::SPARSE_SWITCH:
2206 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
2209
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 case Instruction::FILL_ARRAY_DATA: {
2211 /* Similar to the verification done for APUT */
2212 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2213 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002214 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 if (res_class != NULL) {
2216 Class* component_type = res_class->GetComponentType();
2217 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2218 component_type->IsPrimitiveVoid()) {
2219 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002220 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 } else {
2222 const RegType& value_type = reg_types_.FromClass(component_type);
2223 DCHECK(!value_type.IsUnknown());
2224 // Now verify if the element width in the table matches the element width declared in
2225 // the array
2226 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2227 if (array_data[0] != Instruction::kArrayDataSignature) {
2228 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2229 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002230 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 // Since we don't compress the data in Dex, expect to see equal width of data stored
2232 // in the table and expected from the array class.
2233 if (array_data[1] != elem_width) {
2234 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2235 << " vs " << elem_width << ")";
2236 }
2237 }
2238 }
jeffhaobdb76512011-09-07 11:43:16 -07002239 }
2240 }
2241 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 }
jeffhaobdb76512011-09-07 11:43:16 -07002243 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 case Instruction::IF_NE: {
2245 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2246 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2247 bool mismatch = false;
2248 if (reg_type1.IsZero()) { // zero then integral or reference expected
2249 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2250 } else if (reg_type1.IsReferenceTypes()) { // both references?
2251 mismatch = !reg_type2.IsReferenceTypes();
2252 } else { // both integral?
2253 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2254 }
2255 if (mismatch) {
2256 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2257 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002258 }
2259 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 }
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::IF_LT:
2262 case Instruction::IF_GE:
2263 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 case Instruction::IF_LE: {
2265 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2266 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2267 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2268 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2269 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002270 }
2271 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 }
jeffhaobdb76512011-09-07 11:43:16 -07002273 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 case Instruction::IF_NEZ: {
2275 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2276 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2277 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2278 }
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 }
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::IF_LTZ:
2282 case Instruction::IF_GEZ:
2283 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 case Instruction::IF_LEZ: {
2285 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2286 if (!reg_type.IsIntegralTypes()) {
2287 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2288 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2289 }
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 }
jeffhaobdb76512011-09-07 11:43:16 -07002292 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2294 break;
jeffhaobdb76512011-09-07 11:43:16 -07002295 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2297 break;
jeffhaobdb76512011-09-07 11:43:16 -07002298 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 VerifyAGet(dec_insn, reg_types_.Char(), true);
2300 break;
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 case Instruction::AGET:
2305 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2306 break;
jeffhaobdb76512011-09-07 11:43:16 -07002307 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 VerifyAGet(dec_insn, reg_types_.Long(), true);
2309 break;
2310 case Instruction::AGET_OBJECT:
2311 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 case Instruction::APUT_BOOLEAN:
2315 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2316 break;
2317 case Instruction::APUT_BYTE:
2318 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2319 break;
2320 case Instruction::APUT_CHAR:
2321 VerifyAPut(dec_insn, reg_types_.Char(), true);
2322 break;
2323 case Instruction::APUT_SHORT:
2324 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335
jeffhaobdb76512011-09-07 11:43:16 -07002336 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002337 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 break;
jeffhaobdb76512011-09-07 11:43:16 -07002339 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002340 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 break;
jeffhaobdb76512011-09-07 11:43:16 -07002342 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002343 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 break;
jeffhaobdb76512011-09-07 11:43:16 -07002345 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002346 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 break;
2348 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002349 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002352 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002355 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 break;
jeffhaobdb76512011-09-07 11:43:16 -07002357
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002359 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 break;
2361 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002362 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 break;
2364 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002365 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 break;
2367 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002368 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002371 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002374 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 break;
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002377 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002381 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 break;
jeffhaobdb76512011-09-07 11:43:16 -07002383 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002384 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 break;
jeffhaobdb76512011-09-07 11:43:16 -07002386 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002387 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002390 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
2392 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002393 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002396 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002399 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
2401
2402 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002403 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 break;
2405 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002406 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 break;
2408 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002409 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 break;
2411 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002412 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002415 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002418 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002421 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
2423
2424 case Instruction::INVOKE_VIRTUAL:
2425 case Instruction::INVOKE_VIRTUAL_RANGE:
2426 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 case Instruction::INVOKE_SUPER_RANGE: {
2428 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2429 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2430 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2431 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2432 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2433 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002434 const char* descriptor;
2435 if (called_method == NULL) {
2436 uint32_t method_idx = dec_insn.vB_;
2437 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2438 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002439 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002440 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002441 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002442 }
Ian Rogers9074b992011-10-26 17:41:55 -07002443 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002444 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002446 just_set_result = true;
2447 }
2448 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 }
jeffhaobdb76512011-09-07 11:43:16 -07002450 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 case Instruction::INVOKE_DIRECT_RANGE: {
2452 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2453 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2454 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002455 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 * Some additional checks when calling a constructor. We know from the invocation arg check
2457 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2458 * that to require that called_method->klass is the same as this->klass or this->super,
2459 * allowing the latter only if the "this" argument is the same as the "this" argument to
2460 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002461 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 bool is_constructor;
2463 if (called_method != NULL) {
2464 is_constructor = called_method->IsConstructor();
2465 } else {
2466 uint32_t method_idx = dec_insn.vB_;
2467 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2468 const char* name = dex_file_->GetMethodName(method_id);
2469 is_constructor = strcmp(name, "<init>") == 0;
2470 }
2471 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2473 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475
2476 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 if (this_type.IsZero()) {
2478 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 if (called_method != NULL) {
2482 Class* this_class = this_type.GetClass();
2483 DCHECK(this_class != NULL);
2484 /* must be in same class or in superclass */
2485 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2486 if (this_class != method_->GetDeclaringClass()) {
2487 Fail(VERIFY_ERROR_GENERIC)
2488 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2489 break;
2490 }
2491 } else if (called_method->GetDeclaringClass() != this_class) {
2492 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 }
jeffhaobdb76512011-09-07 11:43:16 -07002495 }
2496
2497 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002498 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2500 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 }
2503
2504 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002505 * Replace the uninitialized reference with an initialized one. We need to do this for all
2506 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002507 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 work_line_->MarkRefsAsInitialized(this_type);
2509 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002511 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002512 const char* descriptor;
2513 if (called_method == NULL) {
2514 uint32_t method_idx = dec_insn.vB_;
2515 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2516 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002517 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002518 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002519 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002520 }
Ian Rogers9074b992011-10-26 17:41:55 -07002521 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002522 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002524 just_set_result = true;
2525 }
2526 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 }
jeffhaobdb76512011-09-07 11:43:16 -07002528 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 case Instruction::INVOKE_STATIC_RANGE: {
2530 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2531 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2532 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002533 const char* descriptor;
2534 if (called_method == NULL) {
2535 uint32_t method_idx = dec_insn.vB_;
2536 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2537 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002538 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002539 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002540 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002541 }
Ian Rogers9074b992011-10-26 17:41:55 -07002542 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002543 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 work_line_->SetResultRegisterType(return_type);
2545 just_set_result = true;
2546 }
jeffhaobdb76512011-09-07 11:43:16 -07002547 }
2548 break;
2549 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 case Instruction::INVOKE_INTERFACE_RANGE: {
2551 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2552 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2553 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002554 if (abs_method != NULL) {
2555 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002556 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002557 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2558 << PrettyMethod(abs_method) << "'";
2559 break;
2560 }
2561 }
2562 /* Get the type of the "this" arg, which should either be a sub-interface of called
2563 * interface or Object (see comments in RegType::JoinClass).
2564 */
2565 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2566 if (failure_ == VERIFY_ERROR_NONE) {
2567 if (this_type.IsZero()) {
2568 /* null pointer always passes (and always fails at runtime) */
2569 } else {
2570 if (this_type.IsUninitializedTypes()) {
2571 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2572 << this_type;
2573 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002575 // In the past we have tried to assert that "called_interface" is assignable
2576 // from "this_type.GetClass()", however, as we do an imprecise Join
2577 // (RegType::JoinClass) we don't have full information on what interfaces are
2578 // implemented by "this_type". For example, two classes may implement the same
2579 // interfaces and have a common parent that doesn't implement the interface. The
2580 // join will set "this_type" to the parent class and a test that this implements
2581 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002582 }
2583 }
jeffhaobdb76512011-09-07 11:43:16 -07002584 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 * We don't have an object instance, so we can't find the concrete method. However, all of
2586 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002587 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002588 const char* descriptor;
2589 if (abs_method == NULL) {
2590 uint32_t method_idx = dec_insn.vB_;
2591 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2592 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002593 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002594 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002595 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002596 }
Ian Rogers9074b992011-10-26 17:41:55 -07002597 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002598 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2599 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002601 just_set_result = true;
2602 }
2603 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 }
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::NEG_INT:
2606 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609 case Instruction::NEG_LONG:
2610 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002618 break;
2619 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664
2665 case Instruction::ADD_INT:
2666 case Instruction::SUB_INT:
2667 case Instruction::MUL_INT:
2668 case Instruction::REM_INT:
2669 case Instruction::DIV_INT:
2670 case Instruction::SHL_INT:
2671 case Instruction::SHR_INT:
2672 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::AND_INT:
2676 case Instruction::OR_INT:
2677 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::ADD_LONG:
2681 case Instruction::SUB_LONG:
2682 case Instruction::MUL_LONG:
2683 case Instruction::DIV_LONG:
2684 case Instruction::REM_LONG:
2685 case Instruction::AND_LONG:
2686 case Instruction::OR_LONG:
2687 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::SHL_LONG:
2691 case Instruction::SHR_LONG:
2692 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 /* shift distance is Int, making these different from other binary operations */
2694 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
2696 case Instruction::ADD_FLOAT:
2697 case Instruction::SUB_FLOAT:
2698 case Instruction::MUL_FLOAT:
2699 case Instruction::DIV_FLOAT:
2700 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::ADD_DOUBLE:
2704 case Instruction::SUB_DOUBLE:
2705 case Instruction::MUL_DOUBLE:
2706 case Instruction::DIV_DOUBLE:
2707 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::ADD_INT_2ADDR:
2711 case Instruction::SUB_INT_2ADDR:
2712 case Instruction::MUL_INT_2ADDR:
2713 case Instruction::REM_INT_2ADDR:
2714 case Instruction::SHL_INT_2ADDR:
2715 case Instruction::SHR_INT_2ADDR:
2716 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719 case Instruction::AND_INT_2ADDR:
2720 case Instruction::OR_INT_2ADDR:
2721 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
2727 case Instruction::ADD_LONG_2ADDR:
2728 case Instruction::SUB_LONG_2ADDR:
2729 case Instruction::MUL_LONG_2ADDR:
2730 case Instruction::DIV_LONG_2ADDR:
2731 case Instruction::REM_LONG_2ADDR:
2732 case Instruction::AND_LONG_2ADDR:
2733 case Instruction::OR_LONG_2ADDR:
2734 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737 case Instruction::SHL_LONG_2ADDR:
2738 case Instruction::SHR_LONG_2ADDR:
2739 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::ADD_FLOAT_2ADDR:
2743 case Instruction::SUB_FLOAT_2ADDR:
2744 case Instruction::MUL_FLOAT_2ADDR:
2745 case Instruction::DIV_FLOAT_2ADDR:
2746 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::ADD_DOUBLE_2ADDR:
2750 case Instruction::SUB_DOUBLE_2ADDR:
2751 case Instruction::MUL_DOUBLE_2ADDR:
2752 case Instruction::DIV_DOUBLE_2ADDR:
2753 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756 case Instruction::ADD_INT_LIT16:
2757 case Instruction::RSUB_INT:
2758 case Instruction::MUL_INT_LIT16:
2759 case Instruction::DIV_INT_LIT16:
2760 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::AND_INT_LIT16:
2764 case Instruction::OR_INT_LIT16:
2765 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::ADD_INT_LIT8:
2769 case Instruction::RSUB_INT_LIT8:
2770 case Instruction::MUL_INT_LIT8:
2771 case Instruction::DIV_INT_LIT8:
2772 case Instruction::REM_INT_LIT8:
2773 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002774 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002775 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::AND_INT_LIT8:
2779 case Instruction::OR_INT_LIT8:
2780 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783
2784 /*
2785 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002786 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002787 * inserted in the course of verification, we can expect to see it here.
2788 */
jeffhaob4df5142011-09-19 20:25:32 -07002789 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002793 case Instruction::UNUSED_EE:
2794 case Instruction::UNUSED_EF:
2795 case Instruction::UNUSED_F2:
2796 case Instruction::UNUSED_F3:
2797 case Instruction::UNUSED_F4:
2798 case Instruction::UNUSED_F5:
2799 case Instruction::UNUSED_F6:
2800 case Instruction::UNUSED_F7:
2801 case Instruction::UNUSED_F8:
2802 case Instruction::UNUSED_F9:
2803 case Instruction::UNUSED_FA:
2804 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002805 case Instruction::UNUSED_F0:
2806 case Instruction::UNUSED_F1:
2807 case Instruction::UNUSED_E3:
2808 case Instruction::UNUSED_E8:
2809 case Instruction::UNUSED_E7:
2810 case Instruction::UNUSED_E4:
2811 case Instruction::UNUSED_E9:
2812 case Instruction::UNUSED_FC:
2813 case Instruction::UNUSED_E5:
2814 case Instruction::UNUSED_EA:
2815 case Instruction::UNUSED_FD:
2816 case Instruction::UNUSED_E6:
2817 case Instruction::UNUSED_EB:
2818 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002819 case Instruction::UNUSED_3E:
2820 case Instruction::UNUSED_3F:
2821 case Instruction::UNUSED_40:
2822 case Instruction::UNUSED_41:
2823 case Instruction::UNUSED_42:
2824 case Instruction::UNUSED_43:
2825 case Instruction::UNUSED_73:
2826 case Instruction::UNUSED_79:
2827 case Instruction::UNUSED_7A:
2828 case Instruction::UNUSED_EC:
2829 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002830 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832
2833 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002834 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002835 * complain if an instruction is missing (which is desirable).
2836 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002838
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 if (failure_ != VERIFY_ERROR_NONE) {
2840 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002841 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002842 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002843 return false;
2844 } else {
2845 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002846 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002848 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002850 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002852 opcode_flag = Instruction::kThrow;
2853 }
2854 }
jeffhaobdb76512011-09-07 11:43:16 -07002855 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 * If we didn't just set the result register, clear it out. This ensures that you can only use
2857 * "move-result" immediately after the result is set. (We could check this statically, but it's
2858 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002859 */
2860 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002862 }
2863
jeffhaoa0a764a2011-09-16 10:43:38 -07002864 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002865 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2867 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2868 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002869 return false;
2870 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2872 // next instruction isn't one.
2873 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002874 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 }
2876 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2877 if (next_line != NULL) {
2878 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2879 // needed.
2880 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002881 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 }
jeffhaobdb76512011-09-07 11:43:16 -07002883 } else {
2884 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 * We're not recording register data for the next instruction, so we don't know what the prior
2886 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002887 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002889 }
2890 }
2891
2892 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002893 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002894 *
2895 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002896 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002897 * somebody could get a reference field, check it for zero, and if the
2898 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002899 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002900 * that, and will reject the code.
2901 *
2902 * TODO: avoid re-fetching the branch target
2903 */
2904 if ((opcode_flag & Instruction::kBranch) != 0) {
2905 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002907 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002909 return false;
2910 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002911 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002913 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 }
jeffhaobdb76512011-09-07 11:43:16 -07002915 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 if (!UpdateRegisters(work_insn_idx_ + branch_target, 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 }
2920
2921 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002922 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002923 *
2924 * We've already verified that the table is structurally sound, so we
2925 * just need to walk through and tag the targets.
2926 */
2927 if ((opcode_flag & Instruction::kSwitch) != 0) {
2928 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2929 const uint16_t* switch_insns = insns + offset_to_switch;
2930 int switch_count = switch_insns[1];
2931 int offset_to_targets, targ;
2932
2933 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2934 /* 0 = sig, 1 = count, 2/3 = first key */
2935 offset_to_targets = 4;
2936 } else {
2937 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002938 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002939 offset_to_targets = 2 + 2 * switch_count;
2940 }
2941
2942 /* verify each switch target */
2943 for (targ = 0; targ < switch_count; targ++) {
2944 int offset;
2945 uint32_t abs_offset;
2946
2947 /* offsets are 32-bit, and only partly endian-swapped */
2948 offset = switch_insns[offset_to_targets + targ * 2] |
2949 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 abs_offset = work_insn_idx_ + offset;
2951 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2952 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
2955 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002956 return false;
2957 }
2958 }
2959
2960 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2962 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002963 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2965 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002966 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002967
Ian Rogers0571d352011-11-03 19:51:38 -07002968 for (; iterator.HasNext(); iterator.Next()) {
2969 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 within_catch_all = true;
2971 }
jeffhaobdb76512011-09-07 11:43:16 -07002972 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2974 * "work_regs", because at runtime the exception will be thrown before the instruction
2975 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002976 */
Ian Rogers0571d352011-11-03 19:51:38 -07002977 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002978 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 }
jeffhaobdb76512011-09-07 11:43:16 -07002980 }
2981
2982 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2984 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002985 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002987 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 * The state in work_line reflects the post-execution state. If the current instruction is a
2989 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002990 * it will do so before grabbing the lock).
2991 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2993 Fail(VERIFY_ERROR_GENERIC)
2994 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002995 return false;
2996 }
2997 }
2998 }
2999
jeffhaod1f0fde2011-09-08 17:25:33 -07003000 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 if ((opcode_flag & Instruction::kReturn) != 0) {
3002 if(!work_line_->VerifyMonitorStackEmpty()) {
3003 return false;
3004 }
jeffhaobdb76512011-09-07 11:43:16 -07003005 }
3006
3007 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003008 * Update start_guess. Advance to the next instruction of that's
3009 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003010 * neither of those exists we're in a return or throw; leave start_guess
3011 * alone and let the caller sort it out.
3012 */
3013 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003015 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3016 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003018 }
3019
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3021 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003022
3023 return true;
3024}
3025
Ian Rogers28ad40d2011-10-27 15:19:26 -07003026const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003027 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003028 Class* referrer = method_->GetDeclaringClass();
3029 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3030 const RegType& result =
3031 klass != NULL ? reg_types_.FromClass(klass)
3032 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3033 if (klass == NULL && !result.IsUnresolvedTypes()) {
3034 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003036 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3037 // check at runtime if access is allowed and so pass here.
3038 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3039 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003040 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003041 << result << "'";
3042 return reg_types_.Unknown();
3043 } else {
3044 return result;
3045 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003046}
3047
Ian Rogers28ad40d2011-10-27 15:19:26 -07003048const RegType& DexVerifier::GetCaughtExceptionType() {
3049 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003051 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3053 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003054 CatchHandlerIterator iterator(handlers_ptr);
3055 for (; iterator.HasNext(); iterator.Next()) {
3056 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3057 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003058 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003060 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3062 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3063 * test, so is essentially harmless.
3064 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003065 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3066 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3067 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003069 common_super = &exception;
3070 } else if (common_super->Equals(exception)) {
3071 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003073 common_super = &common_super->Merge(exception, &reg_types_);
3074 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 }
3076 }
3077 }
3078 }
Ian Rogers0571d352011-11-03 19:51:38 -07003079 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 }
3081 }
3082 if (common_super == NULL) {
3083 /* no catch blocks, or no catches with classes we can find */
3084 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3085 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003086 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003087}
3088
3089Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003090 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3091 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3092 if (failure_ != VERIFY_ERROR_NONE) {
3093 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3094 return NULL;
3095 }
3096 if(klass_type.IsUnresolvedTypes()) {
3097 return NULL; // Can't resolve Class so no more to do here
3098 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 Class* referrer = method_->GetDeclaringClass();
3100 DexCache* dex_cache = referrer->GetDexCache();
3101 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3102 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003103 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003104 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003105 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 if (is_direct) {
3107 res_method = klass->FindDirectMethod(name, signature);
3108 } else if (klass->IsInterface()) {
3109 res_method = klass->FindInterfaceMethod(name, signature);
3110 } else {
3111 res_method = klass->FindVirtualMethod(name, signature);
3112 }
3113 if (res_method != NULL) {
3114 dex_cache->SetResolvedMethod(method_idx, res_method);
3115 } else {
3116 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003117 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 << " " << signature;
3119 return NULL;
3120 }
3121 }
3122 /* Check if access is allowed. */
3123 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3124 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003125 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 return NULL;
3127 }
3128 return res_method;
3129}
3130
3131Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3132 MethodType method_type, bool is_range, bool is_super) {
3133 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3134 // we're making.
3135 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3136 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003137 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 return NULL;
3139 }
3140 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3141 // enforce them here.
3142 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3143 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3144 << PrettyMethod(res_method);
3145 return NULL;
3146 }
3147 // See if the method type implied by the invoke instruction matches the access flags for the
3148 // target method.
3149 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3150 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3151 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3152 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003153 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3154 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 return NULL;
3156 }
3157 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3158 // has a vtable entry for the target method.
3159 if (is_super) {
3160 DCHECK(method_type == METHOD_VIRTUAL);
3161 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3162 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3163 if (super == NULL) { // Only Object has no super class
3164 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3165 << " to super " << PrettyMethod(res_method);
3166 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003167 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003169 << " to super " << PrettyDescriptor(super)
3170 << "." << mh.GetName()
3171 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 }
3173 return NULL;
3174 }
3175 }
3176 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3177 // match the call to the signature. Also, we might might be calling through an abstract method
3178 // definition (which doesn't have register count values).
3179 int expected_args = dec_insn.vA_;
3180 /* caught by static verifier */
3181 DCHECK(is_range || expected_args <= 5);
3182 if (expected_args > code_item_->outs_size_) {
3183 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3184 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3185 return NULL;
3186 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003187 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 if (sig[0] != '(') {
3189 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3190 << " as descriptor doesn't start with '(': " << sig;
3191 return NULL;
3192 }
jeffhaobdb76512011-09-07 11:43:16 -07003193 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 * Check the "this" argument, which must be an instance of the class
3195 * that declared the method. For an interface class, we don't do the
3196 * full interface merge, so we can't do a rigorous check here (which
3197 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003198 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 int actual_args = 0;
3200 if (!res_method->IsStatic()) {
3201 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3202 if (failure_ != VERIFY_ERROR_NONE) {
3203 return NULL;
3204 }
3205 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3206 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3207 return NULL;
3208 }
3209 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003210 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3211 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3212 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3213 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 return NULL;
3215 }
3216 }
3217 actual_args++;
3218 }
3219 /*
3220 * Process the target method's signature. This signature may or may not
3221 * have been verified, so we can't assume it's properly formed.
3222 */
3223 size_t sig_offset = 0;
3224 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3225 if (actual_args >= expected_args) {
3226 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3227 << "'. Expected " << expected_args << " args, found more ("
3228 << sig.substr(sig_offset) << ")";
3229 return NULL;
3230 }
3231 std::string descriptor;
3232 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3233 size_t end;
3234 if (sig[sig_offset] == 'L') {
3235 end = sig.find(';', sig_offset);
3236 } else {
3237 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3238 if (sig[end] == 'L') {
3239 end = sig.find(';', end);
3240 }
3241 }
3242 if (end == std::string::npos) {
3243 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3244 << "bad signature component '" << sig << "' (missing ';')";
3245 return NULL;
3246 }
3247 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3248 sig_offset = end;
3249 } else {
3250 descriptor = sig[sig_offset];
3251 }
3252 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003253 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3254 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003255 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3256 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3257 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 }
3259 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3260 }
3261 if (sig[sig_offset] != ')') {
3262 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3263 return NULL;
3264 }
3265 if (actual_args != expected_args) {
3266 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3267 << " expected " << expected_args << " args, found " << actual_args;
3268 return NULL;
3269 } else {
3270 return res_method;
3271 }
3272}
3273
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003274const RegType& DexVerifier::GetMethodReturnType() {
3275 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3276 MethodHelper(method_).GetReturnTypeDescriptor());
3277}
3278
Ian Rogersd81871c2011-10-03 13:57:23 -07003279void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3280 const RegType& insn_type, bool is_primitive) {
3281 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3282 if (!index_type.IsArrayIndexTypes()) {
3283 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3284 } else {
3285 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3286 if (failure_ == VERIFY_ERROR_NONE) {
3287 if (array_class == NULL) {
3288 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3289 // instruction type. TODO: have a proper notion of bottom here.
3290 if (!is_primitive || insn_type.IsCategory1Types()) {
3291 // Reference or category 1
3292 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3293 } else {
3294 // Category 2
3295 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3296 }
3297 } else {
3298 /* verify the class */
3299 Class* component_class = array_class->GetComponentType();
3300 const RegType& component_type = reg_types_.FromClass(component_class);
3301 if (!array_class->IsArrayClass()) {
3302 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003303 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 } else if (component_class->IsPrimitive() && !is_primitive) {
3305 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003306 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 << " source for aget-object";
3308 } else if (!component_class->IsPrimitive() && is_primitive) {
3309 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003310 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 << " source for category 1 aget";
3312 } else if (is_primitive && !insn_type.Equals(component_type) &&
3313 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3314 (insn_type.IsLong() && component_type.IsDouble()))) {
3315 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003316 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 << " incompatible with aget of type " << insn_type;
3318 } else {
3319 // Use knowledge of the field type which is stronger than the type inferred from the
3320 // instruction, which can't differentiate object types and ints from floats, longs from
3321 // doubles.
3322 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3323 }
3324 }
3325 }
3326 }
3327}
3328
3329void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3330 const RegType& insn_type, bool is_primitive) {
3331 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3332 if (!index_type.IsArrayIndexTypes()) {
3333 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3334 } else {
3335 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3336 if (failure_ == VERIFY_ERROR_NONE) {
3337 if (array_class == NULL) {
3338 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3339 // instruction type.
3340 } else {
3341 /* verify the class */
3342 Class* component_class = array_class->GetComponentType();
3343 const RegType& component_type = reg_types_.FromClass(component_class);
3344 if (!array_class->IsArrayClass()) {
3345 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003346 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003347 } else if (component_class->IsPrimitive() && !is_primitive) {
3348 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003349 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 << " source for aput-object";
3351 } else if (!component_class->IsPrimitive() && is_primitive) {
3352 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003353 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 << " source for category 1 aput";
3355 } else if (is_primitive && !insn_type.Equals(component_type) &&
3356 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3357 (insn_type.IsLong() && component_type.IsDouble()))) {
3358 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003359 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 << " incompatible with aput of type " << insn_type;
3361 } else {
3362 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003363 // Note: we use the instruction type (rather than the component type) for aput-object as
3364 // incompatible classes will be caught at runtime as an array store exception
3365 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 }
3367 }
3368 }
3369 }
3370}
3371
3372Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003373 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3374 // Check access to class
3375 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3376 if (failure_ != VERIFY_ERROR_NONE) {
3377 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3378 << dex_file_->GetFieldName(field_id) << ") in "
3379 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3380 return NULL;
3381 }
3382 if(klass_type.IsUnresolvedTypes()) {
3383 return NULL; // Can't resolve Class so no more to do here
3384 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003385 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003387 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3388 << dex_file_->GetFieldName(field_id) << ") in "
3389 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 DCHECK(Thread::Current()->IsExceptionPending());
3391 Thread::Current()->ClearException();
3392 return NULL;
3393 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3394 field->GetAccessFlags())) {
3395 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3396 << " from " << PrettyClass(method_->GetDeclaringClass());
3397 return NULL;
3398 } else if (!field->IsStatic()) {
3399 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3400 return NULL;
3401 } else {
3402 return field;
3403 }
3404}
3405
Ian Rogersd81871c2011-10-03 13:57:23 -07003406Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003407 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3408 // Check access to class
3409 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3410 if (failure_ != VERIFY_ERROR_NONE) {
3411 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3412 << dex_file_->GetFieldName(field_id) << ") in "
3413 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3414 return NULL;
3415 }
3416 if(klass_type.IsUnresolvedTypes()) {
3417 return NULL; // Can't resolve Class so no more to do here
3418 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003419 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003421 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3422 << dex_file_->GetFieldName(field_id) << ") in "
3423 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003424 DCHECK(Thread::Current()->IsExceptionPending());
3425 Thread::Current()->ClearException();
3426 return NULL;
3427 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3428 field->GetAccessFlags())) {
3429 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3430 << " from " << PrettyClass(method_->GetDeclaringClass());
3431 return NULL;
3432 } else if (field->IsStatic()) {
3433 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3434 << " to not be static";
3435 return NULL;
3436 } else if (obj_type.IsZero()) {
3437 // Cannot infer and check type, however, access will cause null pointer exception
3438 return field;
3439 } else if(obj_type.IsUninitializedReference() &&
3440 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3441 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3442 // Field accesses through uninitialized references are only allowable for constructors where
3443 // the field is declared in this class
3444 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3445 << " of a not fully initialized object within the context of "
3446 << PrettyMethod(method_);
3447 return NULL;
3448 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3449 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3450 // of C1. For resolution to occur the declared class of the field must be compatible with
3451 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3452 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3453 << " from object of type " << PrettyClass(obj_type.GetClass());
3454 return NULL;
3455 } else {
3456 return field;
3457 }
3458}
3459
Ian Rogersb94a27b2011-10-26 00:33:41 -07003460void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3461 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003462 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003463 Field* field;
3464 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003465 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003466 } else {
3467 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003468 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003469 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003470 if (failure_ != VERIFY_ERROR_NONE) {
3471 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3472 } else {
3473 const char* descriptor;
3474 const ClassLoader* loader;
3475 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003476 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003477 loader = field->GetDeclaringClass()->GetClassLoader();
3478 } else {
3479 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3480 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3481 loader = method_->GetDeclaringClass()->GetClassLoader();
3482 }
3483 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003485 if (field_type.Equals(insn_type) ||
3486 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3487 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 // expected that read is of the correct primitive type or that int reads are reading
3489 // floats or long reads are reading doubles
3490 } else {
3491 // This is a global failure rather than a class change failure as the instructions and
3492 // the descriptors for the type should have been consistent within the same file at
3493 // compile time
3494 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003495 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003496 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 return;
3498 }
3499 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003500 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003502 << " to be compatible with type '" << insn_type
3503 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003504 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 return;
3506 }
3507 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003508 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 }
3510}
3511
Ian Rogersb94a27b2011-10-26 00:33:41 -07003512void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3513 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003514 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003515 Field* field;
3516 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003517 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003518 } else {
3519 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003520 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003521 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003522 if (failure_ != VERIFY_ERROR_NONE) {
3523 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3524 } else {
3525 const char* descriptor;
3526 const ClassLoader* loader;
3527 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003528 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003529 loader = field->GetDeclaringClass()->GetClassLoader();
3530 } else {
3531 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3532 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3533 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003534 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003535 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3536 if (field != NULL) {
3537 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3538 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3539 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3540 return;
3541 }
3542 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003543 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003544 // Primitive field assignability rules are weaker than regular assignability rules
3545 bool instruction_compatible;
3546 bool value_compatible;
3547 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3548 if (field_type.IsIntegralTypes()) {
3549 instruction_compatible = insn_type.IsIntegralTypes();
3550 value_compatible = value_type.IsIntegralTypes();
3551 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003552 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003553 value_compatible = value_type.IsFloatTypes();
3554 } else if (field_type.IsLong()) {
3555 instruction_compatible = insn_type.IsLong();
3556 value_compatible = value_type.IsLongTypes();
3557 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003558 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003559 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003560 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003561 instruction_compatible = false; // reference field with primitive store
3562 value_compatible = false; // unused
3563 }
3564 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 // This is a global failure rather than a class change failure as the instructions and
3566 // the descriptors for the type should have been consistent within the same file at
3567 // compile time
3568 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003569 << " to be of type '" << insn_type
3570 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003571 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 return;
3573 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003574 if (!value_compatible) {
3575 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3576 << " of type " << value_type
3577 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003578 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003579 return;
3580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003582 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003584 << " to be compatible with type '" << insn_type
3585 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003586 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 return;
3588 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003589 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003590 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 }
3592}
3593
3594bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3595 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3596 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3597 return false;
3598 }
3599 return true;
3600}
3601
3602void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003603 const RegType& res_type, bool is_range) {
3604 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003605 /*
3606 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3607 * list and fail. It's legal, if silly, for arg_count to be zero.
3608 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003609 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3610 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 uint32_t arg_count = dec_insn.vA_;
3612 for (size_t ui = 0; ui < arg_count; ui++) {
3613 uint32_t get_reg;
3614
3615 if (is_range)
3616 get_reg = dec_insn.vC_ + ui;
3617 else
3618 get_reg = dec_insn.arg_[ui];
3619
3620 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3621 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3622 << ") not valid";
3623 return;
3624 }
3625 }
3626}
3627
3628void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003629 if (Runtime::Current()->IsStarted()) {
3630 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3631 << " " << fail_messages_.str();
3632 return;
3633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3635 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3636 VerifyErrorRefType ref_type;
3637 switch (inst->Opcode()) {
3638 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003639 case Instruction::CHECK_CAST:
3640 case Instruction::INSTANCE_OF:
3641 case Instruction::NEW_INSTANCE:
3642 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003644 case Instruction::FILLED_NEW_ARRAY_RANGE:
3645 ref_type = VERIFY_ERROR_REF_CLASS;
3646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003648 case Instruction::IGET_BOOLEAN:
3649 case Instruction::IGET_BYTE:
3650 case Instruction::IGET_CHAR:
3651 case Instruction::IGET_SHORT:
3652 case Instruction::IGET_WIDE:
3653 case Instruction::IGET_OBJECT:
3654 case Instruction::IPUT:
3655 case Instruction::IPUT_BOOLEAN:
3656 case Instruction::IPUT_BYTE:
3657 case Instruction::IPUT_CHAR:
3658 case Instruction::IPUT_SHORT:
3659 case Instruction::IPUT_WIDE:
3660 case Instruction::IPUT_OBJECT:
3661 case Instruction::SGET:
3662 case Instruction::SGET_BOOLEAN:
3663 case Instruction::SGET_BYTE:
3664 case Instruction::SGET_CHAR:
3665 case Instruction::SGET_SHORT:
3666 case Instruction::SGET_WIDE:
3667 case Instruction::SGET_OBJECT:
3668 case Instruction::SPUT:
3669 case Instruction::SPUT_BOOLEAN:
3670 case Instruction::SPUT_BYTE:
3671 case Instruction::SPUT_CHAR:
3672 case Instruction::SPUT_SHORT:
3673 case Instruction::SPUT_WIDE:
3674 case Instruction::SPUT_OBJECT:
3675 ref_type = VERIFY_ERROR_REF_FIELD;
3676 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003678 case Instruction::INVOKE_VIRTUAL_RANGE:
3679 case Instruction::INVOKE_SUPER:
3680 case Instruction::INVOKE_SUPER_RANGE:
3681 case Instruction::INVOKE_DIRECT:
3682 case Instruction::INVOKE_DIRECT_RANGE:
3683 case Instruction::INVOKE_STATIC:
3684 case Instruction::INVOKE_STATIC_RANGE:
3685 case Instruction::INVOKE_INTERFACE:
3686 case Instruction::INVOKE_INTERFACE_RANGE:
3687 ref_type = VERIFY_ERROR_REF_METHOD;
3688 break;
jeffhaobdb76512011-09-07 11:43:16 -07003689 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003690 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003691 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3694 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3695 // instruction, so assert it.
3696 size_t width = inst->SizeInCodeUnits();
3697 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003698 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 // NOPs
3700 for (size_t i = 2; i < width; i++) {
3701 insns[work_insn_idx_ + i] = Instruction::NOP;
3702 }
3703 // Encode the opcode, with the failure code in the high byte
3704 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3705 (failure_ << 8) | // AA - component
3706 (ref_type << (8 + kVerifyErrorRefTypeShift));
3707 insns[work_insn_idx_] = new_instruction;
3708 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3709 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003710 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3711 << fail_messages_.str();
3712 if (gDebugVerify) {
3713 std::cout << std::endl << info_messages_.str();
3714 Dump(std::cout);
3715 }
jeffhaobdb76512011-09-07 11:43:16 -07003716}
jeffhaoba5ebb92011-08-25 17:24:37 -07003717
Ian Rogersd81871c2011-10-03 13:57:23 -07003718bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3719 const bool merge_debug = true;
3720 bool changed = true;
3721 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3722 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003723 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 * We haven't processed this instruction before, and we haven't touched the registers here, so
3725 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3726 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003727 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003729 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003730 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3731 copy->CopyFromLine(target_line);
3732 changed = target_line->MergeRegisters(merge_line);
3733 if (failure_ != VERIFY_ERROR_NONE) {
3734 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003735 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003736 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3738 << *copy.get() << " MERGE" << std::endl
3739 << *merge_line << " ==" << std::endl
3740 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003741 }
3742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 if (changed) {
3744 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003745 }
3746 return true;
3747}
3748
Ian Rogersd81871c2011-10-03 13:57:23 -07003749void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3750 size_t* log2_max_gc_pc) {
3751 size_t local_gc_points = 0;
3752 size_t max_insn = 0;
3753 size_t max_ref_reg = -1;
3754 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3755 if (insn_flags_[i].IsGcPoint()) {
3756 local_gc_points++;
3757 max_insn = i;
3758 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003759 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003760 }
3761 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 *gc_points = local_gc_points;
3763 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3764 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003765 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 i++;
3767 }
3768 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003769}
3770
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003771const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 size_t num_entries, ref_bitmap_bits, pc_bits;
3773 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3774 // There's a single byte to encode the size of each bitmap
3775 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3776 // TODO: either a better GC map format or per method failures
3777 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3778 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003779 return NULL;
3780 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3782 // There are 2 bytes to encode the number of entries
3783 if (num_entries >= 65536) {
3784 // TODO: either a better GC map format or per method failures
3785 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3786 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003787 return NULL;
3788 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003790 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003791 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003792 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003794 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003795 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003797 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 // TODO: either a better GC map format or per method failures
3799 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3800 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3801 return NULL;
3802 }
3803 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003804 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 if (table == NULL) {
3806 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3807 return NULL;
3808 }
3809 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003810 table->push_back(format);
3811 table->push_back(ref_bitmap_bytes);
3812 table->push_back(num_entries & 0xFF);
3813 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003815 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3816 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003817 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003818 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003819 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 }
3821 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003822 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 }
3824 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003825 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 return table;
3827}
jeffhaoa0a764a2011-09-16 10:43:38 -07003828
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003829void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3831 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003832 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 size_t map_index = 0;
3834 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3835 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3836 if (insn_flags_[i].IsGcPoint()) {
3837 CHECK_LT(map_index, map.NumEntries());
3838 CHECK_EQ(map.GetPC(map_index), i);
3839 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3840 map_index++;
3841 RegisterLine* line = reg_table_.GetLine(i);
3842 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003843 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 CHECK_LT(j / 8, map.RegWidth());
3845 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3846 } else if ((j / 8) < map.RegWidth()) {
3847 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3848 } else {
3849 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3850 }
3851 }
3852 } else {
3853 CHECK(reg_bitmap == NULL);
3854 }
3855 }
3856}
jeffhaoa0a764a2011-09-16 10:43:38 -07003857
Ian Rogersd81871c2011-10-03 13:57:23 -07003858const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3859 size_t num_entries = NumEntries();
3860 // Do linear or binary search?
3861 static const size_t kSearchThreshold = 8;
3862 if (num_entries < kSearchThreshold) {
3863 for (size_t i = 0; i < num_entries; i++) {
3864 if (GetPC(i) == dex_pc) {
3865 return GetBitMap(i);
3866 }
3867 }
3868 } else {
3869 int lo = 0;
3870 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003871 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 int mid = (hi + lo) / 2;
3873 int mid_pc = GetPC(mid);
3874 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003875 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003877 hi = mid - 1;
3878 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003880 }
3881 }
3882 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 if (error_if_not_present) {
3884 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3885 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003886 return NULL;
3887}
3888
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003889DexVerifier::GcMapTable DexVerifier::gc_maps_;
3890
3891void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003892 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3893 if (existing_gc_map != NULL) {
3894 CHECK(*existing_gc_map == gc_map);
3895 delete existing_gc_map;
3896 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003897 gc_maps_[ref] = &gc_map;
3898 CHECK(GetGcMap(ref) != NULL);
3899}
3900
3901const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3902 GcMapTable::const_iterator it = gc_maps_.find(ref);
3903 if (it == gc_maps_.end()) {
3904 return NULL;
3905 }
3906 CHECK(it->second != NULL);
3907 return it->second;
3908}
3909
3910void DexVerifier::DeleteGcMaps() {
3911 STLDeleteValues(&gc_maps_);
3912}
3913
Ian Rogersd81871c2011-10-03 13:57:23 -07003914} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003915} // namespace art