blob: 31c2697b5cafcc9c1652aa37a1ff76cbed89a99c [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
925bool DexVerifier::VerifyClass(const Class* klass) {
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 Rogersd81871c2011-10-03 13:57:23 -0700931 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " that has no super class";
932 return false;
933 }
934 if (super != NULL) {
Ian Rogers672f5202012-01-12 18:06:40 -0800935 // Acquire lock to prevent races on verifying the super class
936 ObjectLock lock(super);
937
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 if (!super->IsVerified() && !super->IsErroneous()) {
939 Runtime::Current()->GetClassLinker()->VerifyClass(super);
940 }
941 if (!super->IsVerified()) {
942 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
943 << " that attempts to sub-class corrupt class " << PrettyClass(super);
944 return false;
945 } else if (super->IsFinal()) {
946 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass)
947 << " that attempts to sub-class final class " << PrettyClass(super);
948 return false;
949 }
950 }
jeffhaobdb76512011-09-07 11:43:16 -0700951 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
952 Method* method = klass->GetDirectMethod(i);
953 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
955 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700956 return false;
957 }
958 }
959 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
960 Method* method = klass->GetVirtualMethod(i);
961 if (!VerifyMethod(method)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 LOG(ERROR) << "Verifier rejected class " << PrettyClass(klass) << " due to bad method "
963 << PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700964 return false;
965 }
966 }
967 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700968}
969
jeffhaobdb76512011-09-07 11:43:16 -0700970bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 DexVerifier verifier(method);
972 bool success = verifier.Verify();
Brian Carlstrom75412882012-01-18 01:26:54 -0800973 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
974
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 // We expect either success and no verification error, or failure and a generic failure to
976 // reject the class.
977 if (success) {
978 if (verifier.failure_ != VERIFY_ERROR_NONE) {
979 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
980 << verifier.fail_messages_;
981 }
982 } else {
983 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700984 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700985 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700986 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700987 verifier.Dump(std::cout);
988 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
990 }
991 return success;
992}
993
Shih-wei Liao371814f2011-10-27 16:52:10 -0700994void DexVerifier::VerifyMethodAndDump(Method* method) {
995 DexVerifier verifier(method);
996 verifier.Verify();
997
Elliott Hughese0918552011-10-28 17:18:29 -0700998 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
999 << verifier.fail_messages_.str() << std::endl
1000 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -07001001}
1002
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001003DexVerifier::DexVerifier(Method* method)
1004 : work_insn_idx_(-1),
1005 method_(method),
1006 failure_(VERIFY_ERROR_NONE),
1007 new_instance_count_(0),
1008 monitor_enter_count_(0) {
1009 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001010 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001011 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 dex_file_ = &class_linker->FindDexFile(dex_cache);
1013 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001014}
1015
Ian Rogersd81871c2011-10-03 13:57:23 -07001016bool DexVerifier::Verify() {
1017 // If there aren't any instructions, make sure that's expected, then exit successfully.
1018 if (code_item_ == NULL) {
1019 if (!method_->IsNative() && !method_->IsAbstract()) {
1020 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001021 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 } else {
1023 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001024 }
jeffhaobdb76512011-09-07 11:43:16 -07001025 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1027 if (code_item_->ins_size_ > code_item_->registers_size_) {
1028 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1029 << " regs=" << code_item_->registers_size_;
1030 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001031 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 // Allocate and initialize an array to hold instruction data.
1033 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1034 // Run through the instructions and see if the width checks out.
1035 bool result = ComputeWidthsAndCountOps();
1036 // Flag instructions guarded by a "try" block and check exception handlers.
1037 result = result && ScanTryCatchBlocks();
1038 // Perform static instruction verification.
1039 result = result && VerifyInstructions();
1040 // Perform code flow analysis.
1041 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001042 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001043}
1044
Ian Rogersd81871c2011-10-03 13:57:23 -07001045bool DexVerifier::ComputeWidthsAndCountOps() {
1046 const uint16_t* insns = code_item_->insns_;
1047 size_t insns_size = code_item_->insns_size_in_code_units_;
1048 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001049 size_t new_instance_count = 0;
1050 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001052
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001054 Instruction::Code opcode = inst->Opcode();
1055 if (opcode == Instruction::NEW_INSTANCE) {
1056 new_instance_count++;
1057 } else if (opcode == Instruction::MONITOR_ENTER) {
1058 monitor_enter_count++;
1059 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 size_t inst_size = inst->SizeInCodeUnits();
1061 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1062 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001063 inst = inst->Next();
1064 }
1065
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 if (dex_pc != insns_size) {
1067 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1068 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001069 return false;
1070 }
1071
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 new_instance_count_ = new_instance_count;
1073 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001074 return true;
1075}
1076
Ian Rogersd81871c2011-10-03 13:57:23 -07001077bool DexVerifier::ScanTryCatchBlocks() {
1078 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001079 if (tries_size == 0) {
1080 return true;
1081 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001083 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001084
1085 for (uint32_t idx = 0; idx < tries_size; idx++) {
1086 const DexFile::TryItem* try_item = &tries[idx];
1087 uint32_t start = try_item->start_addr_;
1088 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001089 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1091 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001092 return false;
1093 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 if (!insn_flags_[start].IsOpcode()) {
1095 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001096 return false;
1097 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 for (uint32_t dex_pc = start; dex_pc < end;
1099 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1100 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001101 }
1102 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001103 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001104 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001105 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001106 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001107 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001108 CatchHandlerIterator iterator(handlers_ptr);
1109 for (; iterator.HasNext(); iterator.Next()) {
1110 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 if (!insn_flags_[dex_pc].IsOpcode()) {
1112 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001113 return false;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001116 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1117 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001118 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1119 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001120 if (exception_type == NULL) {
1121 DCHECK(Thread::Current()->IsExceptionPending());
1122 Thread::Current()->ClearException();
1123 }
1124 }
jeffhaobdb76512011-09-07 11:43:16 -07001125 }
Ian Rogers0571d352011-11-03 19:51:38 -07001126 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001127 }
jeffhaobdb76512011-09-07 11:43:16 -07001128 return true;
1129}
1130
Ian Rogersd81871c2011-10-03 13:57:23 -07001131bool DexVerifier::VerifyInstructions() {
1132 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001133
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 /* Flag the start of the method as a branch target. */
1135 insn_flags_[0].SetBranchTarget();
1136
1137 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1138 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1139 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001140 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1141 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 return false;
1143 }
1144 /* Flag instructions that are garbage collection points */
1145 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1146 insn_flags_[dex_pc].SetGcPoint();
1147 }
1148 dex_pc += inst->SizeInCodeUnits();
1149 inst = inst->Next();
1150 }
1151 return true;
1152}
1153
1154bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1155 Instruction::DecodedInstruction dec_insn(inst);
1156 bool result = true;
1157 switch (inst->GetVerifyTypeArgumentA()) {
1158 case Instruction::kVerifyRegA:
1159 result = result && CheckRegisterIndex(dec_insn.vA_);
1160 break;
1161 case Instruction::kVerifyRegAWide:
1162 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1163 break;
1164 }
1165 switch (inst->GetVerifyTypeArgumentB()) {
1166 case Instruction::kVerifyRegB:
1167 result = result && CheckRegisterIndex(dec_insn.vB_);
1168 break;
1169 case Instruction::kVerifyRegBField:
1170 result = result && CheckFieldIndex(dec_insn.vB_);
1171 break;
1172 case Instruction::kVerifyRegBMethod:
1173 result = result && CheckMethodIndex(dec_insn.vB_);
1174 break;
1175 case Instruction::kVerifyRegBNewInstance:
1176 result = result && CheckNewInstance(dec_insn.vB_);
1177 break;
1178 case Instruction::kVerifyRegBString:
1179 result = result && CheckStringIndex(dec_insn.vB_);
1180 break;
1181 case Instruction::kVerifyRegBType:
1182 result = result && CheckTypeIndex(dec_insn.vB_);
1183 break;
1184 case Instruction::kVerifyRegBWide:
1185 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1186 break;
1187 }
1188 switch (inst->GetVerifyTypeArgumentC()) {
1189 case Instruction::kVerifyRegC:
1190 result = result && CheckRegisterIndex(dec_insn.vC_);
1191 break;
1192 case Instruction::kVerifyRegCField:
1193 result = result && CheckFieldIndex(dec_insn.vC_);
1194 break;
1195 case Instruction::kVerifyRegCNewArray:
1196 result = result && CheckNewArray(dec_insn.vC_);
1197 break;
1198 case Instruction::kVerifyRegCType:
1199 result = result && CheckTypeIndex(dec_insn.vC_);
1200 break;
1201 case Instruction::kVerifyRegCWide:
1202 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1203 break;
1204 }
1205 switch (inst->GetVerifyExtraFlags()) {
1206 case Instruction::kVerifyArrayData:
1207 result = result && CheckArrayData(code_offset);
1208 break;
1209 case Instruction::kVerifyBranchTarget:
1210 result = result && CheckBranchTarget(code_offset);
1211 break;
1212 case Instruction::kVerifySwitchTargets:
1213 result = result && CheckSwitchTargets(code_offset);
1214 break;
1215 case Instruction::kVerifyVarArg:
1216 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1217 break;
1218 case Instruction::kVerifyVarArgRange:
1219 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1220 break;
1221 case Instruction::kVerifyError:
1222 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1223 result = false;
1224 break;
1225 }
1226 return result;
1227}
1228
1229bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1230 if (idx >= code_item_->registers_size_) {
1231 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1232 << code_item_->registers_size_ << ")";
1233 return false;
1234 }
1235 return true;
1236}
1237
1238bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1239 if (idx + 1 >= code_item_->registers_size_) {
1240 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1241 << "+1 >= " << code_item_->registers_size_ << ")";
1242 return false;
1243 }
1244 return true;
1245}
1246
1247bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1248 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1249 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1250 << dex_file_->GetHeader().field_ids_size_ << ")";
1251 return false;
1252 }
1253 return true;
1254}
1255
1256bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1257 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1258 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1259 << dex_file_->GetHeader().method_ids_size_ << ")";
1260 return false;
1261 }
1262 return true;
1263}
1264
1265bool DexVerifier::CheckNewInstance(uint32_t idx) {
1266 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1267 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1268 << dex_file_->GetHeader().type_ids_size_ << ")";
1269 return false;
1270 }
1271 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001272 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 if (descriptor[0] != 'L') {
1274 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1275 return false;
1276 }
1277 return true;
1278}
1279
1280bool DexVerifier::CheckStringIndex(uint32_t idx) {
1281 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1282 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1283 << dex_file_->GetHeader().string_ids_size_ << ")";
1284 return false;
1285 }
1286 return true;
1287}
1288
1289bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1290 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1291 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1292 << dex_file_->GetHeader().type_ids_size_ << ")";
1293 return false;
1294 }
1295 return true;
1296}
1297
1298bool DexVerifier::CheckNewArray(uint32_t idx) {
1299 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1300 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1301 << dex_file_->GetHeader().type_ids_size_ << ")";
1302 return false;
1303 }
1304 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001305 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 const char* cp = descriptor;
1307 while (*cp++ == '[') {
1308 bracket_count++;
1309 }
1310 if (bracket_count == 0) {
1311 /* The given class must be an array type. */
1312 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1313 return false;
1314 } else if (bracket_count > 255) {
1315 /* It is illegal to create an array of more than 255 dimensions. */
1316 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1317 return false;
1318 }
1319 return true;
1320}
1321
1322bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1323 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1324 const uint16_t* insns = code_item_->insns_ + cur_offset;
1325 const uint16_t* array_data;
1326 int32_t array_data_offset;
1327
1328 DCHECK_LT(cur_offset, insn_count);
1329 /* make sure the start of the array data table is in range */
1330 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1331 if ((int32_t) cur_offset + array_data_offset < 0 ||
1332 cur_offset + array_data_offset + 2 >= insn_count) {
1333 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1334 << ", data offset " << array_data_offset << ", count " << insn_count;
1335 return false;
1336 }
1337 /* offset to array data table is a relative branch-style offset */
1338 array_data = insns + array_data_offset;
1339 /* make sure the table is 32-bit aligned */
1340 if ((((uint32_t) array_data) & 0x03) != 0) {
1341 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1342 << ", data offset " << array_data_offset;
1343 return false;
1344 }
1345 uint32_t value_width = array_data[1];
1346 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1347 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1348 /* make sure the end of the switch is in range */
1349 if (cur_offset + array_data_offset + table_size > insn_count) {
1350 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1351 << ", data offset " << array_data_offset << ", end "
1352 << cur_offset + array_data_offset + table_size
1353 << ", count " << insn_count;
1354 return false;
1355 }
1356 return true;
1357}
1358
1359bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1360 int32_t offset;
1361 bool isConditional, selfOkay;
1362 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1363 return false;
1364 }
1365 if (!selfOkay && offset == 0) {
1366 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1367 return false;
1368 }
1369 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1370 // identical "wrap-around" behavior, but it's unwise to depend on that.
1371 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1372 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1373 return false;
1374 }
1375 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1376 int32_t abs_offset = cur_offset + offset;
1377 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1378 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1379 << (void*) abs_offset << ") at " << (void*) cur_offset;
1380 return false;
1381 }
1382 insn_flags_[abs_offset].SetBranchTarget();
1383 return true;
1384}
1385
1386bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1387 bool* selfOkay) {
1388 const uint16_t* insns = code_item_->insns_ + cur_offset;
1389 *pConditional = false;
1390 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 switch (*insns & 0xff) {
1392 case Instruction::GOTO:
1393 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001394 break;
1395 case Instruction::GOTO_32:
1396 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 *selfOkay = true;
1398 break;
1399 case Instruction::GOTO_16:
1400 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001401 break;
1402 case Instruction::IF_EQ:
1403 case Instruction::IF_NE:
1404 case Instruction::IF_LT:
1405 case Instruction::IF_GE:
1406 case Instruction::IF_GT:
1407 case Instruction::IF_LE:
1408 case Instruction::IF_EQZ:
1409 case Instruction::IF_NEZ:
1410 case Instruction::IF_LTZ:
1411 case Instruction::IF_GEZ:
1412 case Instruction::IF_GTZ:
1413 case Instruction::IF_LEZ:
1414 *pOffset = (int16_t) insns[1];
1415 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 break;
1417 default:
1418 return false;
1419 break;
1420 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 return true;
1422}
1423
Ian Rogersd81871c2011-10-03 13:57:23 -07001424bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1425 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001426 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001428 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1430 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1431 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1432 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 return false;
1434 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001437 /* make sure the table is 32-bit aligned */
1438 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1440 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001441 return false;
1442 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 uint32_t switch_count = switch_insns[1];
1444 int32_t keys_offset, targets_offset;
1445 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001446 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1447 /* 0=sig, 1=count, 2/3=firstKey */
1448 targets_offset = 4;
1449 keys_offset = -1;
1450 expected_signature = Instruction::kPackedSwitchSignature;
1451 } else {
1452 /* 0=sig, 1=count, 2..count*2 = keys */
1453 keys_offset = 2;
1454 targets_offset = 2 + 2 * switch_count;
1455 expected_signature = Instruction::kSparseSwitchSignature;
1456 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001459 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1460 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 return false;
1462 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 /* make sure the end of the switch is in range */
1464 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1466 << switch_offset << ", end "
1467 << (cur_offset + switch_offset + table_size)
1468 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001469 return false;
1470 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 /* for a sparse switch, verify the keys are in ascending order */
1472 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1474 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1476 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1477 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1479 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 return false;
1481 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 last_key = key;
1483 }
1484 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 for (uint32_t targ = 0; targ < switch_count; targ++) {
1487 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1488 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1489 int32_t abs_offset = cur_offset + offset;
1490 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1491 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1492 << (void*) abs_offset << ") at "
1493 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001494 return false;
1495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 insn_flags_[abs_offset].SetBranchTarget();
1497 }
1498 return true;
1499}
1500
1501bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1502 if (vA > 5) {
1503 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1504 return false;
1505 }
1506 uint16_t registers_size = code_item_->registers_size_;
1507 for (uint32_t idx = 0; idx < vA; idx++) {
1508 if (arg[idx] > registers_size) {
1509 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1510 << ") in non-range invoke (> " << registers_size << ")";
1511 return false;
1512 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001513 }
1514
1515 return true;
1516}
1517
Ian Rogersd81871c2011-10-03 13:57:23 -07001518bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1519 uint16_t registers_size = code_item_->registers_size_;
1520 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1521 // integer overflow when adding them here.
1522 if (vA + vC > registers_size) {
1523 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1524 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001525 return false;
1526 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 return true;
1528}
1529
Brian Carlstrom75412882012-01-18 01:26:54 -08001530const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1531 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1532 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1533 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1534 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1535 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1536 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1537 gc_map.begin(),
1538 gc_map.end());
1539 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1540 DCHECK_EQ(gc_map.size(),
1541 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1542 (length_prefixed_gc_map->at(1) << 16) |
1543 (length_prefixed_gc_map->at(2) << 8) |
1544 (length_prefixed_gc_map->at(3) << 0)));
1545 return length_prefixed_gc_map;
1546}
1547
Ian Rogersd81871c2011-10-03 13:57:23 -07001548bool DexVerifier::VerifyCodeFlow() {
1549 uint16_t registers_size = code_item_->registers_size_;
1550 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001551
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 if (registers_size * insns_size > 4*1024*1024) {
1553 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1554 << " insns_size=" << insns_size << ")";
1555 }
1556 /* Create and initialize table holding register status */
1557 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1558 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001559
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 work_line_.reset(new RegisterLine(registers_size, this));
1561 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001562
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 /* Initialize register types of method arguments. */
1564 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001565 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1566 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 return false;
1568 }
1569 /* Perform code flow verification. */
1570 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001571 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
1574
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001576 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1577 if (map.get() == NULL) {
1578 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 return false; // Not a real failure, but a failure to encode
1580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001581#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001582 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001583#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001584 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1585 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1586 verifier::DexVerifier::SetGcMap(ref, *gc_map);
1587 method_->SetGcMap(&gc_map->at(0));
jeffhaobdb76512011-09-07 11:43:16 -07001588 return true;
1589}
1590
Ian Rogersd81871c2011-10-03 13:57:23 -07001591void DexVerifier::Dump(std::ostream& os) {
1592 if (method_->IsNative()) {
1593 os << "Native method" << std::endl;
1594 return;
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 DCHECK(code_item_ != NULL);
1597 const Instruction* inst = Instruction::At(code_item_->insns_);
1598 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1599 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001600 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001601 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1603 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001604 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001605 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001606 inst = inst->Next();
1607 }
jeffhaobdb76512011-09-07 11:43:16 -07001608}
1609
Ian Rogersd81871c2011-10-03 13:57:23 -07001610static bool IsPrimitiveDescriptor(char descriptor) {
1611 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001612 case 'I':
1613 case 'C':
1614 case 'S':
1615 case 'B':
1616 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001617 case 'F':
1618 case 'D':
1619 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001621 default:
1622 return false;
1623 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001624}
1625
Ian Rogersd81871c2011-10-03 13:57:23 -07001626bool DexVerifier::SetTypesFromSignature() {
1627 RegisterLine* reg_line = reg_table_.GetLine(0);
1628 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1629 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001630
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1632 //Include the "this" pointer.
1633 size_t cur_arg = 0;
1634 if (!method_->IsStatic()) {
1635 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1636 // argument as uninitialized. This restricts field access until the superclass constructor is
1637 // called.
1638 Class* declaring_class = method_->GetDeclaringClass();
1639 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1640 reg_line->SetRegisterType(arg_start + cur_arg,
1641 reg_types_.UninitializedThisArgument(declaring_class));
1642 } else {
1643 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001644 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001646 }
1647
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001648 const DexFile::ProtoId& proto_id =
1649 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001650 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001651
1652 for (; iterator.HasNext(); iterator.Next()) {
1653 const char* descriptor = iterator.GetDescriptor();
1654 if (descriptor == NULL) {
1655 LOG(FATAL) << "Null descriptor";
1656 }
1657 if (cur_arg >= expected_args) {
1658 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1659 << " args, found more (" << descriptor << ")";
1660 return false;
1661 }
1662 switch (descriptor[0]) {
1663 case 'L':
1664 case '[':
1665 // We assume that reference arguments are initialized. The only way it could be otherwise
1666 // (assuming the caller was verified) is if the current method is <init>, but in that case
1667 // it's effectively considered initialized the instant we reach here (in the sense that we
1668 // can return without doing anything or call virtual methods).
1669 {
1670 const RegType& reg_type =
1671 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001672 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 }
1674 break;
1675 case 'Z':
1676 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1677 break;
1678 case 'C':
1679 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1680 break;
1681 case 'B':
1682 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1683 break;
1684 case 'I':
1685 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1686 break;
1687 case 'S':
1688 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1689 break;
1690 case 'F':
1691 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1692 break;
1693 case 'J':
1694 case 'D': {
1695 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1696 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1697 cur_arg++;
1698 break;
1699 }
1700 default:
1701 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1702 return false;
1703 }
1704 cur_arg++;
1705 }
1706 if (cur_arg != expected_args) {
1707 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1708 return false;
1709 }
1710 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1711 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1712 // format. Only major difference from the method argument format is that 'V' is supported.
1713 bool result;
1714 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1715 result = descriptor[1] == '\0';
1716 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1717 size_t i = 0;
1718 do {
1719 i++;
1720 } while (descriptor[i] == '['); // process leading [
1721 if (descriptor[i] == 'L') { // object array
1722 do {
1723 i++; // find closing ;
1724 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1725 result = descriptor[i] == ';';
1726 } else { // primitive array
1727 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1728 }
1729 } else if (descriptor[0] == 'L') {
1730 // could be more thorough here, but shouldn't be required
1731 size_t i = 0;
1732 do {
1733 i++;
1734 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1735 result = descriptor[i] == ';';
1736 } else {
1737 result = false;
1738 }
1739 if (!result) {
1740 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1741 << descriptor << "'";
1742 }
1743 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001744}
1745
Ian Rogersd81871c2011-10-03 13:57:23 -07001746bool DexVerifier::CodeFlowVerifyMethod() {
1747 const uint16_t* insns = code_item_->insns_;
1748 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001749
jeffhaobdb76512011-09-07 11:43:16 -07001750 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 insn_flags_[0].SetChanged();
1752 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001753
jeffhaobdb76512011-09-07 11:43:16 -07001754 /* Continue until no instructions are marked "changed". */
1755 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1757 uint32_t insn_idx = start_guess;
1758 for (; insn_idx < insns_size; insn_idx++) {
1759 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
1761 }
jeffhaobdb76512011-09-07 11:43:16 -07001762 if (insn_idx == insns_size) {
1763 if (start_guess != 0) {
1764 /* try again, starting from the top */
1765 start_guess = 0;
1766 continue;
1767 } else {
1768 /* all flags are clear */
1769 break;
1770 }
1771 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 // We carry the working set of registers from instruction to instruction. If this address can
1773 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1774 // "changed" flags, we need to load the set of registers from the table.
1775 // Because we always prefer to continue on to the next instruction, we should never have a
1776 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1777 // target.
1778 work_insn_idx_ = insn_idx;
1779 if (insn_flags_[insn_idx].IsBranchTarget()) {
1780 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001781 } else {
1782#ifndef NDEBUG
1783 /*
1784 * Sanity check: retrieve the stored register line (assuming
1785 * a full table) and make sure it actually matches.
1786 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1788 if (register_line != NULL) {
1789 if (work_line_->CompareLine(register_line) != 0) {
1790 Dump(std::cout);
1791 std::cout << info_messages_.str();
1792 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1793 << "@" << (void*)work_insn_idx_ << std::endl
1794 << " work_line=" << *work_line_ << std::endl
1795 << " expected=" << *register_line;
1796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798#endif
1799 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 if (!CodeFlowVerifyInstruction(&start_guess)) {
1801 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001802 return false;
1803 }
jeffhaobdb76512011-09-07 11:43:16 -07001804 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 insn_flags_[insn_idx].SetVisited();
1806 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001807 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001808
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001810 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001811 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001812 * (besides the wasted space), but it indicates a flaw somewhere
1813 * down the line, possibly in the verifier.
1814 *
1815 * If we've substituted "always throw" instructions into the stream,
1816 * we are almost certainly going to have some dead code.
1817 */
1818 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 uint32_t insn_idx = 0;
1820 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001821 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001822 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001823 * may or may not be preceded by a padding NOP (for alignment).
1824 */
1825 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1826 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1827 insns[insn_idx] == Instruction::kArrayDataSignature ||
1828 (insns[insn_idx] == Instruction::NOP &&
1829 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1830 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1831 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001833 }
1834
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001836 if (dead_start < 0)
1837 dead_start = insn_idx;
1838 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001840 dead_start = -1;
1841 }
1842 }
1843 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001845 }
1846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 return true;
1848}
1849
Ian Rogersd81871c2011-10-03 13:57:23 -07001850bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001851#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001853 gDvm.verifierStats.instrsReexamined++;
1854 } else {
1855 gDvm.verifierStats.instrsExamined++;
1856 }
1857#endif
1858
1859 /*
1860 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001861 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001862 * control to another statement:
1863 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001864 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001865 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001866 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001867 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001868 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001869 * throw an exception that is handled by an encompassing "try"
1870 * block.
1871 *
1872 * We can also return, in which case there is no successor instruction
1873 * from this point.
1874 *
1875 * The behavior can be determined from the OpcodeFlags.
1876 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1878 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001879 Instruction::DecodedInstruction dec_insn(inst);
1880 int opcode_flag = inst->Flag();
1881
jeffhaobdb76512011-09-07 11:43:16 -07001882 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001883 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001884 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001886 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1887 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 }
jeffhaobdb76512011-09-07 11:43:16 -07001889
1890 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001891 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001892 * can throw an exception, we will copy/merge this into the "catch"
1893 * address rather than work_line, because we don't want the result
1894 * from the "successful" code path (e.g. a check-cast that "improves"
1895 * a type) to be visible to the exception handler.
1896 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1898 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001899 } else {
1900#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001902#endif
1903 }
1904
1905 switch (dec_insn.opcode_) {
1906 case Instruction::NOP:
1907 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001908 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001909 * a signature that looks like a NOP; if we see one of these in
1910 * the course of executing code then we have a problem.
1911 */
1912 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001914 }
1915 break;
1916
1917 case Instruction::MOVE:
1918 case Instruction::MOVE_FROM16:
1919 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001921 break;
1922 case Instruction::MOVE_WIDE:
1923 case Instruction::MOVE_WIDE_FROM16:
1924 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
1927 case Instruction::MOVE_OBJECT:
1928 case Instruction::MOVE_OBJECT_FROM16:
1929 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001931 break;
1932
1933 /*
1934 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001935 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001936 * might want to hold the result in an actual CPU register, so the
1937 * Dalvik spec requires that these only appear immediately after an
1938 * invoke or filled-new-array.
1939 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001941 * redundant with the reset done below, but it can make the debug info
1942 * easier to read in some cases.)
1943 */
1944 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
1947 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001949 break;
1950 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001952 break;
1953
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001955 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 * This statement can only appear as the first instruction in an exception handler (though not
1957 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001958 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001959 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001960 const RegType& res_type = GetCaughtExceptionType();
1961 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001962 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 }
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1966 if (!GetMethodReturnType().IsUnknown()) {
1967 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 }
1970 break;
1971 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001973 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 const RegType& return_type = GetMethodReturnType();
1975 if (!return_type.IsCategory1Types()) {
1976 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1977 } else {
1978 // Compilers may generate synthetic functions that write byte values into boolean fields.
1979 // Also, it may use integer values for boolean, byte, short, and character return types.
1980 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1981 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1982 ((return_type.IsBoolean() || return_type.IsByte() ||
1983 return_type.IsShort() || return_type.IsChar()) &&
1984 src_type.IsInteger()));
1985 /* check the register contents */
1986 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1987 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001988 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 }
1991 }
1992 break;
1993 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001995 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 const RegType& return_type = GetMethodReturnType();
1997 if (!return_type.IsCategory2Types()) {
1998 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1999 } else {
2000 /* check the register contents */
2001 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
2002 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002003 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 }
jeffhaobdb76512011-09-07 11:43:16 -07002005 }
2006 }
2007 break;
2008 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2010 const RegType& return_type = GetMethodReturnType();
2011 if (!return_type.IsReferenceTypes()) {
2012 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2013 } else {
2014 /* return_type is the *expected* return type, not register value */
2015 DCHECK(!return_type.IsZero());
2016 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002017 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2018 // Disallow returning uninitialized values and verify that the reference in vAA is an
2019 // instance of the "return_type"
2020 if (reg_type.IsUninitializedTypes()) {
2021 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2022 } else if (!return_type.IsAssignableFrom(reg_type)) {
2023 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2024 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002025 }
2026 }
2027 }
2028 break;
2029
2030 case Instruction::CONST_4:
2031 case Instruction::CONST_16:
2032 case Instruction::CONST:
2033 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::CONST_HIGH16:
2037 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 work_line_->SetRegisterType(dec_insn.vA_,
2039 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::CONST_WIDE_16:
2042 case Instruction::CONST_WIDE_32:
2043 case Instruction::CONST_WIDE:
2044 case Instruction::CONST_WIDE_HIGH16:
2045 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048 case Instruction::CONST_STRING:
2049 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002053 // Get type from instruction if unresolved then we need an access check
2054 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2055 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2056 // Register holds class, ie its type is class, but on error we keep it Unknown
2057 work_line_->SetRegisterType(dec_insn.vA_,
2058 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 }
jeffhaobdb76512011-09-07 11:43:16 -07002061 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::MONITOR_EXIT:
2065 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002067 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002068 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002069 * to the need to handle asynchronous exceptions, a now-deprecated
2070 * feature that Dalvik doesn't support.)
2071 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002072 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002073 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002074 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002075 * structured locking checks are working, the former would have
2076 * failed on the -enter instruction, and the latter is impossible.
2077 *
2078 * This is fortunate, because issue 3221411 prevents us from
2079 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002080 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002081 * some catch blocks (which will show up as "dead" code when
2082 * we skip them here); if we can't, then the code path could be
2083 * "live" so we still need to check it.
2084 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 opcode_flag &= ~Instruction::kThrow;
2086 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088
Ian Rogers28ad40d2011-10-27 15:19:26 -07002089 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002091 /*
2092 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2093 * could be a "upcast" -- not expected, so we don't try to address it.)
2094 *
2095 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2096 * dec_insn.vA_ when branching to a handler.
2097 */
2098 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2099 const RegType& res_type =
2100 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002101 if (res_type.IsUnknown()) {
2102 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2103 break; // couldn't resolve class
2104 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002105 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2106 const RegType& orig_type =
2107 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2108 if (!res_type.IsNonZeroReferenceTypes()) {
2109 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2110 } else if (!orig_type.IsReferenceTypes()) {
2111 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002112 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002113 if (is_checkcast) {
2114 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002117 }
jeffhaobdb76512011-09-07 11:43:16 -07002118 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002119 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 }
2121 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002122 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2123 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002124 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002125 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 } else {
2127 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2128 }
2129 }
2130 break;
2131 }
2132 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002133 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2134 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2135 // can't create an instance of an interface or abstract class */
2136 if (!res_type.IsInstantiableTypes()) {
2137 Fail(VERIFY_ERROR_INSTANTIATION)
2138 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002140 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2141 // Any registers holding previous allocations from this address that have not yet been
2142 // initialized must be marked invalid.
2143 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2144 // add the new uninitialized reference to the register state
2145 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 }
2147 break;
2148 }
2149 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002150 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2151 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2152 if (!res_type.IsArrayClass()) {
2153 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002154 } else {
2155 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002157 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002158 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002159 }
2160 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002164 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2165 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2166 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002168 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002169 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002170 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002171 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002172 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002173 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002174 just_set_result = true;
2175 }
2176 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 }
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::CMPL_FLOAT:
2179 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2181 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2182 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::CMPL_DOUBLE:
2185 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2187 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2188 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2192 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2193 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002196 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2197 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2198 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002199 }
2200 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 }
jeffhaobdb76512011-09-07 11:43:16 -07002202 case Instruction::GOTO:
2203 case Instruction::GOTO_16:
2204 case Instruction::GOTO_32:
2205 /* no effect on or use of registers */
2206 break;
2207
2208 case Instruction::PACKED_SWITCH:
2209 case Instruction::SPARSE_SWITCH:
2210 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002212 break;
2213
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 case Instruction::FILL_ARRAY_DATA: {
2215 /* Similar to the verification done for APUT */
2216 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2217 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002218 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 if (res_class != NULL) {
2220 Class* component_type = res_class->GetComponentType();
2221 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2222 component_type->IsPrimitiveVoid()) {
2223 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002224 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 } else {
2226 const RegType& value_type = reg_types_.FromClass(component_type);
2227 DCHECK(!value_type.IsUnknown());
2228 // Now verify if the element width in the table matches the element width declared in
2229 // the array
2230 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2231 if (array_data[0] != Instruction::kArrayDataSignature) {
2232 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2233 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002234 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 // Since we don't compress the data in Dex, expect to see equal width of data stored
2236 // in the table and expected from the array class.
2237 if (array_data[1] != elem_width) {
2238 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2239 << " vs " << elem_width << ")";
2240 }
2241 }
2242 }
jeffhaobdb76512011-09-07 11:43:16 -07002243 }
2244 }
2245 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 }
jeffhaobdb76512011-09-07 11:43:16 -07002247 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 case Instruction::IF_NE: {
2249 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2250 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2251 bool mismatch = false;
2252 if (reg_type1.IsZero()) { // zero then integral or reference expected
2253 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2254 } else if (reg_type1.IsReferenceTypes()) { // both references?
2255 mismatch = !reg_type2.IsReferenceTypes();
2256 } else { // both integral?
2257 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2258 }
2259 if (mismatch) {
2260 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2261 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002262 }
2263 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 }
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::IF_LT:
2266 case Instruction::IF_GE:
2267 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 case Instruction::IF_LE: {
2269 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2270 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2271 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2272 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2273 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002274 }
2275 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 }
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 case Instruction::IF_NEZ: {
2279 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2280 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2281 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2282 }
jeffhaobdb76512011-09-07 11:43:16 -07002283 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 case Instruction::IF_LTZ:
2286 case Instruction::IF_GEZ:
2287 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 case Instruction::IF_LEZ: {
2289 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2290 if (!reg_type.IsIntegralTypes()) {
2291 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2292 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2293 }
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 }
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2301 break;
jeffhaobdb76512011-09-07 11:43:16 -07002302 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002303 VerifyAGet(dec_insn, reg_types_.Char(), true);
2304 break;
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 case Instruction::AGET:
2309 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 VerifyAGet(dec_insn, reg_types_.Long(), true);
2313 break;
2314 case Instruction::AGET_OBJECT:
2315 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 case Instruction::APUT_BOOLEAN:
2319 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2320 break;
2321 case Instruction::APUT_BYTE:
2322 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2323 break;
2324 case Instruction::APUT_CHAR:
2325 VerifyAPut(dec_insn, reg_types_.Char(), true);
2326 break;
2327 case Instruction::APUT_SHORT:
2328 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002341 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002344 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002347 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
jeffhaobdb76512011-09-07 11:43:16 -07002349 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002350 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 break;
2352 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002353 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002356 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002359 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 break;
jeffhaobdb76512011-09-07 11:43:16 -07002361
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002363 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 break;
2365 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002366 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 break;
2368 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002369 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 break;
2371 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002372 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002375 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002378 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 break;
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002381 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383
jeffhaobdb76512011-09-07 11:43:16 -07002384 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002385 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 break;
jeffhaobdb76512011-09-07 11:43:16 -07002387 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002388 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 break;
jeffhaobdb76512011-09-07 11:43:16 -07002390 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002391 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 break;
jeffhaobdb76512011-09-07 11:43:16 -07002393 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002394 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 break;
2396 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002397 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002398 break;
2399 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002400 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002403 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 break;
2405
2406 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002407 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 break;
2409 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002410 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
2412 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002413 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
2415 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002416 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002419 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002422 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002425 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427
2428 case Instruction::INVOKE_VIRTUAL:
2429 case Instruction::INVOKE_VIRTUAL_RANGE:
2430 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 case Instruction::INVOKE_SUPER_RANGE: {
2432 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2433 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2434 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2435 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2436 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2437 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002438 const char* descriptor;
2439 if (called_method == NULL) {
2440 uint32_t method_idx = dec_insn.vB_;
2441 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2442 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002443 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002444 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002445 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002446 }
Ian Rogers9074b992011-10-26 17:41:55 -07002447 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002448 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002450 just_set_result = true;
2451 }
2452 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 }
jeffhaobdb76512011-09-07 11:43:16 -07002454 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 case Instruction::INVOKE_DIRECT_RANGE: {
2456 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2457 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2458 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002459 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 * Some additional checks when calling a constructor. We know from the invocation arg check
2461 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2462 * that to require that called_method->klass is the same as this->klass or this->super,
2463 * allowing the latter only if the "this" argument is the same as the "this" argument to
2464 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002465 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 bool is_constructor;
2467 if (called_method != NULL) {
2468 is_constructor = called_method->IsConstructor();
2469 } else {
2470 uint32_t method_idx = dec_insn.vB_;
2471 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2472 const char* name = dex_file_->GetMethodName(method_id);
2473 is_constructor = strcmp(name, "<init>") == 0;
2474 }
2475 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2477 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479
2480 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 if (this_type.IsZero()) {
2482 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002485 if (called_method != NULL) {
2486 Class* this_class = this_type.GetClass();
2487 DCHECK(this_class != NULL);
2488 /* must be in same class or in superclass */
2489 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2490 if (this_class != method_->GetDeclaringClass()) {
2491 Fail(VERIFY_ERROR_GENERIC)
2492 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2493 break;
2494 }
2495 } else if (called_method->GetDeclaringClass() != this_class) {
2496 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 }
jeffhaobdb76512011-09-07 11:43:16 -07002499 }
2500
2501 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002502 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2504 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 }
2507
2508 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002509 * Replace the uninitialized reference with an initialized one. We need to do this for all
2510 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002511 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 work_line_->MarkRefsAsInitialized(this_type);
2513 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002515 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002516 const char* descriptor;
2517 if (called_method == NULL) {
2518 uint32_t method_idx = dec_insn.vB_;
2519 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2520 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002521 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002522 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002523 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002524 }
Ian Rogers9074b992011-10-26 17:41:55 -07002525 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002526 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002528 just_set_result = true;
2529 }
2530 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 }
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 case Instruction::INVOKE_STATIC_RANGE: {
2534 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2535 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2536 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002537 const char* descriptor;
2538 if (called_method == NULL) {
2539 uint32_t method_idx = dec_insn.vB_;
2540 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2541 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002542 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002543 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002544 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002545 }
Ian Rogers9074b992011-10-26 17:41:55 -07002546 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002547 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 work_line_->SetResultRegisterType(return_type);
2549 just_set_result = true;
2550 }
jeffhaobdb76512011-09-07 11:43:16 -07002551 }
2552 break;
2553 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 case Instruction::INVOKE_INTERFACE_RANGE: {
2555 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2556 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2557 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 if (abs_method != NULL) {
2559 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002560 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002561 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2562 << PrettyMethod(abs_method) << "'";
2563 break;
2564 }
2565 }
2566 /* Get the type of the "this" arg, which should either be a sub-interface of called
2567 * interface or Object (see comments in RegType::JoinClass).
2568 */
2569 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2570 if (failure_ == VERIFY_ERROR_NONE) {
2571 if (this_type.IsZero()) {
2572 /* null pointer always passes (and always fails at runtime) */
2573 } else {
2574 if (this_type.IsUninitializedTypes()) {
2575 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2576 << this_type;
2577 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 // In the past we have tried to assert that "called_interface" is assignable
2580 // from "this_type.GetClass()", however, as we do an imprecise Join
2581 // (RegType::JoinClass) we don't have full information on what interfaces are
2582 // implemented by "this_type". For example, two classes may implement the same
2583 // interfaces and have a common parent that doesn't implement the interface. The
2584 // join will set "this_type" to the parent class and a test that this implements
2585 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002586 }
2587 }
jeffhaobdb76512011-09-07 11:43:16 -07002588 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 * We don't have an object instance, so we can't find the concrete method. However, all of
2590 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002591 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002592 const char* descriptor;
2593 if (abs_method == NULL) {
2594 uint32_t method_idx = dec_insn.vB_;
2595 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2596 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002597 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002598 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002599 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002600 }
Ian Rogers9074b992011-10-26 17:41:55 -07002601 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002602 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2603 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002605 just_set_result = true;
2606 }
2607 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 }
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::NEG_INT:
2610 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::NEG_LONG:
2614 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002619 break;
2620 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002622 break;
2623 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668
2669 case Instruction::ADD_INT:
2670 case Instruction::SUB_INT:
2671 case Instruction::MUL_INT:
2672 case Instruction::REM_INT:
2673 case Instruction::DIV_INT:
2674 case Instruction::SHL_INT:
2675 case Instruction::SHR_INT:
2676 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::AND_INT:
2680 case Instruction::OR_INT:
2681 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::ADD_LONG:
2685 case Instruction::SUB_LONG:
2686 case Instruction::MUL_LONG:
2687 case Instruction::DIV_LONG:
2688 case Instruction::REM_LONG:
2689 case Instruction::AND_LONG:
2690 case Instruction::OR_LONG:
2691 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::SHL_LONG:
2695 case Instruction::SHR_LONG:
2696 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 /* shift distance is Int, making these different from other binary operations */
2698 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::ADD_FLOAT:
2701 case Instruction::SUB_FLOAT:
2702 case Instruction::MUL_FLOAT:
2703 case Instruction::DIV_FLOAT:
2704 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::ADD_DOUBLE:
2708 case Instruction::SUB_DOUBLE:
2709 case Instruction::MUL_DOUBLE:
2710 case Instruction::DIV_DOUBLE:
2711 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002713 break;
2714 case Instruction::ADD_INT_2ADDR:
2715 case Instruction::SUB_INT_2ADDR:
2716 case Instruction::MUL_INT_2ADDR:
2717 case Instruction::REM_INT_2ADDR:
2718 case Instruction::SHL_INT_2ADDR:
2719 case Instruction::SHR_INT_2ADDR:
2720 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::AND_INT_2ADDR:
2724 case Instruction::OR_INT_2ADDR:
2725 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002727 break;
2728 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::ADD_LONG_2ADDR:
2732 case Instruction::SUB_LONG_2ADDR:
2733 case Instruction::MUL_LONG_2ADDR:
2734 case Instruction::DIV_LONG_2ADDR:
2735 case Instruction::REM_LONG_2ADDR:
2736 case Instruction::AND_LONG_2ADDR:
2737 case Instruction::OR_LONG_2ADDR:
2738 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::SHL_LONG_2ADDR:
2742 case Instruction::SHR_LONG_2ADDR:
2743 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::ADD_FLOAT_2ADDR:
2747 case Instruction::SUB_FLOAT_2ADDR:
2748 case Instruction::MUL_FLOAT_2ADDR:
2749 case Instruction::DIV_FLOAT_2ADDR:
2750 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::ADD_DOUBLE_2ADDR:
2754 case Instruction::SUB_DOUBLE_2ADDR:
2755 case Instruction::MUL_DOUBLE_2ADDR:
2756 case Instruction::DIV_DOUBLE_2ADDR:
2757 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::ADD_INT_LIT16:
2761 case Instruction::RSUB_INT:
2762 case Instruction::MUL_INT_LIT16:
2763 case Instruction::DIV_INT_LIT16:
2764 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::AND_INT_LIT16:
2768 case Instruction::OR_INT_LIT16:
2769 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002771 break;
2772 case Instruction::ADD_INT_LIT8:
2773 case Instruction::RSUB_INT_LIT8:
2774 case Instruction::MUL_INT_LIT8:
2775 case Instruction::DIV_INT_LIT8:
2776 case Instruction::REM_INT_LIT8:
2777 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002778 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002779 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::AND_INT_LIT8:
2783 case Instruction::OR_INT_LIT8:
2784 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002786 break;
2787
2788 /*
2789 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002790 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002791 * inserted in the course of verification, we can expect to see it here.
2792 */
jeffhaob4df5142011-09-19 20:25:32 -07002793 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002797 case Instruction::UNUSED_EE:
2798 case Instruction::UNUSED_EF:
2799 case Instruction::UNUSED_F2:
2800 case Instruction::UNUSED_F3:
2801 case Instruction::UNUSED_F4:
2802 case Instruction::UNUSED_F5:
2803 case Instruction::UNUSED_F6:
2804 case Instruction::UNUSED_F7:
2805 case Instruction::UNUSED_F8:
2806 case Instruction::UNUSED_F9:
2807 case Instruction::UNUSED_FA:
2808 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002809 case Instruction::UNUSED_F0:
2810 case Instruction::UNUSED_F1:
2811 case Instruction::UNUSED_E3:
2812 case Instruction::UNUSED_E8:
2813 case Instruction::UNUSED_E7:
2814 case Instruction::UNUSED_E4:
2815 case Instruction::UNUSED_E9:
2816 case Instruction::UNUSED_FC:
2817 case Instruction::UNUSED_E5:
2818 case Instruction::UNUSED_EA:
2819 case Instruction::UNUSED_FD:
2820 case Instruction::UNUSED_E6:
2821 case Instruction::UNUSED_EB:
2822 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002823 case Instruction::UNUSED_3E:
2824 case Instruction::UNUSED_3F:
2825 case Instruction::UNUSED_40:
2826 case Instruction::UNUSED_41:
2827 case Instruction::UNUSED_42:
2828 case Instruction::UNUSED_43:
2829 case Instruction::UNUSED_73:
2830 case Instruction::UNUSED_79:
2831 case Instruction::UNUSED_7A:
2832 case Instruction::UNUSED_EC:
2833 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002834 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836
2837 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002838 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002839 * complain if an instruction is missing (which is desirable).
2840 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002842
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 if (failure_ != VERIFY_ERROR_NONE) {
2844 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002845 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002846 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002847 return false;
2848 } else {
2849 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002850 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002852 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002854 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002856 opcode_flag = Instruction::kThrow;
2857 }
2858 }
jeffhaobdb76512011-09-07 11:43:16 -07002859 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 * If we didn't just set the result register, clear it out. This ensures that you can only use
2861 * "move-result" immediately after the result is set. (We could check this statically, but it's
2862 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002863 */
2864 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002866 }
2867
jeffhaoa0a764a2011-09-16 10:43:38 -07002868 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002869 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2871 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2872 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002873 return false;
2874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2876 // next instruction isn't one.
2877 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002878 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 }
2880 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2881 if (next_line != NULL) {
2882 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2883 // needed.
2884 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002885 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 }
jeffhaobdb76512011-09-07 11:43:16 -07002887 } else {
2888 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 * We're not recording register data for the next instruction, so we don't know what the prior
2890 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002891 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002893 }
2894 }
2895
2896 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002897 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002898 *
2899 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002900 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002901 * somebody could get a reference field, check it for zero, and if the
2902 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002903 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002904 * that, and will reject the code.
2905 *
2906 * TODO: avoid re-fetching the branch target
2907 */
2908 if ((opcode_flag & Instruction::kBranch) != 0) {
2909 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002911 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002913 return false;
2914 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002915 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002917 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 }
jeffhaobdb76512011-09-07 11:43:16 -07002919 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002921 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 }
jeffhaobdb76512011-09-07 11:43:16 -07002923 }
2924
2925 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002926 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002927 *
2928 * We've already verified that the table is structurally sound, so we
2929 * just need to walk through and tag the targets.
2930 */
2931 if ((opcode_flag & Instruction::kSwitch) != 0) {
2932 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2933 const uint16_t* switch_insns = insns + offset_to_switch;
2934 int switch_count = switch_insns[1];
2935 int offset_to_targets, targ;
2936
2937 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2938 /* 0 = sig, 1 = count, 2/3 = first key */
2939 offset_to_targets = 4;
2940 } else {
2941 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002942 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002943 offset_to_targets = 2 + 2 * switch_count;
2944 }
2945
2946 /* verify each switch target */
2947 for (targ = 0; targ < switch_count; targ++) {
2948 int offset;
2949 uint32_t abs_offset;
2950
2951 /* offsets are 32-bit, and only partly endian-swapped */
2952 offset = switch_insns[offset_to_targets + targ * 2] |
2953 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 abs_offset = work_insn_idx_ + offset;
2955 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2956 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002957 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 }
2959 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002960 return false;
2961 }
2962 }
2963
2964 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2966 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002967 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2969 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002970 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002971
Ian Rogers0571d352011-11-03 19:51:38 -07002972 for (; iterator.HasNext(); iterator.Next()) {
2973 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 within_catch_all = true;
2975 }
jeffhaobdb76512011-09-07 11:43:16 -07002976 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2978 * "work_regs", because at runtime the exception will be thrown before the instruction
2979 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002980 */
Ian Rogers0571d352011-11-03 19:51:38 -07002981 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002982 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 }
jeffhaobdb76512011-09-07 11:43:16 -07002984 }
2985
2986 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2988 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002989 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002991 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 * The state in work_line reflects the post-execution state. If the current instruction is a
2993 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002994 * it will do so before grabbing the lock).
2995 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2997 Fail(VERIFY_ERROR_GENERIC)
2998 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002999 return false;
3000 }
3001 }
3002 }
3003
jeffhaod1f0fde2011-09-08 17:25:33 -07003004 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 if ((opcode_flag & Instruction::kReturn) != 0) {
3006 if(!work_line_->VerifyMonitorStackEmpty()) {
3007 return false;
3008 }
jeffhaobdb76512011-09-07 11:43:16 -07003009 }
3010
3011 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003012 * Update start_guess. Advance to the next instruction of that's
3013 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003014 * neither of those exists we're in a return or throw; leave start_guess
3015 * alone and let the caller sort it out.
3016 */
3017 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003019 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3020 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003022 }
3023
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3025 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003026
3027 return true;
3028}
3029
Ian Rogers28ad40d2011-10-27 15:19:26 -07003030const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003031 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003032 Class* referrer = method_->GetDeclaringClass();
3033 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3034 const RegType& result =
3035 klass != NULL ? reg_types_.FromClass(klass)
3036 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3037 if (klass == NULL && !result.IsUnresolvedTypes()) {
3038 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003040 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3041 // check at runtime if access is allowed and so pass here.
3042 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3043 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003044 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003045 << result << "'";
3046 return reg_types_.Unknown();
3047 } else {
3048 return result;
3049 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003050}
3051
Ian Rogers28ad40d2011-10-27 15:19:26 -07003052const RegType& DexVerifier::GetCaughtExceptionType() {
3053 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003055 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3057 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003058 CatchHandlerIterator iterator(handlers_ptr);
3059 for (; iterator.HasNext(); iterator.Next()) {
3060 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3061 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003064 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3066 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3067 * test, so is essentially harmless.
3068 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003069 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3070 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3071 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003073 common_super = &exception;
3074 } else if (common_super->Equals(exception)) {
3075 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003077 common_super = &common_super->Merge(exception, &reg_types_);
3078 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 }
3080 }
3081 }
3082 }
Ian Rogers0571d352011-11-03 19:51:38 -07003083 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 }
3085 }
3086 if (common_super == NULL) {
3087 /* no catch blocks, or no catches with classes we can find */
3088 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3089 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003090 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003091}
3092
3093Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003094 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3095 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3096 if (failure_ != VERIFY_ERROR_NONE) {
3097 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3098 return NULL;
3099 }
3100 if(klass_type.IsUnresolvedTypes()) {
3101 return NULL; // Can't resolve Class so no more to do here
3102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 Class* referrer = method_->GetDeclaringClass();
3104 DexCache* dex_cache = referrer->GetDexCache();
3105 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3106 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003107 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003108 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003109 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 if (is_direct) {
3111 res_method = klass->FindDirectMethod(name, signature);
3112 } else if (klass->IsInterface()) {
3113 res_method = klass->FindInterfaceMethod(name, signature);
3114 } else {
3115 res_method = klass->FindVirtualMethod(name, signature);
3116 }
3117 if (res_method != NULL) {
3118 dex_cache->SetResolvedMethod(method_idx, res_method);
3119 } else {
3120 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003121 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 << " " << signature;
3123 return NULL;
3124 }
3125 }
3126 /* Check if access is allowed. */
3127 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3128 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003129 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 return NULL;
3131 }
3132 return res_method;
3133}
3134
3135Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3136 MethodType method_type, bool is_range, bool is_super) {
3137 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3138 // we're making.
3139 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3140 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003141 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 return NULL;
3143 }
3144 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3145 // enforce them here.
3146 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3147 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3148 << PrettyMethod(res_method);
3149 return NULL;
3150 }
3151 // See if the method type implied by the invoke instruction matches the access flags for the
3152 // target method.
3153 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3154 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3155 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3156 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003157 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3158 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 return NULL;
3160 }
3161 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3162 // has a vtable entry for the target method.
3163 if (is_super) {
3164 DCHECK(method_type == METHOD_VIRTUAL);
3165 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3166 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3167 if (super == NULL) { // Only Object has no super class
3168 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3169 << " to super " << PrettyMethod(res_method);
3170 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003171 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003173 << " to super " << PrettyDescriptor(super)
3174 << "." << mh.GetName()
3175 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 }
3177 return NULL;
3178 }
3179 }
3180 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3181 // match the call to the signature. Also, we might might be calling through an abstract method
3182 // definition (which doesn't have register count values).
3183 int expected_args = dec_insn.vA_;
3184 /* caught by static verifier */
3185 DCHECK(is_range || expected_args <= 5);
3186 if (expected_args > code_item_->outs_size_) {
3187 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3188 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3189 return NULL;
3190 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003191 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 if (sig[0] != '(') {
3193 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3194 << " as descriptor doesn't start with '(': " << sig;
3195 return NULL;
3196 }
jeffhaobdb76512011-09-07 11:43:16 -07003197 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 * Check the "this" argument, which must be an instance of the class
3199 * that declared the method. For an interface class, we don't do the
3200 * full interface merge, so we can't do a rigorous check here (which
3201 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003202 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 int actual_args = 0;
3204 if (!res_method->IsStatic()) {
3205 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3206 if (failure_ != VERIFY_ERROR_NONE) {
3207 return NULL;
3208 }
3209 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3210 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3211 return NULL;
3212 }
3213 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003214 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3215 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3216 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3217 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 return NULL;
3219 }
3220 }
3221 actual_args++;
3222 }
3223 /*
3224 * Process the target method's signature. This signature may or may not
3225 * have been verified, so we can't assume it's properly formed.
3226 */
3227 size_t sig_offset = 0;
3228 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3229 if (actual_args >= expected_args) {
3230 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3231 << "'. Expected " << expected_args << " args, found more ("
3232 << sig.substr(sig_offset) << ")";
3233 return NULL;
3234 }
3235 std::string descriptor;
3236 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3237 size_t end;
3238 if (sig[sig_offset] == 'L') {
3239 end = sig.find(';', sig_offset);
3240 } else {
3241 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3242 if (sig[end] == 'L') {
3243 end = sig.find(';', end);
3244 }
3245 }
3246 if (end == std::string::npos) {
3247 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3248 << "bad signature component '" << sig << "' (missing ';')";
3249 return NULL;
3250 }
3251 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3252 sig_offset = end;
3253 } else {
3254 descriptor = sig[sig_offset];
3255 }
3256 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003257 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3258 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003259 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3260 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3261 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 }
3263 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3264 }
3265 if (sig[sig_offset] != ')') {
3266 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3267 return NULL;
3268 }
3269 if (actual_args != expected_args) {
3270 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3271 << " expected " << expected_args << " args, found " << actual_args;
3272 return NULL;
3273 } else {
3274 return res_method;
3275 }
3276}
3277
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003278const RegType& DexVerifier::GetMethodReturnType() {
3279 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3280 MethodHelper(method_).GetReturnTypeDescriptor());
3281}
3282
Ian Rogersd81871c2011-10-03 13:57:23 -07003283void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3284 const RegType& insn_type, bool is_primitive) {
3285 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3286 if (!index_type.IsArrayIndexTypes()) {
3287 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3288 } else {
3289 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3290 if (failure_ == VERIFY_ERROR_NONE) {
3291 if (array_class == NULL) {
3292 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3293 // instruction type. TODO: have a proper notion of bottom here.
3294 if (!is_primitive || insn_type.IsCategory1Types()) {
3295 // Reference or category 1
3296 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3297 } else {
3298 // Category 2
3299 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3300 }
3301 } else {
3302 /* verify the class */
3303 Class* component_class = array_class->GetComponentType();
3304 const RegType& component_type = reg_types_.FromClass(component_class);
3305 if (!array_class->IsArrayClass()) {
3306 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003307 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 } else if (component_class->IsPrimitive() && !is_primitive) {
3309 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003310 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 << " source for aget-object";
3312 } else if (!component_class->IsPrimitive() && is_primitive) {
3313 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003314 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 << " source for category 1 aget";
3316 } else if (is_primitive && !insn_type.Equals(component_type) &&
3317 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3318 (insn_type.IsLong() && component_type.IsDouble()))) {
3319 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003320 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 << " incompatible with aget of type " << insn_type;
3322 } else {
3323 // Use knowledge of the field type which is stronger than the type inferred from the
3324 // instruction, which can't differentiate object types and ints from floats, longs from
3325 // doubles.
3326 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3327 }
3328 }
3329 }
3330 }
3331}
3332
3333void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3334 const RegType& insn_type, bool is_primitive) {
3335 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3336 if (!index_type.IsArrayIndexTypes()) {
3337 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3338 } else {
3339 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3340 if (failure_ == VERIFY_ERROR_NONE) {
3341 if (array_class == NULL) {
3342 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3343 // instruction type.
3344 } else {
3345 /* verify the class */
3346 Class* component_class = array_class->GetComponentType();
3347 const RegType& component_type = reg_types_.FromClass(component_class);
3348 if (!array_class->IsArrayClass()) {
3349 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003350 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 } else if (component_class->IsPrimitive() && !is_primitive) {
3352 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003353 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 << " source for aput-object";
3355 } else if (!component_class->IsPrimitive() && is_primitive) {
3356 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003357 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 << " source for category 1 aput";
3359 } else if (is_primitive && !insn_type.Equals(component_type) &&
3360 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3361 (insn_type.IsLong() && component_type.IsDouble()))) {
3362 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003363 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 << " incompatible with aput of type " << insn_type;
3365 } else {
3366 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003367 // Note: we use the instruction type (rather than the component type) for aput-object as
3368 // incompatible classes will be caught at runtime as an array store exception
3369 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 }
3371 }
3372 }
3373 }
3374}
3375
3376Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003377 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3378 // Check access to class
3379 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3380 if (failure_ != VERIFY_ERROR_NONE) {
3381 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3382 << dex_file_->GetFieldName(field_id) << ") in "
3383 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3384 return NULL;
3385 }
3386 if(klass_type.IsUnresolvedTypes()) {
3387 return NULL; // Can't resolve Class so no more to do here
3388 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003389 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003391 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3392 << dex_file_->GetFieldName(field_id) << ") in "
3393 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 DCHECK(Thread::Current()->IsExceptionPending());
3395 Thread::Current()->ClearException();
3396 return NULL;
3397 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3398 field->GetAccessFlags())) {
3399 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3400 << " from " << PrettyClass(method_->GetDeclaringClass());
3401 return NULL;
3402 } else if (!field->IsStatic()) {
3403 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3404 return NULL;
3405 } else {
3406 return field;
3407 }
3408}
3409
Ian Rogersd81871c2011-10-03 13:57:23 -07003410Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003411 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3412 // Check access to class
3413 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3414 if (failure_ != VERIFY_ERROR_NONE) {
3415 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3416 << dex_file_->GetFieldName(field_id) << ") in "
3417 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3418 return NULL;
3419 }
3420 if(klass_type.IsUnresolvedTypes()) {
3421 return NULL; // Can't resolve Class so no more to do here
3422 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003423 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003424 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003425 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3426 << dex_file_->GetFieldName(field_id) << ") in "
3427 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 DCHECK(Thread::Current()->IsExceptionPending());
3429 Thread::Current()->ClearException();
3430 return NULL;
3431 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3432 field->GetAccessFlags())) {
3433 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3434 << " from " << PrettyClass(method_->GetDeclaringClass());
3435 return NULL;
3436 } else if (field->IsStatic()) {
3437 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3438 << " to not be static";
3439 return NULL;
3440 } else if (obj_type.IsZero()) {
3441 // Cannot infer and check type, however, access will cause null pointer exception
3442 return field;
3443 } else if(obj_type.IsUninitializedReference() &&
3444 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3445 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3446 // Field accesses through uninitialized references are only allowable for constructors where
3447 // the field is declared in this class
3448 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3449 << " of a not fully initialized object within the context of "
3450 << PrettyMethod(method_);
3451 return NULL;
3452 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3453 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3454 // of C1. For resolution to occur the declared class of the field must be compatible with
3455 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3456 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3457 << " from object of type " << PrettyClass(obj_type.GetClass());
3458 return NULL;
3459 } else {
3460 return field;
3461 }
3462}
3463
Ian Rogersb94a27b2011-10-26 00:33:41 -07003464void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3465 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003466 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003467 Field* field;
3468 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003469 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003470 } else {
3471 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003472 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003473 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003474 if (failure_ != VERIFY_ERROR_NONE) {
3475 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3476 } else {
3477 const char* descriptor;
3478 const ClassLoader* loader;
3479 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003480 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003481 loader = field->GetDeclaringClass()->GetClassLoader();
3482 } else {
3483 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3484 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3485 loader = method_->GetDeclaringClass()->GetClassLoader();
3486 }
3487 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003489 if (field_type.Equals(insn_type) ||
3490 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3491 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 // expected that read is of the correct primitive type or that int reads are reading
3493 // floats or long reads are reading doubles
3494 } else {
3495 // This is a global failure rather than a class change failure as the instructions and
3496 // the descriptors for the type should have been consistent within the same file at
3497 // compile time
3498 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003499 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003500 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 return;
3502 }
3503 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003504 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003506 << " to be compatible with type '" << insn_type
3507 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003508 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 return;
3510 }
3511 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003512 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 }
3514}
3515
Ian Rogersb94a27b2011-10-26 00:33:41 -07003516void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3517 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003518 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003519 Field* field;
3520 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003521 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003522 } else {
3523 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003524 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003525 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003526 if (failure_ != VERIFY_ERROR_NONE) {
3527 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3528 } else {
3529 const char* descriptor;
3530 const ClassLoader* loader;
3531 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003532 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003533 loader = field->GetDeclaringClass()->GetClassLoader();
3534 } else {
3535 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3536 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3537 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003539 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3540 if (field != NULL) {
3541 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3542 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3543 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3544 return;
3545 }
3546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003548 // Primitive field assignability rules are weaker than regular assignability rules
3549 bool instruction_compatible;
3550 bool value_compatible;
3551 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3552 if (field_type.IsIntegralTypes()) {
3553 instruction_compatible = insn_type.IsIntegralTypes();
3554 value_compatible = value_type.IsIntegralTypes();
3555 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003556 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003557 value_compatible = value_type.IsFloatTypes();
3558 } else if (field_type.IsLong()) {
3559 instruction_compatible = insn_type.IsLong();
3560 value_compatible = value_type.IsLongTypes();
3561 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003562 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003563 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003564 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003565 instruction_compatible = false; // reference field with primitive store
3566 value_compatible = false; // unused
3567 }
3568 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 // This is a global failure rather than a class change failure as the instructions and
3570 // the descriptors for the type should have been consistent within the same file at
3571 // compile time
3572 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003573 << " to be of type '" << insn_type
3574 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003575 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 return;
3577 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003578 if (!value_compatible) {
3579 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3580 << " of type " << value_type
3581 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003582 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003583 return;
3584 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003586 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003588 << " to be compatible with type '" << insn_type
3589 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003590 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 return;
3592 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003593 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003595 }
3596}
3597
3598bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3599 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3600 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3601 return false;
3602 }
3603 return true;
3604}
3605
3606void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003607 const RegType& res_type, bool is_range) {
3608 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 /*
3610 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3611 * list and fail. It's legal, if silly, for arg_count to be zero.
3612 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003613 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3614 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 uint32_t arg_count = dec_insn.vA_;
3616 for (size_t ui = 0; ui < arg_count; ui++) {
3617 uint32_t get_reg;
3618
3619 if (is_range)
3620 get_reg = dec_insn.vC_ + ui;
3621 else
3622 get_reg = dec_insn.arg_[ui];
3623
3624 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3625 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3626 << ") not valid";
3627 return;
3628 }
3629 }
3630}
3631
3632void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003633 if (Runtime::Current()->IsStarted()) {
3634 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3635 << " " << fail_messages_.str();
3636 return;
3637 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3639 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3640 VerifyErrorRefType ref_type;
3641 switch (inst->Opcode()) {
3642 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003643 case Instruction::CHECK_CAST:
3644 case Instruction::INSTANCE_OF:
3645 case Instruction::NEW_INSTANCE:
3646 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003648 case Instruction::FILLED_NEW_ARRAY_RANGE:
3649 ref_type = VERIFY_ERROR_REF_CLASS;
3650 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003651 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003652 case Instruction::IGET_BOOLEAN:
3653 case Instruction::IGET_BYTE:
3654 case Instruction::IGET_CHAR:
3655 case Instruction::IGET_SHORT:
3656 case Instruction::IGET_WIDE:
3657 case Instruction::IGET_OBJECT:
3658 case Instruction::IPUT:
3659 case Instruction::IPUT_BOOLEAN:
3660 case Instruction::IPUT_BYTE:
3661 case Instruction::IPUT_CHAR:
3662 case Instruction::IPUT_SHORT:
3663 case Instruction::IPUT_WIDE:
3664 case Instruction::IPUT_OBJECT:
3665 case Instruction::SGET:
3666 case Instruction::SGET_BOOLEAN:
3667 case Instruction::SGET_BYTE:
3668 case Instruction::SGET_CHAR:
3669 case Instruction::SGET_SHORT:
3670 case Instruction::SGET_WIDE:
3671 case Instruction::SGET_OBJECT:
3672 case Instruction::SPUT:
3673 case Instruction::SPUT_BOOLEAN:
3674 case Instruction::SPUT_BYTE:
3675 case Instruction::SPUT_CHAR:
3676 case Instruction::SPUT_SHORT:
3677 case Instruction::SPUT_WIDE:
3678 case Instruction::SPUT_OBJECT:
3679 ref_type = VERIFY_ERROR_REF_FIELD;
3680 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003682 case Instruction::INVOKE_VIRTUAL_RANGE:
3683 case Instruction::INVOKE_SUPER:
3684 case Instruction::INVOKE_SUPER_RANGE:
3685 case Instruction::INVOKE_DIRECT:
3686 case Instruction::INVOKE_DIRECT_RANGE:
3687 case Instruction::INVOKE_STATIC:
3688 case Instruction::INVOKE_STATIC_RANGE:
3689 case Instruction::INVOKE_INTERFACE:
3690 case Instruction::INVOKE_INTERFACE_RANGE:
3691 ref_type = VERIFY_ERROR_REF_METHOD;
3692 break;
jeffhaobdb76512011-09-07 11:43:16 -07003693 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003694 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003695 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003696 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3698 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3699 // instruction, so assert it.
3700 size_t width = inst->SizeInCodeUnits();
3701 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003702 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 // NOPs
3704 for (size_t i = 2; i < width; i++) {
3705 insns[work_insn_idx_ + i] = Instruction::NOP;
3706 }
3707 // Encode the opcode, with the failure code in the high byte
3708 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3709 (failure_ << 8) | // AA - component
3710 (ref_type << (8 + kVerifyErrorRefTypeShift));
3711 insns[work_insn_idx_] = new_instruction;
3712 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3713 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003714 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3715 << fail_messages_.str();
3716 if (gDebugVerify) {
3717 std::cout << std::endl << info_messages_.str();
3718 Dump(std::cout);
3719 }
jeffhaobdb76512011-09-07 11:43:16 -07003720}
jeffhaoba5ebb92011-08-25 17:24:37 -07003721
Ian Rogersd81871c2011-10-03 13:57:23 -07003722bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3723 const bool merge_debug = true;
3724 bool changed = true;
3725 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3726 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003727 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 * We haven't processed this instruction before, and we haven't touched the registers here, so
3729 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3730 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003731 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003733 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003734 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3735 copy->CopyFromLine(target_line);
3736 changed = target_line->MergeRegisters(merge_line);
3737 if (failure_ != VERIFY_ERROR_NONE) {
3738 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003739 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003740 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003741 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3742 << *copy.get() << " MERGE" << std::endl
3743 << *merge_line << " ==" << std::endl
3744 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003745 }
3746 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003747 if (changed) {
3748 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003749 }
3750 return true;
3751}
3752
Ian Rogersd81871c2011-10-03 13:57:23 -07003753void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3754 size_t* log2_max_gc_pc) {
3755 size_t local_gc_points = 0;
3756 size_t max_insn = 0;
3757 size_t max_ref_reg = -1;
3758 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3759 if (insn_flags_[i].IsGcPoint()) {
3760 local_gc_points++;
3761 max_insn = i;
3762 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003763 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003764 }
3765 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 *gc_points = local_gc_points;
3767 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3768 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003769 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 i++;
3771 }
3772 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003773}
3774
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003775const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 size_t num_entries, ref_bitmap_bits, pc_bits;
3777 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3778 // There's a single byte to encode the size of each bitmap
3779 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3780 // TODO: either a better GC map format or per method failures
3781 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3782 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003783 return NULL;
3784 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3786 // There are 2 bytes to encode the number of entries
3787 if (num_entries >= 65536) {
3788 // TODO: either a better GC map format or per method failures
3789 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3790 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003791 return NULL;
3792 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003794 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003795 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003796 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003798 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003799 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003801 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 // TODO: either a better GC map format or per method failures
3803 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3804 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3805 return NULL;
3806 }
3807 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003808 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 if (table == NULL) {
3810 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3811 return NULL;
3812 }
3813 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003814 table->push_back(format);
3815 table->push_back(ref_bitmap_bytes);
3816 table->push_back(num_entries & 0xFF);
3817 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003818 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3820 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003821 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003823 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 }
3825 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003826 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003827 }
3828 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003829 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 return table;
3831}
jeffhaoa0a764a2011-09-16 10:43:38 -07003832
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003833void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3835 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003836 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003837 size_t map_index = 0;
3838 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3839 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3840 if (insn_flags_[i].IsGcPoint()) {
3841 CHECK_LT(map_index, map.NumEntries());
3842 CHECK_EQ(map.GetPC(map_index), i);
3843 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3844 map_index++;
3845 RegisterLine* line = reg_table_.GetLine(i);
3846 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003847 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 CHECK_LT(j / 8, map.RegWidth());
3849 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3850 } else if ((j / 8) < map.RegWidth()) {
3851 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3852 } else {
3853 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3854 }
3855 }
3856 } else {
3857 CHECK(reg_bitmap == NULL);
3858 }
3859 }
3860}
jeffhaoa0a764a2011-09-16 10:43:38 -07003861
Ian Rogersd81871c2011-10-03 13:57:23 -07003862const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3863 size_t num_entries = NumEntries();
3864 // Do linear or binary search?
3865 static const size_t kSearchThreshold = 8;
3866 if (num_entries < kSearchThreshold) {
3867 for (size_t i = 0; i < num_entries; i++) {
3868 if (GetPC(i) == dex_pc) {
3869 return GetBitMap(i);
3870 }
3871 }
3872 } else {
3873 int lo = 0;
3874 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003875 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 int mid = (hi + lo) / 2;
3877 int mid_pc = GetPC(mid);
3878 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003879 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003881 hi = mid - 1;
3882 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003884 }
3885 }
3886 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 if (error_if_not_present) {
3888 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3889 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003890 return NULL;
3891}
3892
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003893DexVerifier::GcMapTable DexVerifier::gc_maps_;
3894
3895void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003896 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3897 if (existing_gc_map != NULL) {
3898 CHECK(*existing_gc_map == gc_map);
3899 delete existing_gc_map;
3900 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003901 gc_maps_[ref] = &gc_map;
3902 CHECK(GetGcMap(ref) != NULL);
3903}
3904
3905const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3906 GcMapTable::const_iterator it = gc_maps_.find(ref);
3907 if (it == gc_maps_.end()) {
3908 return NULL;
3909 }
3910 CHECK(it->second != NULL);
3911 return it->second;
3912}
3913
3914void DexVerifier::DeleteGcMaps() {
3915 STLDeleteValues(&gc_maps_);
3916}
3917
Ian Rogersd81871c2011-10-03 13:57:23 -07003918} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003919} // namespace art