blob: 077bb320d4d542521873c8af06c07df53a9b73dd [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 Rogers5d86e522012-02-01 11:45:24 -0800212 } else if (IsJavaLangObjectArray()) {
Ian Rogers89310de2012-02-01 13:47:30 -0800213 return src.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
Ian Rogers9074b992011-10-26 17:41:55 -0700214 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700215 GetClass()->IsAssignableFrom(src.GetClass())) {
216 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700217 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700218 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700219 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700220 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700221 }
222 }
223}
224
Ian Rogers84fa0742011-10-25 18:13:30 -0700225static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
226 return a.IsConstant() ? b : a;
227}
jeffhaobdb76512011-09-07 11:43:16 -0700228
Ian Rogersd81871c2011-10-03 13:57:23 -0700229const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
230 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700231 if (IsUnknown() && incoming_type.IsUnknown()) {
232 return *this; // Unknown MERGE Unknown => Unknown
233 } else if (IsConflict()) {
234 return *this; // Conflict MERGE * => Conflict
235 } else if (incoming_type.IsConflict()) {
236 return incoming_type; // * MERGE Conflict => Conflict
237 } else if (IsUnknown() || incoming_type.IsUnknown()) {
238 return reg_types->Conflict(); // Unknown MERGE * => Conflict
239 } else if(IsConstant() && incoming_type.IsConstant()) {
240 int32_t val1 = ConstantValue();
241 int32_t val2 = incoming_type.ConstantValue();
242 if (val1 >= 0 && val2 >= 0) {
243 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
244 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700245 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700246 } else {
247 return incoming_type;
248 }
249 } else if (val1 < 0 && val2 < 0) {
250 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
251 if (val1 <= val2) {
252 return *this;
253 } else {
254 return incoming_type;
255 }
256 } else {
257 // Values are +ve and -ve, choose smallest signed type in which they both fit
258 if (IsConstantByte()) {
259 if (incoming_type.IsConstantByte()) {
260 return reg_types->ByteConstant();
261 } else if (incoming_type.IsConstantShort()) {
262 return reg_types->ShortConstant();
263 } else {
264 return reg_types->IntConstant();
265 }
266 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700267 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700268 return reg_types->ShortConstant();
269 } else {
270 return reg_types->IntConstant();
271 }
272 } else {
273 return reg_types->IntConstant();
274 }
275 }
276 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
277 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
278 return reg_types->Boolean(); // boolean MERGE boolean => boolean
279 }
280 if (IsByteTypes() && incoming_type.IsByteTypes()) {
281 return reg_types->Byte(); // byte MERGE byte => byte
282 }
283 if (IsShortTypes() && incoming_type.IsShortTypes()) {
284 return reg_types->Short(); // short MERGE short => short
285 }
286 if (IsCharTypes() && incoming_type.IsCharTypes()) {
287 return reg_types->Char(); // char MERGE char => char
288 }
289 return reg_types->Integer(); // int MERGE * => int
290 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
291 (IsLongTypes() && incoming_type.IsLongTypes()) ||
292 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
293 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
294 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
295 // check constant case was handled prior to entry
296 DCHECK(!IsConstant() || !incoming_type.IsConstant());
297 // float/long/double MERGE float/long/double_constant => float/long/double
298 return SelectNonConstant(*this, incoming_type);
299 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700300 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700301 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700302 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
303 return reg_types->JavaLangObject(); // Object MERGE ref => Object
304 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
305 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
306 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
307 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700308 return reg_types->Conflict();
309 } else { // Two reference types, compute Join
310 Class* c1 = GetClass();
311 Class* c2 = incoming_type.GetClass();
312 DCHECK(c1 != NULL && !c1->IsPrimitive());
313 DCHECK(c2 != NULL && !c2->IsPrimitive());
314 Class* join_class = ClassJoin(c1, c2);
315 if (c1 == join_class) {
316 return *this;
317 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 return incoming_type;
319 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700320 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700321 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700323 } else {
324 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 }
326}
327
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700328static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700330 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
331 case Primitive::kPrimByte: return RegType::kRegTypeByte;
332 case Primitive::kPrimShort: return RegType::kRegTypeShort;
333 case Primitive::kPrimChar: return RegType::kRegTypeChar;
334 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
335 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
336 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
337 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
338 case Primitive::kPrimVoid:
339 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700340 }
341}
342
343static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
344 if (descriptor.length() == 1) {
345 switch (descriptor[0]) {
346 case 'Z': return RegType::kRegTypeBoolean;
347 case 'B': return RegType::kRegTypeByte;
348 case 'S': return RegType::kRegTypeShort;
349 case 'C': return RegType::kRegTypeChar;
350 case 'I': return RegType::kRegTypeInteger;
351 case 'J': return RegType::kRegTypeLongLo;
352 case 'F': return RegType::kRegTypeFloat;
353 case 'D': return RegType::kRegTypeDoubleLo;
354 case 'V':
355 default: return RegType::kRegTypeUnknown;
356 }
357 } else if(descriptor[0] == 'L' || descriptor[0] == '[') {
358 return RegType::kRegTypeReference;
359 } else {
360 return RegType::kRegTypeUnknown;
361 }
362}
363
364std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700365 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700366 return os;
367}
368
369const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800370 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
372}
373
374const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800375 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700376 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 // entries should be sized greater than primitive types
378 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
379 RegType* entry = entries_[type];
380 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700381 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800382 if (strlen(descriptor) != 0) {
383 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700384 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700385 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700386 entries_[type] = entry;
387 }
388 return *entry;
389 } else {
390 DCHECK (type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700392 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700394 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 if (cur_entry->IsReference()) {
396 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800397 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 return *cur_entry;
399 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700400 } else if (cur_entry->IsUnresolvedReference() &&
401 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700402 return *cur_entry;
403 }
404 }
Ian Rogers672297c2012-01-10 14:50:55 -0800405 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700406 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700407 // Able to resolve so create resolved register type
408 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700409 entries_.push_back(entry);
410 return *entry;
411 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700412 // TODO: we assume unresolved, but we may be able to do better by validating whether the
413 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700414 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700415 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700416 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800417 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700418 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800419 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700420 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
421 entries_.size());
422 entries_.push_back(entry);
423 return *entry;
424 } else {
425 // The descriptor is broken return the unknown type as there's nothing sensible that
426 // could be done at runtime
427 return Unknown();
428 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700429 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 }
431}
432
433const RegType& RegTypeCache::FromClass(Class* klass) {
434 if (klass->IsPrimitive()) {
435 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
436 // entries should be sized greater than primitive types
437 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
438 RegType* entry = entries_[type];
439 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700440 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700441 entries_[type] = entry;
442 }
443 return *entry;
444 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700445 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700446 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700447 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700448 return *cur_entry;
449 }
450 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700451 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 entries_.push_back(entry);
453 return *entry;
454 }
455}
456
Ian Rogers28ad40d2011-10-27 15:19:26 -0700457const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
458 RegType* entry;
459 if (type.IsUnresolvedTypes()) {
460 String* descriptor = type.GetDescriptor();
461 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
462 RegType* cur_entry = entries_[i];
463 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
464 cur_entry->GetAllocationPc() == allocation_pc &&
465 cur_entry->GetDescriptor() == descriptor) {
466 return *cur_entry;
467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700469 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
470 descriptor, allocation_pc, entries_.size());
471 } else {
472 Class* klass = type.GetClass();
473 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
474 RegType* cur_entry = entries_[i];
475 if (cur_entry->IsUninitializedReference() &&
476 cur_entry->GetAllocationPc() == allocation_pc &&
477 cur_entry->GetClass() == klass) {
478 return *cur_entry;
479 }
480 }
481 entry = new RegType(RegType::kRegTypeUninitializedReference,
482 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700484 entries_.push_back(entry);
485 return *entry;
486}
487
488const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
489 RegType* entry;
490 if (uninit_type.IsUnresolvedTypes()) {
491 String* descriptor = uninit_type.GetDescriptor();
492 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
493 RegType* cur_entry = entries_[i];
494 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
495 return *cur_entry;
496 }
497 }
498 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
499 } else {
500 Class* klass = uninit_type.GetClass();
501 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
502 RegType* cur_entry = entries_[i];
503 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
504 return *cur_entry;
505 }
506 }
507 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
508 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700509 entries_.push_back(entry);
510 return *entry;
511}
512
513const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700514 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 RegType* cur_entry = entries_[i];
516 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
517 return *cur_entry;
518 }
519 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700520 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 entries_.size());
522 entries_.push_back(entry);
523 return *entry;
524}
525
526const RegType& RegTypeCache::FromType(RegType::Type type) {
527 CHECK(type < RegType::kRegTypeReference);
528 switch (type) {
529 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
530 case RegType::kRegTypeByte: return From(type, NULL, "B");
531 case RegType::kRegTypeShort: return From(type, NULL, "S");
532 case RegType::kRegTypeChar: return From(type, NULL, "C");
533 case RegType::kRegTypeInteger: return From(type, NULL, "I");
534 case RegType::kRegTypeFloat: return From(type, NULL, "F");
535 case RegType::kRegTypeLongLo:
536 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
537 case RegType::kRegTypeDoubleLo:
538 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
539 default: return From(type, NULL, "");
540 }
541}
542
543const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700544 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
545 RegType* cur_entry = entries_[i];
546 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
547 return *cur_entry;
548 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700550 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
551 entries_.push_back(entry);
552 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700553}
554
Ian Rogers28ad40d2011-10-27 15:19:26 -0700555const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
Ian Rogers89310de2012-02-01 13:47:30 -0800556 CHECK(array.IsArrayTypes());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700557 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800558 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
559 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800560 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700561 } else {
562 return FromClass(array.GetClass()->GetComponentType());
563 }
564}
565
566
Ian Rogersd81871c2011-10-03 13:57:23 -0700567bool RegisterLine::CheckConstructorReturn() const {
568 for (size_t i = 0; i < num_regs_; i++) {
569 if (GetRegisterType(i).IsUninitializedThisReference()) {
570 verifier_->Fail(VERIFY_ERROR_GENERIC)
571 << "Constructor returning without calling superclass constructor";
572 return false;
573 }
574 }
575 return true;
576}
577
jeffhao60f83e32012-02-13 17:16:30 -0800578bool RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700579 DCHECK(vdst < num_regs_);
580 if (new_type.IsLowHalf()) {
581 line_[vdst] = new_type.GetId();
582 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
583 } else if (new_type.IsHighHalf()) {
584 /* should never set these explicitly */
585 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Explicit set of high register type";
jeffhao60f83e32012-02-13 17:16:30 -0800586 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 } else if (new_type.IsConflict()) { // should only be set during a merge
588 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
jeffhao60f83e32012-02-13 17:16:30 -0800589 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 } else {
591 line_[vdst] = new_type.GetId();
592 }
593 // Clear the monitor entry bits for this register.
594 ClearAllRegToLockDepths(vdst);
jeffhao60f83e32012-02-13 17:16:30 -0800595 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700596}
597
598void RegisterLine::SetResultTypeToUnknown() {
599 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
600 result_[0] = unknown_id;
601 result_[1] = unknown_id;
602}
603
604void RegisterLine::SetResultRegisterType(const RegType& new_type) {
605 result_[0] = new_type.GetId();
606 if(new_type.IsLowHalf()) {
607 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
608 result_[1] = new_type.GetId() + 1;
609 } else {
610 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
611 }
612}
613
614const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
615 // The register index was validated during the static pass, so we don't need to check it here.
616 DCHECK_LT(vsrc, num_regs_);
617 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
618}
619
620const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
621 if (dec_insn.vA_ < 1) {
622 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
623 return verifier_->GetRegTypeCache()->Unknown();
624 }
625 /* get the element type of the array held in vsrc */
626 const RegType& this_type = GetRegisterType(dec_insn.vC_);
627 if (!this_type.IsReferenceTypes()) {
628 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
629 << dec_insn.vC_ << " (type=" << this_type << ")";
630 return verifier_->GetRegTypeCache()->Unknown();
631 }
632 return this_type;
633}
634
Ian Rogersd81871c2011-10-03 13:57:23 -0700635bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
636 // Verify the src register type against the check type refining the type of the register
637 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700638 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
640 << " but expected " << check_type;
641 return false;
642 }
jeffhao457cc512012-02-02 16:55:13 -0800643 if (check_type.IsLowHalf()) {
644 const RegType& src_type_h = GetRegisterType(vsrc + 1);
645 if (!src_type.CheckWidePair(src_type_h)) {
646 verifier_->Fail(VERIFY_ERROR_GENERIC) << "wide register v" << vsrc << " has type "
647 << src_type << "/" << src_type_h;
648 return false;
649 }
650 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
652 // precise than the subtype in vsrc so leave it for reference types. For primitive types
653 // if they are a defined type then they are as precise as we can get, however, for constant
654 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
655 return true;
656}
657
658void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700659 DCHECK(uninit_type.IsUninitializedTypes());
660 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
661 size_t changed = 0;
662 for (size_t i = 0; i < num_regs_; i++) {
663 if (GetRegisterType(i).Equals(uninit_type)) {
664 line_[i] = init_type.GetId();
665 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700668 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669}
670
671void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
672 for (size_t i = 0; i < num_regs_; i++) {
673 if (GetRegisterType(i).Equals(uninit_type)) {
674 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
675 ClearAllRegToLockDepths(i);
676 }
677 }
678}
679
680void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
681 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
682 const RegType& type = GetRegisterType(vsrc);
jeffhao60f83e32012-02-13 17:16:30 -0800683 if (!SetRegisterType(vdst, type)) {
684 return;
685 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
687 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
688 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
689 << " cat=" << static_cast<int>(cat);
690 } else if (cat == kTypeCategoryRef) {
691 CopyRegToLockDepth(vdst, vsrc);
692 }
693}
694
695void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
696 const RegType& type_l = GetRegisterType(vsrc);
697 const RegType& type_h = GetRegisterType(vsrc + 1);
698
699 if (!type_l.CheckWidePair(type_h)) {
700 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
701 << " type=" << type_l << "/" << type_h;
702 } else {
703 SetRegisterType(vdst, type_l); // implicitly sets the second half
704 }
705}
706
707void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
708 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
709 if ((!is_reference && !type.IsCategory1Types()) ||
710 (is_reference && !type.IsReferenceTypes())) {
711 verifier_->Fail(VERIFY_ERROR_GENERIC)
712 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
713 } else {
714 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
715 SetRegisterType(vdst, type);
716 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
717 }
718}
719
720/*
721 * Implement "move-result-wide". Copy the category-2 value from the result
722 * register to another register, and reset the result register.
723 */
724void RegisterLine::CopyResultRegister2(uint32_t vdst) {
725 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
726 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
727 if (!type_l.IsCategory2Types()) {
728 verifier_->Fail(VERIFY_ERROR_GENERIC)
729 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
730 } else {
731 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
732 SetRegisterType(vdst, type_l); // also sets the high
733 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
734 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
735 }
736}
737
738void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
739 const RegType& dst_type, const RegType& src_type) {
740 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
741 SetRegisterType(dec_insn.vA_, dst_type);
742 }
743}
744
745void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
746 const RegType& dst_type,
747 const RegType& src_type1, const RegType& src_type2,
748 bool check_boolean_op) {
749 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
750 VerifyRegisterType(dec_insn.vC_, src_type2)) {
751 if (check_boolean_op) {
752 DCHECK(dst_type.IsInteger());
753 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
754 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
755 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
756 return;
757 }
758 }
759 SetRegisterType(dec_insn.vA_, dst_type);
760 }
761}
762
763void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
764 const RegType& dst_type, const RegType& src_type1,
765 const RegType& src_type2, bool check_boolean_op) {
766 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
767 VerifyRegisterType(dec_insn.vB_, src_type2)) {
768 if (check_boolean_op) {
769 DCHECK(dst_type.IsInteger());
770 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
771 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
772 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
773 return;
774 }
775 }
776 SetRegisterType(dec_insn.vA_, dst_type);
777 }
778}
779
780void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
781 const RegType& dst_type, const RegType& src_type,
782 bool check_boolean_op) {
783 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
784 if (check_boolean_op) {
785 DCHECK(dst_type.IsInteger());
786 /* check vB with the call, then check the constant manually */
787 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
788 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
789 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
790 return;
791 }
792 }
793 SetRegisterType(dec_insn.vA_, dst_type);
794 }
795}
796
797void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
798 const RegType& reg_type = GetRegisterType(reg_idx);
799 if (!reg_type.IsReferenceTypes()) {
800 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800801 } else if (monitors_.size() >= 32) {
802 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 } else {
804 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700805 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 }
807}
808
809void RegisterLine::PopMonitor(uint32_t reg_idx) {
810 const RegType& reg_type = GetRegisterType(reg_idx);
811 if (!reg_type.IsReferenceTypes()) {
812 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
813 } else if (monitors_.empty()) {
814 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
815 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700816 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
818 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
819 // format "036" the constant collector may create unlocks on the same object but referenced
820 // via different registers.
821 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
822 : verifier_->LogVerifyInfo())
823 << "monitor-exit not unlocking the top of the monitor stack";
824 } else {
825 // Record the register was unlocked
826 ClearRegToLockDepth(reg_idx, monitors_.size());
827 }
828 }
829}
830
831bool RegisterLine::VerifyMonitorStackEmpty() {
832 if (MonitorStackDepth() != 0) {
833 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
834 return false;
835 } else {
836 return true;
837 }
838}
839
840bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
841 bool changed = false;
842 for (size_t idx = 0; idx < num_regs_; idx++) {
843 if (line_[idx] != incoming_line->line_[idx]) {
844 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
845 const RegType& cur_type = GetRegisterType(idx);
846 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
847 changed = changed || !cur_type.Equals(new_type);
848 line_[idx] = new_type.GetId();
849 }
850 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700851 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
853 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
854 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
855 for (uint32_t idx = 0; idx < num_regs_; idx++) {
856 size_t depths = reg_to_lock_depths_.count(idx);
857 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
858 if (depths != incoming_depths) {
859 if (depths == 0 || incoming_depths == 0) {
860 reg_to_lock_depths_.erase(idx);
861 } else {
862 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
863 << ": " << depths << " != " << incoming_depths;
864 break;
865 }
866 }
867 }
868 }
869 return changed;
870}
871
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800872void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 for (size_t i = 0; i < num_regs_; i += 8) {
874 uint8_t val = 0;
875 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
876 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700877 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 val |= 1 << j;
879 }
880 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800881 if ((i / 8) >= max_bytes) {
882 DCHECK_EQ(0, val);
883 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800885 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
886 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 }
888}
889
890std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700891 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 return os;
893}
894
895
896void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
897 uint32_t insns_size, uint16_t registers_size,
898 DexVerifier* verifier) {
899 DCHECK_GT(insns_size, 0U);
900
901 for (uint32_t i = 0; i < insns_size; i++) {
902 bool interesting = false;
903 switch (mode) {
904 case kTrackRegsAll:
905 interesting = flags[i].IsOpcode();
906 break;
907 case kTrackRegsGcPoints:
908 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
909 break;
910 case kTrackRegsBranches:
911 interesting = flags[i].IsBranchTarget();
912 break;
913 default:
914 break;
915 }
916 if (interesting) {
917 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
918 }
919 }
920}
921
Ian Rogers1c5eb702012-02-01 09:18:34 -0800922bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700923 if (klass->IsVerified()) {
924 return true;
925 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800927 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800928 error = "Verifier rejected class ";
929 error += PrettyDescriptor(klass);
930 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 return false;
932 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800933 if (super != NULL && super->IsFinal()) {
934 error = "Verifier rejected class ";
935 error += PrettyDescriptor(klass);
936 error += " that attempts to sub-class final class ";
937 error += PrettyDescriptor(super);
938 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 }
jeffhaobdb76512011-09-07 11:43:16 -0700940 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
941 Method* method = klass->GetDirectMethod(i);
942 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800943 error = "Verifier rejected class ";
944 error += PrettyDescriptor(klass);
945 error += " due to bad method ";
946 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700947 return false;
948 }
949 }
950 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
951 Method* method = klass->GetVirtualMethod(i);
952 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800953 error = "Verifier rejected class ";
954 error += PrettyDescriptor(klass);
955 error += " due to bad method ";
956 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700957 return false;
958 }
959 }
960 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700961}
962
jeffhaobdb76512011-09-07 11:43:16 -0700963bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 DexVerifier verifier(method);
965 bool success = verifier.Verify();
Brian Carlstrom75412882012-01-18 01:26:54 -0800966 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
967
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 // We expect either success and no verification error, or failure and a generic failure to
969 // reject the class.
970 if (success) {
971 if (verifier.failure_ != VERIFY_ERROR_NONE) {
972 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
973 << verifier.fail_messages_;
974 }
975 } else {
976 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700977 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700978 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700979 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700980 verifier.Dump(std::cout);
981 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
983 }
984 return success;
985}
986
Shih-wei Liao371814f2011-10-27 16:52:10 -0700987void DexVerifier::VerifyMethodAndDump(Method* method) {
988 DexVerifier verifier(method);
989 verifier.Verify();
990
Elliott Hughese0918552011-10-28 17:18:29 -0700991 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
992 << verifier.fail_messages_.str() << std::endl
993 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700994}
995
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800996DexVerifier::DexVerifier(Method* method)
997 : work_insn_idx_(-1),
998 method_(method),
999 failure_(VERIFY_ERROR_NONE),
1000 new_instance_count_(0),
1001 monitor_enter_count_(0) {
1002 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001003 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001004 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 dex_file_ = &class_linker->FindDexFile(dex_cache);
1006 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001007}
1008
Ian Rogersd81871c2011-10-03 13:57:23 -07001009bool DexVerifier::Verify() {
1010 // If there aren't any instructions, make sure that's expected, then exit successfully.
1011 if (code_item_ == NULL) {
1012 if (!method_->IsNative() && !method_->IsAbstract()) {
1013 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001014 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 } else {
1016 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001017 }
jeffhaobdb76512011-09-07 11:43:16 -07001018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1020 if (code_item_->ins_size_ > code_item_->registers_size_) {
1021 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1022 << " regs=" << code_item_->registers_size_;
1023 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001024 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 // Allocate and initialize an array to hold instruction data.
1026 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1027 // Run through the instructions and see if the width checks out.
1028 bool result = ComputeWidthsAndCountOps();
1029 // Flag instructions guarded by a "try" block and check exception handlers.
1030 result = result && ScanTryCatchBlocks();
1031 // Perform static instruction verification.
1032 result = result && VerifyInstructions();
1033 // Perform code flow analysis.
1034 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001035 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001036}
1037
Ian Rogers47a05882012-02-03 12:23:33 -08001038std::ostream& DexVerifier::Fail(VerifyError error) {
1039 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers9ada79c2012-02-03 14:29:52 -08001040 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
1041 // errors into generic errors so that we re-verify at runtime. We may fail to find or to agree
1042 // on access because of not yet available class loaders, or class loaders that will differ at
1043 // runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001044 if (Runtime::Current()->IsCompiler()) {
1045 switch(error) {
1046 case VERIFY_ERROR_NO_CLASS:
1047 case VERIFY_ERROR_NO_FIELD:
1048 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -08001049 case VERIFY_ERROR_ACCESS_CLASS:
1050 case VERIFY_ERROR_ACCESS_FIELD:
1051 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers47a05882012-02-03 12:23:33 -08001052 error = VERIFY_ERROR_GENERIC;
1053 break;
1054 default:
1055 break;
1056 }
1057 }
1058 failure_ = error;
1059 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1060 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1061}
1062
Ian Rogersd81871c2011-10-03 13:57:23 -07001063bool DexVerifier::ComputeWidthsAndCountOps() {
1064 const uint16_t* insns = code_item_->insns_;
1065 size_t insns_size = code_item_->insns_size_in_code_units_;
1066 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001067 size_t new_instance_count = 0;
1068 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001072 Instruction::Code opcode = inst->Opcode();
1073 if (opcode == Instruction::NEW_INSTANCE) {
1074 new_instance_count++;
1075 } else if (opcode == Instruction::MONITOR_ENTER) {
1076 monitor_enter_count++;
1077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 size_t inst_size = inst->SizeInCodeUnits();
1079 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1080 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001081 inst = inst->Next();
1082 }
1083
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 if (dex_pc != insns_size) {
1085 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1086 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001087 return false;
1088 }
1089
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 new_instance_count_ = new_instance_count;
1091 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001092 return true;
1093}
1094
Ian Rogersd81871c2011-10-03 13:57:23 -07001095bool DexVerifier::ScanTryCatchBlocks() {
1096 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001097 if (tries_size == 0) {
1098 return true;
1099 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001101 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001102
1103 for (uint32_t idx = 0; idx < tries_size; idx++) {
1104 const DexFile::TryItem* try_item = &tries[idx];
1105 uint32_t start = try_item->start_addr_;
1106 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001107 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1109 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001110 return false;
1111 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 if (!insn_flags_[start].IsOpcode()) {
1113 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001114 return false;
1115 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 for (uint32_t dex_pc = start; dex_pc < end;
1117 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1118 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001119 }
1120 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001121 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001122 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001123 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001124 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001125 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001126 CatchHandlerIterator iterator(handlers_ptr);
1127 for (; iterator.HasNext(); iterator.Next()) {
1128 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 if (!insn_flags_[dex_pc].IsOpcode()) {
1130 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001131 return false;
1132 }
jeffhao60f83e32012-02-13 17:16:30 -08001133 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1134 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
1135 Fail(VERIFY_ERROR_GENERIC) << "exception handler doesn't start with move-exception ("
1136 << dex_pc << ")";
1137 return false;
1138 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001140 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1141 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001142 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1143 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001144 if (exception_type == NULL) {
1145 DCHECK(Thread::Current()->IsExceptionPending());
1146 Thread::Current()->ClearException();
1147 }
1148 }
jeffhaobdb76512011-09-07 11:43:16 -07001149 }
Ian Rogers0571d352011-11-03 19:51:38 -07001150 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001151 }
jeffhaobdb76512011-09-07 11:43:16 -07001152 return true;
1153}
1154
Ian Rogersd81871c2011-10-03 13:57:23 -07001155bool DexVerifier::VerifyInstructions() {
1156 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001157
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 /* Flag the start of the method as a branch target. */
1159 insn_flags_[0].SetBranchTarget();
1160
1161 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1162 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1163 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001164 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1165 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 return false;
1167 }
1168 /* Flag instructions that are garbage collection points */
1169 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1170 insn_flags_[dex_pc].SetGcPoint();
1171 }
1172 dex_pc += inst->SizeInCodeUnits();
1173 inst = inst->Next();
1174 }
1175 return true;
1176}
1177
1178bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1179 Instruction::DecodedInstruction dec_insn(inst);
1180 bool result = true;
1181 switch (inst->GetVerifyTypeArgumentA()) {
1182 case Instruction::kVerifyRegA:
1183 result = result && CheckRegisterIndex(dec_insn.vA_);
1184 break;
1185 case Instruction::kVerifyRegAWide:
1186 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1187 break;
1188 }
1189 switch (inst->GetVerifyTypeArgumentB()) {
1190 case Instruction::kVerifyRegB:
1191 result = result && CheckRegisterIndex(dec_insn.vB_);
1192 break;
1193 case Instruction::kVerifyRegBField:
1194 result = result && CheckFieldIndex(dec_insn.vB_);
1195 break;
1196 case Instruction::kVerifyRegBMethod:
1197 result = result && CheckMethodIndex(dec_insn.vB_);
1198 break;
1199 case Instruction::kVerifyRegBNewInstance:
1200 result = result && CheckNewInstance(dec_insn.vB_);
1201 break;
1202 case Instruction::kVerifyRegBString:
1203 result = result && CheckStringIndex(dec_insn.vB_);
1204 break;
1205 case Instruction::kVerifyRegBType:
1206 result = result && CheckTypeIndex(dec_insn.vB_);
1207 break;
1208 case Instruction::kVerifyRegBWide:
1209 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1210 break;
1211 }
1212 switch (inst->GetVerifyTypeArgumentC()) {
1213 case Instruction::kVerifyRegC:
1214 result = result && CheckRegisterIndex(dec_insn.vC_);
1215 break;
1216 case Instruction::kVerifyRegCField:
1217 result = result && CheckFieldIndex(dec_insn.vC_);
1218 break;
1219 case Instruction::kVerifyRegCNewArray:
1220 result = result && CheckNewArray(dec_insn.vC_);
1221 break;
1222 case Instruction::kVerifyRegCType:
1223 result = result && CheckTypeIndex(dec_insn.vC_);
1224 break;
1225 case Instruction::kVerifyRegCWide:
1226 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1227 break;
1228 }
1229 switch (inst->GetVerifyExtraFlags()) {
1230 case Instruction::kVerifyArrayData:
1231 result = result && CheckArrayData(code_offset);
1232 break;
1233 case Instruction::kVerifyBranchTarget:
1234 result = result && CheckBranchTarget(code_offset);
1235 break;
1236 case Instruction::kVerifySwitchTargets:
1237 result = result && CheckSwitchTargets(code_offset);
1238 break;
1239 case Instruction::kVerifyVarArg:
1240 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1241 break;
1242 case Instruction::kVerifyVarArgRange:
1243 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1244 break;
1245 case Instruction::kVerifyError:
1246 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1247 result = false;
1248 break;
1249 }
1250 return result;
1251}
1252
1253bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1254 if (idx >= code_item_->registers_size_) {
1255 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1256 << code_item_->registers_size_ << ")";
1257 return false;
1258 }
1259 return true;
1260}
1261
1262bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1263 if (idx + 1 >= code_item_->registers_size_) {
1264 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1265 << "+1 >= " << code_item_->registers_size_ << ")";
1266 return false;
1267 }
1268 return true;
1269}
1270
1271bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1272 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1273 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1274 << dex_file_->GetHeader().field_ids_size_ << ")";
1275 return false;
1276 }
1277 return true;
1278}
1279
1280bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1281 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1282 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1283 << dex_file_->GetHeader().method_ids_size_ << ")";
1284 return false;
1285 }
1286 return true;
1287}
1288
1289bool DexVerifier::CheckNewInstance(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 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001296 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 if (descriptor[0] != 'L') {
1298 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1299 return false;
1300 }
1301 return true;
1302}
1303
1304bool DexVerifier::CheckStringIndex(uint32_t idx) {
1305 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1306 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1307 << dex_file_->GetHeader().string_ids_size_ << ")";
1308 return false;
1309 }
1310 return true;
1311}
1312
1313bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1314 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1315 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1316 << dex_file_->GetHeader().type_ids_size_ << ")";
1317 return false;
1318 }
1319 return true;
1320}
1321
1322bool DexVerifier::CheckNewArray(uint32_t idx) {
1323 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1324 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1325 << dex_file_->GetHeader().type_ids_size_ << ")";
1326 return false;
1327 }
1328 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001329 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 const char* cp = descriptor;
1331 while (*cp++ == '[') {
1332 bracket_count++;
1333 }
1334 if (bracket_count == 0) {
1335 /* The given class must be an array type. */
1336 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1337 return false;
1338 } else if (bracket_count > 255) {
1339 /* It is illegal to create an array of more than 255 dimensions. */
1340 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1341 return false;
1342 }
1343 return true;
1344}
1345
1346bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1347 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1348 const uint16_t* insns = code_item_->insns_ + cur_offset;
1349 const uint16_t* array_data;
1350 int32_t array_data_offset;
1351
1352 DCHECK_LT(cur_offset, insn_count);
1353 /* make sure the start of the array data table is in range */
1354 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1355 if ((int32_t) cur_offset + array_data_offset < 0 ||
1356 cur_offset + array_data_offset + 2 >= insn_count) {
1357 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1358 << ", data offset " << array_data_offset << ", count " << insn_count;
1359 return false;
1360 }
1361 /* offset to array data table is a relative branch-style offset */
1362 array_data = insns + array_data_offset;
1363 /* make sure the table is 32-bit aligned */
1364 if ((((uint32_t) array_data) & 0x03) != 0) {
1365 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1366 << ", data offset " << array_data_offset;
1367 return false;
1368 }
1369 uint32_t value_width = array_data[1];
1370 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1371 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1372 /* make sure the end of the switch is in range */
1373 if (cur_offset + array_data_offset + table_size > insn_count) {
1374 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1375 << ", data offset " << array_data_offset << ", end "
1376 << cur_offset + array_data_offset + table_size
1377 << ", count " << insn_count;
1378 return false;
1379 }
1380 return true;
1381}
1382
1383bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1384 int32_t offset;
1385 bool isConditional, selfOkay;
1386 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1387 return false;
1388 }
1389 if (!selfOkay && offset == 0) {
1390 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1391 return false;
1392 }
1393 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1394 // identical "wrap-around" behavior, but it's unwise to depend on that.
1395 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1396 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1397 return false;
1398 }
1399 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1400 int32_t abs_offset = cur_offset + offset;
1401 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1402 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1403 << (void*) abs_offset << ") at " << (void*) cur_offset;
1404 return false;
1405 }
1406 insn_flags_[abs_offset].SetBranchTarget();
1407 return true;
1408}
1409
1410bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1411 bool* selfOkay) {
1412 const uint16_t* insns = code_item_->insns_ + cur_offset;
1413 *pConditional = false;
1414 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001415 switch (*insns & 0xff) {
1416 case Instruction::GOTO:
1417 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001418 break;
1419 case Instruction::GOTO_32:
1420 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 *selfOkay = true;
1422 break;
1423 case Instruction::GOTO_16:
1424 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001425 break;
1426 case Instruction::IF_EQ:
1427 case Instruction::IF_NE:
1428 case Instruction::IF_LT:
1429 case Instruction::IF_GE:
1430 case Instruction::IF_GT:
1431 case Instruction::IF_LE:
1432 case Instruction::IF_EQZ:
1433 case Instruction::IF_NEZ:
1434 case Instruction::IF_LTZ:
1435 case Instruction::IF_GEZ:
1436 case Instruction::IF_GTZ:
1437 case Instruction::IF_LEZ:
1438 *pOffset = (int16_t) insns[1];
1439 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001440 break;
1441 default:
1442 return false;
1443 break;
1444 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 return true;
1446}
1447
Ian Rogersd81871c2011-10-03 13:57:23 -07001448bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1449 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001450 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1454 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1455 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1456 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 return false;
1458 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 /* make sure the table is 32-bit aligned */
1462 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1464 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 return false;
1466 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 uint32_t switch_count = switch_insns[1];
1468 int32_t keys_offset, targets_offset;
1469 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001470 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1471 /* 0=sig, 1=count, 2/3=firstKey */
1472 targets_offset = 4;
1473 keys_offset = -1;
1474 expected_signature = Instruction::kPackedSwitchSignature;
1475 } else {
1476 /* 0=sig, 1=count, 2..count*2 = keys */
1477 keys_offset = 2;
1478 targets_offset = 2 + 2 * switch_count;
1479 expected_signature = Instruction::kSparseSwitchSignature;
1480 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001481 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001483 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1484 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 return false;
1486 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001487 /* make sure the end of the switch is in range */
1488 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1490 << switch_offset << ", end "
1491 << (cur_offset + switch_offset + table_size)
1492 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 return false;
1494 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001495 /* for a sparse switch, verify the keys are in ascending order */
1496 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1498 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1500 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1501 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1503 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 return false;
1505 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001506 last_key = key;
1507 }
1508 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 for (uint32_t targ = 0; targ < switch_count; targ++) {
1511 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1512 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1513 int32_t abs_offset = cur_offset + offset;
1514 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1515 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1516 << (void*) abs_offset << ") at "
1517 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 return false;
1519 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 insn_flags_[abs_offset].SetBranchTarget();
1521 }
1522 return true;
1523}
1524
1525bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1526 if (vA > 5) {
1527 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1528 return false;
1529 }
1530 uint16_t registers_size = code_item_->registers_size_;
1531 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001532 if (arg[idx] >= registers_size) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
jeffhao457cc512012-02-02 16:55:13 -08001534 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 return false;
1536 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001537 }
1538
1539 return true;
1540}
1541
Ian Rogersd81871c2011-10-03 13:57:23 -07001542bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1543 uint16_t registers_size = code_item_->registers_size_;
1544 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1545 // integer overflow when adding them here.
1546 if (vA + vC > registers_size) {
1547 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1548 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001549 return false;
1550 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001551 return true;
1552}
1553
Brian Carlstrom75412882012-01-18 01:26:54 -08001554const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1555 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1556 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1557 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1558 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1559 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1560 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1561 gc_map.begin(),
1562 gc_map.end());
1563 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1564 DCHECK_EQ(gc_map.size(),
1565 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1566 (length_prefixed_gc_map->at(1) << 16) |
1567 (length_prefixed_gc_map->at(2) << 8) |
1568 (length_prefixed_gc_map->at(3) << 0)));
1569 return length_prefixed_gc_map;
1570}
1571
Ian Rogersd81871c2011-10-03 13:57:23 -07001572bool DexVerifier::VerifyCodeFlow() {
1573 uint16_t registers_size = code_item_->registers_size_;
1574 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001575
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 if (registers_size * insns_size > 4*1024*1024) {
1577 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1578 << " insns_size=" << insns_size << ")";
1579 }
1580 /* Create and initialize table holding register status */
1581 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1582 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001583
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 work_line_.reset(new RegisterLine(registers_size, this));
1585 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001586
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 /* Initialize register types of method arguments. */
1588 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001589 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1590 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 return false;
1592 }
1593 /* Perform code flow verification. */
1594 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001595 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001597 }
1598
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001600 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1601 if (map.get() == NULL) {
1602 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 return false; // Not a real failure, but a failure to encode
1604 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001605#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001606 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001607#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001608 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1609 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1610 verifier::DexVerifier::SetGcMap(ref, *gc_map);
1611 method_->SetGcMap(&gc_map->at(0));
jeffhaobdb76512011-09-07 11:43:16 -07001612 return true;
1613}
1614
Ian Rogersd81871c2011-10-03 13:57:23 -07001615void DexVerifier::Dump(std::ostream& os) {
1616 if (method_->IsNative()) {
1617 os << "Native method" << std::endl;
1618 return;
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 DCHECK(code_item_ != NULL);
1621 const Instruction* inst = Instruction::At(code_item_->insns_);
1622 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1623 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001624 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001625 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1627 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001628 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001629 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001630 inst = inst->Next();
1631 }
jeffhaobdb76512011-09-07 11:43:16 -07001632}
1633
Ian Rogersd81871c2011-10-03 13:57:23 -07001634static bool IsPrimitiveDescriptor(char descriptor) {
1635 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001636 case 'I':
1637 case 'C':
1638 case 'S':
1639 case 'B':
1640 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001641 case 'F':
1642 case 'D':
1643 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001645 default:
1646 return false;
1647 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001648}
1649
Ian Rogersd81871c2011-10-03 13:57:23 -07001650bool DexVerifier::SetTypesFromSignature() {
1651 RegisterLine* reg_line = reg_table_.GetLine(0);
1652 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1653 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001654
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1656 //Include the "this" pointer.
1657 size_t cur_arg = 0;
1658 if (!method_->IsStatic()) {
1659 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1660 // argument as uninitialized. This restricts field access until the superclass constructor is
1661 // called.
1662 Class* declaring_class = method_->GetDeclaringClass();
1663 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1664 reg_line->SetRegisterType(arg_start + cur_arg,
1665 reg_types_.UninitializedThisArgument(declaring_class));
1666 } else {
1667 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001668 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001670 }
1671
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001672 const DexFile::ProtoId& proto_id =
1673 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001674 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001675
1676 for (; iterator.HasNext(); iterator.Next()) {
1677 const char* descriptor = iterator.GetDescriptor();
1678 if (descriptor == NULL) {
1679 LOG(FATAL) << "Null descriptor";
1680 }
1681 if (cur_arg >= expected_args) {
1682 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1683 << " args, found more (" << descriptor << ")";
1684 return false;
1685 }
1686 switch (descriptor[0]) {
1687 case 'L':
1688 case '[':
1689 // We assume that reference arguments are initialized. The only way it could be otherwise
1690 // (assuming the caller was verified) is if the current method is <init>, but in that case
1691 // it's effectively considered initialized the instant we reach here (in the sense that we
1692 // can return without doing anything or call virtual methods).
1693 {
1694 const RegType& reg_type =
1695 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001696 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 }
1698 break;
1699 case 'Z':
1700 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1701 break;
1702 case 'C':
1703 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1704 break;
1705 case 'B':
1706 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1707 break;
1708 case 'I':
1709 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1710 break;
1711 case 'S':
1712 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1713 break;
1714 case 'F':
1715 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1716 break;
1717 case 'J':
1718 case 'D': {
1719 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1720 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1721 cur_arg++;
1722 break;
1723 }
1724 default:
1725 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1726 return false;
1727 }
1728 cur_arg++;
1729 }
1730 if (cur_arg != expected_args) {
1731 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1732 return false;
1733 }
1734 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1735 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1736 // format. Only major difference from the method argument format is that 'V' is supported.
1737 bool result;
1738 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1739 result = descriptor[1] == '\0';
1740 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1741 size_t i = 0;
1742 do {
1743 i++;
1744 } while (descriptor[i] == '['); // process leading [
1745 if (descriptor[i] == 'L') { // object array
1746 do {
1747 i++; // find closing ;
1748 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1749 result = descriptor[i] == ';';
1750 } else { // primitive array
1751 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1752 }
1753 } else if (descriptor[0] == 'L') {
1754 // could be more thorough here, but shouldn't be required
1755 size_t i = 0;
1756 do {
1757 i++;
1758 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1759 result = descriptor[i] == ';';
1760 } else {
1761 result = false;
1762 }
1763 if (!result) {
1764 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1765 << descriptor << "'";
1766 }
1767 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001768}
1769
Ian Rogersd81871c2011-10-03 13:57:23 -07001770bool DexVerifier::CodeFlowVerifyMethod() {
1771 const uint16_t* insns = code_item_->insns_;
1772 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001773
jeffhaobdb76512011-09-07 11:43:16 -07001774 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 insn_flags_[0].SetChanged();
1776 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001777
jeffhaobdb76512011-09-07 11:43:16 -07001778 /* Continue until no instructions are marked "changed". */
1779 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1781 uint32_t insn_idx = start_guess;
1782 for (; insn_idx < insns_size; insn_idx++) {
1783 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 }
jeffhaobdb76512011-09-07 11:43:16 -07001786 if (insn_idx == insns_size) {
1787 if (start_guess != 0) {
1788 /* try again, starting from the top */
1789 start_guess = 0;
1790 continue;
1791 } else {
1792 /* all flags are clear */
1793 break;
1794 }
1795 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 // We carry the working set of registers from instruction to instruction. If this address can
1797 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1798 // "changed" flags, we need to load the set of registers from the table.
1799 // Because we always prefer to continue on to the next instruction, we should never have a
1800 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1801 // target.
1802 work_insn_idx_ = insn_idx;
1803 if (insn_flags_[insn_idx].IsBranchTarget()) {
1804 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001805 } else {
1806#ifndef NDEBUG
1807 /*
1808 * Sanity check: retrieve the stored register line (assuming
1809 * a full table) and make sure it actually matches.
1810 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1812 if (register_line != NULL) {
1813 if (work_line_->CompareLine(register_line) != 0) {
1814 Dump(std::cout);
1815 std::cout << info_messages_.str();
1816 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1817 << "@" << (void*)work_insn_idx_ << std::endl
1818 << " work_line=" << *work_line_ << std::endl
1819 << " expected=" << *register_line;
1820 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 }
1822#endif
1823 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 if (!CodeFlowVerifyInstruction(&start_guess)) {
1825 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001826 return false;
1827 }
jeffhaobdb76512011-09-07 11:43:16 -07001828 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 insn_flags_[insn_idx].SetVisited();
1830 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001831 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001832
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001834 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001835 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001836 * (besides the wasted space), but it indicates a flaw somewhere
1837 * down the line, possibly in the verifier.
1838 *
1839 * If we've substituted "always throw" instructions into the stream,
1840 * we are almost certainly going to have some dead code.
1841 */
1842 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 uint32_t insn_idx = 0;
1844 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001845 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001846 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001847 * may or may not be preceded by a padding NOP (for alignment).
1848 */
1849 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1850 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1851 insns[insn_idx] == Instruction::kArrayDataSignature ||
1852 (insns[insn_idx] == Instruction::NOP &&
1853 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1854 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1855 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
1858
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001860 if (dead_start < 0)
1861 dead_start = insn_idx;
1862 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001864 dead_start = -1;
1865 }
1866 }
1867 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001869 }
1870 }
jeffhaobdb76512011-09-07 11:43:16 -07001871 return true;
1872}
1873
Ian Rogersd81871c2011-10-03 13:57:23 -07001874bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001875#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001877 gDvm.verifierStats.instrsReexamined++;
1878 } else {
1879 gDvm.verifierStats.instrsExamined++;
1880 }
1881#endif
1882
1883 /*
1884 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001886 * control to another statement:
1887 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001888 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001889 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001890 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001891 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001892 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001893 * throw an exception that is handled by an encompassing "try"
1894 * block.
1895 *
1896 * We can also return, in which case there is no successor instruction
1897 * from this point.
1898 *
1899 * The behavior can be determined from the OpcodeFlags.
1900 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1902 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001903 Instruction::DecodedInstruction dec_insn(inst);
1904 int opcode_flag = inst->Flag();
1905
jeffhaobdb76512011-09-07 11:43:16 -07001906 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001907 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001908 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001910 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1911 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
jeffhaobdb76512011-09-07 11:43:16 -07001913
1914 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001915 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001916 * can throw an exception, we will copy/merge this into the "catch"
1917 * address rather than work_line, because we don't want the result
1918 * from the "successful" code path (e.g. a check-cast that "improves"
1919 * a type) to be visible to the exception handler.
1920 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1922 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001923 } else {
1924#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001926#endif
1927 }
1928
1929 switch (dec_insn.opcode_) {
1930 case Instruction::NOP:
1931 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001932 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001933 * a signature that looks like a NOP; if we see one of these in
1934 * the course of executing code then we have a problem.
1935 */
1936 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001938 }
1939 break;
1940
1941 case Instruction::MOVE:
1942 case Instruction::MOVE_FROM16:
1943 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
1946 case Instruction::MOVE_WIDE:
1947 case Instruction::MOVE_WIDE_FROM16:
1948 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951 case Instruction::MOVE_OBJECT:
1952 case Instruction::MOVE_OBJECT_FROM16:
1953 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
1956
1957 /*
1958 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001959 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001960 * might want to hold the result in an actual CPU register, so the
1961 * Dalvik spec requires that these only appear immediately after an
1962 * invoke or filled-new-array.
1963 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001964 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001965 * redundant with the reset done below, but it can make the debug info
1966 * easier to read in some cases.)
1967 */
1968 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001970 break;
1971 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001976 break;
1977
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001979 /*
jeffhao60f83e32012-02-13 17:16:30 -08001980 * This statement can only appear as the first instruction in an exception handler. We verify
1981 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001982 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001983 const RegType& res_type = GetCaughtExceptionType();
1984 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 }
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1989 if (!GetMethodReturnType().IsUnknown()) {
1990 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 }
1993 break;
1994 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001996 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 const RegType& return_type = GetMethodReturnType();
1998 if (!return_type.IsCategory1Types()) {
1999 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
2000 } else {
2001 // Compilers may generate synthetic functions that write byte values into boolean fields.
2002 // Also, it may use integer values for boolean, byte, short, and character return types.
2003 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
2004 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2005 ((return_type.IsBoolean() || return_type.IsByte() ||
2006 return_type.IsShort() || return_type.IsChar()) &&
2007 src_type.IsInteger()));
2008 /* check the register contents */
2009 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
2010 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002011 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 }
jeffhaobdb76512011-09-07 11:43:16 -07002013 }
2014 }
2015 break;
2016 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002018 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 const RegType& return_type = GetMethodReturnType();
2020 if (!return_type.IsCategory2Types()) {
2021 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
2022 } else {
2023 /* check the register contents */
2024 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
2025 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002026 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 }
jeffhaobdb76512011-09-07 11:43:16 -07002028 }
2029 }
2030 break;
2031 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2033 const RegType& return_type = GetMethodReturnType();
2034 if (!return_type.IsReferenceTypes()) {
2035 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2036 } else {
2037 /* return_type is the *expected* return type, not register value */
2038 DCHECK(!return_type.IsZero());
2039 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002040 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2041 // Disallow returning uninitialized values and verify that the reference in vAA is an
2042 // instance of the "return_type"
2043 if (reg_type.IsUninitializedTypes()) {
2044 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2045 } else if (!return_type.IsAssignableFrom(reg_type)) {
2046 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2047 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002048 }
2049 }
2050 }
2051 break;
2052
2053 case Instruction::CONST_4:
2054 case Instruction::CONST_16:
2055 case Instruction::CONST:
2056 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::CONST_HIGH16:
2060 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 work_line_->SetRegisterType(dec_insn.vA_,
2062 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::CONST_WIDE_16:
2065 case Instruction::CONST_WIDE_32:
2066 case Instruction::CONST_WIDE:
2067 case Instruction::CONST_WIDE_HIGH16:
2068 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::CONST_STRING:
2072 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002076 // Get type from instruction if unresolved then we need an access check
2077 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2078 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2079 // Register holds class, ie its type is class, but on error we keep it Unknown
2080 work_line_->SetRegisterType(dec_insn.vA_,
2081 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 }
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::MONITOR_EXIT:
2088 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002089 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002090 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002091 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002092 * to the need to handle asynchronous exceptions, a now-deprecated
2093 * feature that Dalvik doesn't support.)
2094 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002095 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002096 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002097 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002098 * structured locking checks are working, the former would have
2099 * failed on the -enter instruction, and the latter is impossible.
2100 *
2101 * This is fortunate, because issue 3221411 prevents us from
2102 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002103 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002104 * some catch blocks (which will show up as "dead" code when
2105 * we skip them here); if we can't, then the code path could be
2106 * "live" so we still need to check it.
2107 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 opcode_flag &= ~Instruction::kThrow;
2109 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111
Ian Rogers28ad40d2011-10-27 15:19:26 -07002112 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002114 /*
2115 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2116 * could be a "upcast" -- not expected, so we don't try to address it.)
2117 *
2118 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2119 * dec_insn.vA_ when branching to a handler.
2120 */
2121 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2122 const RegType& res_type =
2123 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002124 if (res_type.IsUnknown()) {
2125 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2126 break; // couldn't resolve class
2127 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002128 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2129 const RegType& orig_type =
2130 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2131 if (!res_type.IsNonZeroReferenceTypes()) {
2132 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2133 } else if (!orig_type.IsReferenceTypes()) {
2134 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002135 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002136 if (is_checkcast) {
2137 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002140 }
jeffhaobdb76512011-09-07 11:43:16 -07002141 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 }
2144 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002145 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2146 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002147 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002148 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 } else {
2150 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2151 }
2152 }
2153 break;
2154 }
2155 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002156 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2157 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2158 // can't create an instance of an interface or abstract class */
2159 if (!res_type.IsInstantiableTypes()) {
2160 Fail(VERIFY_ERROR_INSTANTIATION)
2161 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002163 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2164 // Any registers holding previous allocations from this address that have not yet been
2165 // initialized must be marked invalid.
2166 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2167 // add the new uninitialized reference to the register state
2168 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 }
2170 break;
2171 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002172 case Instruction::NEW_ARRAY:
2173 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002176 VerifyNewArray(dec_insn, true, false);
2177 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002179 case Instruction::FILLED_NEW_ARRAY_RANGE:
2180 VerifyNewArray(dec_insn, true, true);
2181 just_set_result = true; // Filled new array range sets result register
2182 break;
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::CMPL_FLOAT:
2184 case Instruction::CMPG_FLOAT:
jeffhao457cc512012-02-02 16:55:13 -08002185 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float())) {
2186 break;
2187 }
2188 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float())) {
2189 break;
2190 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
2193 case Instruction::CMPL_DOUBLE:
2194 case Instruction::CMPG_DOUBLE:
jeffhao457cc512012-02-02 16:55:13 -08002195 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double())) {
2196 break;
2197 }
2198 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double())) {
2199 break;
2200 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::CMP_LONG:
jeffhao457cc512012-02-02 16:55:13 -08002204 if (!work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long())) {
2205 break;
2206 }
2207 if (!work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long())) {
2208 break;
2209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002213 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2214 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2215 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002216 }
2217 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 }
jeffhaobdb76512011-09-07 11:43:16 -07002219 case Instruction::GOTO:
2220 case Instruction::GOTO_16:
2221 case Instruction::GOTO_32:
2222 /* no effect on or use of registers */
2223 break;
2224
2225 case Instruction::PACKED_SWITCH:
2226 case Instruction::SPARSE_SWITCH:
2227 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 case Instruction::FILL_ARRAY_DATA: {
2232 /* Similar to the verification done for APUT */
Ian Rogers89310de2012-02-01 13:47:30 -08002233 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA_);
2234 /* array_type can be null if the reg type is Zero */
2235 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002236 if (!array_type.IsArrayTypes()) {
2237 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002238 } else {
jeffhao457cc512012-02-02 16:55:13 -08002239 const RegType& component_type = reg_types_.GetComponentType(array_type,
2240 method_->GetDeclaringClass()->GetClassLoader());
2241 DCHECK(!component_type.IsUnknown());
2242 if (component_type.IsNonZeroReferenceTypes()) {
2243 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data with component type "
2244 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 } else {
jeffhao457cc512012-02-02 16:55:13 -08002246 // Now verify if the element width in the table matches the element width declared in
2247 // the array
2248 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2249 if (array_data[0] != Instruction::kArrayDataSignature) {
2250 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2251 } else {
2252 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2253 // Since we don't compress the data in Dex, expect to see equal width of data stored
2254 // in the table and expected from the array class.
2255 if (array_data[1] != elem_width) {
2256 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2257 << " vs " << elem_width << ")";
2258 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 }
2260 }
jeffhaobdb76512011-09-07 11:43:16 -07002261 }
2262 }
2263 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 }
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::IF_NE: {
2267 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2268 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2269 bool mismatch = false;
2270 if (reg_type1.IsZero()) { // zero then integral or reference expected
2271 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2272 } else if (reg_type1.IsReferenceTypes()) { // both references?
2273 mismatch = !reg_type2.IsReferenceTypes();
2274 } else { // both integral?
2275 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2276 }
2277 if (mismatch) {
2278 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2279 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002280 }
2281 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 }
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IF_LT:
2284 case Instruction::IF_GE:
2285 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 case Instruction::IF_LE: {
2287 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2288 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2289 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2290 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2291 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002292 }
2293 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 }
jeffhaobdb76512011-09-07 11:43:16 -07002295 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 case Instruction::IF_NEZ: {
2297 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2298 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2299 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2300 }
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 }
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::IF_LTZ:
2304 case Instruction::IF_GEZ:
2305 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 case Instruction::IF_LEZ: {
2307 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2308 if (!reg_type.IsIntegralTypes()) {
2309 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2310 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2311 }
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2316 break;
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2319 break;
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 VerifyAGet(dec_insn, reg_types_.Char(), true);
2322 break;
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 case Instruction::AGET:
2327 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2328 break;
jeffhaobdb76512011-09-07 11:43:16 -07002329 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 VerifyAGet(dec_insn, reg_types_.Long(), true);
2331 break;
2332 case Instruction::AGET_OBJECT:
2333 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 case Instruction::APUT_BOOLEAN:
2337 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2338 break;
2339 case Instruction::APUT_BYTE:
2340 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2341 break;
2342 case Instruction::APUT_CHAR:
2343 VerifyAPut(dec_insn, reg_types_.Char(), true);
2344 break;
2345 case Instruction::APUT_SHORT:
2346 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357
jeffhaobdb76512011-09-07 11:43:16 -07002358 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002359 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 break;
jeffhaobdb76512011-09-07 11:43:16 -07002361 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002362 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 break;
jeffhaobdb76512011-09-07 11:43:16 -07002364 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002365 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 break;
jeffhaobdb76512011-09-07 11:43:16 -07002367 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002368 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 break;
2370 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002371 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002374 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002377 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 break;
jeffhaobdb76512011-09-07 11:43:16 -07002379
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002381 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 break;
2383 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002384 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 break;
2386 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002387 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
2389 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002390 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002393 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002396 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 break;
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002399 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401
jeffhaobdb76512011-09-07 11:43:16 -07002402 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002403 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 break;
jeffhaobdb76512011-09-07 11:43:16 -07002405 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002406 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 break;
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002409 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 break;
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002412 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 break;
2414 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002415 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002418 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002421 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 break;
2423
2424 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002425 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 break;
2427 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002428 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 break;
2430 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002431 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 break;
2433 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002434 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002437 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002438 break;
2439 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002440 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002443 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445
2446 case Instruction::INVOKE_VIRTUAL:
2447 case Instruction::INVOKE_VIRTUAL_RANGE:
2448 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 case Instruction::INVOKE_SUPER_RANGE: {
2450 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2451 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2452 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2453 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2454 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2455 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002456 const char* descriptor;
2457 if (called_method == NULL) {
2458 uint32_t method_idx = dec_insn.vB_;
2459 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2460 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002461 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002463 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 }
Ian Rogers9074b992011-10-26 17:41:55 -07002465 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002468 just_set_result = true;
2469 }
2470 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 }
jeffhaobdb76512011-09-07 11:43:16 -07002472 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 case Instruction::INVOKE_DIRECT_RANGE: {
2474 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2475 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2476 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002477 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 * Some additional checks when calling a constructor. We know from the invocation arg check
2479 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2480 * that to require that called_method->klass is the same as this->klass or this->super,
2481 * allowing the latter only if the "this" argument is the same as the "this" argument to
2482 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002483 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 bool is_constructor;
2485 if (called_method != NULL) {
2486 is_constructor = called_method->IsConstructor();
2487 } else {
2488 uint32_t method_idx = dec_insn.vB_;
2489 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2490 const char* name = dex_file_->GetMethodName(method_id);
2491 is_constructor = strcmp(name, "<init>") == 0;
2492 }
2493 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2495 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497
2498 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 if (this_type.IsZero()) {
2500 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002503 if (called_method != NULL) {
2504 Class* this_class = this_type.GetClass();
2505 DCHECK(this_class != NULL);
2506 /* must be in same class or in superclass */
2507 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2508 if (this_class != method_->GetDeclaringClass()) {
2509 Fail(VERIFY_ERROR_GENERIC)
2510 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2511 break;
2512 }
2513 } else if (called_method->GetDeclaringClass() != this_class) {
2514 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 }
jeffhaobdb76512011-09-07 11:43:16 -07002517 }
2518
2519 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002520 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2522 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 }
2525
2526 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002527 * Replace the uninitialized reference with an initialized one. We need to do this for all
2528 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002529 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 work_line_->MarkRefsAsInitialized(this_type);
2531 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002533 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002534 const char* descriptor;
2535 if (called_method == NULL) {
2536 uint32_t method_idx = dec_insn.vB_;
2537 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2538 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002539 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002540 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002541 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002542 }
Ian Rogers9074b992011-10-26 17:41:55 -07002543 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002544 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002546 just_set_result = true;
2547 }
2548 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 }
jeffhaobdb76512011-09-07 11:43:16 -07002550 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 case Instruction::INVOKE_STATIC_RANGE: {
2552 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2553 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2554 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002555 const char* descriptor;
2556 if (called_method == NULL) {
2557 uint32_t method_idx = dec_insn.vB_;
2558 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2559 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002560 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002561 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002562 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002563 }
Ian Rogers9074b992011-10-26 17:41:55 -07002564 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002565 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 work_line_->SetResultRegisterType(return_type);
2567 just_set_result = true;
2568 }
jeffhaobdb76512011-09-07 11:43:16 -07002569 }
2570 break;
2571 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 case Instruction::INVOKE_INTERFACE_RANGE: {
2573 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2574 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2575 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002576 if (abs_method != NULL) {
2577 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002578 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2580 << PrettyMethod(abs_method) << "'";
2581 break;
2582 }
2583 }
2584 /* Get the type of the "this" arg, which should either be a sub-interface of called
2585 * interface or Object (see comments in RegType::JoinClass).
2586 */
2587 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2588 if (failure_ == VERIFY_ERROR_NONE) {
2589 if (this_type.IsZero()) {
2590 /* null pointer always passes (and always fails at runtime) */
2591 } else {
2592 if (this_type.IsUninitializedTypes()) {
2593 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2594 << this_type;
2595 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002597 // In the past we have tried to assert that "called_interface" is assignable
2598 // from "this_type.GetClass()", however, as we do an imprecise Join
2599 // (RegType::JoinClass) we don't have full information on what interfaces are
2600 // implemented by "this_type". For example, two classes may implement the same
2601 // interfaces and have a common parent that doesn't implement the interface. The
2602 // join will set "this_type" to the parent class and a test that this implements
2603 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002604 }
2605 }
jeffhaobdb76512011-09-07 11:43:16 -07002606 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 * We don't have an object instance, so we can't find the concrete method. However, all of
2608 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002609 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002610 const char* descriptor;
2611 if (abs_method == NULL) {
2612 uint32_t method_idx = dec_insn.vB_;
2613 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2614 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002615 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002616 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002617 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002618 }
Ian Rogers9074b992011-10-26 17:41:55 -07002619 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002620 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2621 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002623 just_set_result = true;
2624 }
2625 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 }
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::NEG_INT:
2628 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::NEG_LONG:
2632 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686
2687 case Instruction::ADD_INT:
2688 case Instruction::SUB_INT:
2689 case Instruction::MUL_INT:
2690 case Instruction::REM_INT:
2691 case Instruction::DIV_INT:
2692 case Instruction::SHL_INT:
2693 case Instruction::SHR_INT:
2694 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
2697 case Instruction::AND_INT:
2698 case Instruction::OR_INT:
2699 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::ADD_LONG:
2703 case Instruction::SUB_LONG:
2704 case Instruction::MUL_LONG:
2705 case Instruction::DIV_LONG:
2706 case Instruction::REM_LONG:
2707 case Instruction::AND_LONG:
2708 case Instruction::OR_LONG:
2709 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::SHL_LONG:
2713 case Instruction::SHR_LONG:
2714 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 /* shift distance is Int, making these different from other binary operations */
2716 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002717 break;
2718 case Instruction::ADD_FLOAT:
2719 case Instruction::SUB_FLOAT:
2720 case Instruction::MUL_FLOAT:
2721 case Instruction::DIV_FLOAT:
2722 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725 case Instruction::ADD_DOUBLE:
2726 case Instruction::SUB_DOUBLE:
2727 case Instruction::MUL_DOUBLE:
2728 case Instruction::DIV_DOUBLE:
2729 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::ADD_INT_2ADDR:
2733 case Instruction::SUB_INT_2ADDR:
2734 case Instruction::MUL_INT_2ADDR:
2735 case Instruction::REM_INT_2ADDR:
2736 case Instruction::SHL_INT_2ADDR:
2737 case Instruction::SHR_INT_2ADDR:
2738 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::AND_INT_2ADDR:
2742 case Instruction::OR_INT_2ADDR:
2743 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::ADD_LONG_2ADDR:
2750 case Instruction::SUB_LONG_2ADDR:
2751 case Instruction::MUL_LONG_2ADDR:
2752 case Instruction::DIV_LONG_2ADDR:
2753 case Instruction::REM_LONG_2ADDR:
2754 case Instruction::AND_LONG_2ADDR:
2755 case Instruction::OR_LONG_2ADDR:
2756 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::SHL_LONG_2ADDR:
2760 case Instruction::SHR_LONG_2ADDR:
2761 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::ADD_FLOAT_2ADDR:
2765 case Instruction::SUB_FLOAT_2ADDR:
2766 case Instruction::MUL_FLOAT_2ADDR:
2767 case Instruction::DIV_FLOAT_2ADDR:
2768 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771 case Instruction::ADD_DOUBLE_2ADDR:
2772 case Instruction::SUB_DOUBLE_2ADDR:
2773 case Instruction::MUL_DOUBLE_2ADDR:
2774 case Instruction::DIV_DOUBLE_2ADDR:
2775 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::ADD_INT_LIT16:
2779 case Instruction::RSUB_INT:
2780 case Instruction::MUL_INT_LIT16:
2781 case Instruction::DIV_INT_LIT16:
2782 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::AND_INT_LIT16:
2786 case Instruction::OR_INT_LIT16:
2787 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
2790 case Instruction::ADD_INT_LIT8:
2791 case Instruction::RSUB_INT_LIT8:
2792 case Instruction::MUL_INT_LIT8:
2793 case Instruction::DIV_INT_LIT8:
2794 case Instruction::REM_INT_LIT8:
2795 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002796 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002797 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002799 break;
2800 case Instruction::AND_INT_LIT8:
2801 case Instruction::OR_INT_LIT8:
2802 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
2805
2806 /*
2807 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002808 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002809 * inserted in the course of verification, we can expect to see it here.
2810 */
jeffhaob4df5142011-09-19 20:25:32 -07002811 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002815 case Instruction::UNUSED_EE:
2816 case Instruction::UNUSED_EF:
2817 case Instruction::UNUSED_F2:
2818 case Instruction::UNUSED_F3:
2819 case Instruction::UNUSED_F4:
2820 case Instruction::UNUSED_F5:
2821 case Instruction::UNUSED_F6:
2822 case Instruction::UNUSED_F7:
2823 case Instruction::UNUSED_F8:
2824 case Instruction::UNUSED_F9:
2825 case Instruction::UNUSED_FA:
2826 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::UNUSED_F0:
2828 case Instruction::UNUSED_F1:
2829 case Instruction::UNUSED_E3:
2830 case Instruction::UNUSED_E8:
2831 case Instruction::UNUSED_E7:
2832 case Instruction::UNUSED_E4:
2833 case Instruction::UNUSED_E9:
2834 case Instruction::UNUSED_FC:
2835 case Instruction::UNUSED_E5:
2836 case Instruction::UNUSED_EA:
2837 case Instruction::UNUSED_FD:
2838 case Instruction::UNUSED_E6:
2839 case Instruction::UNUSED_EB:
2840 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002841 case Instruction::UNUSED_3E:
2842 case Instruction::UNUSED_3F:
2843 case Instruction::UNUSED_40:
2844 case Instruction::UNUSED_41:
2845 case Instruction::UNUSED_42:
2846 case Instruction::UNUSED_43:
2847 case Instruction::UNUSED_73:
2848 case Instruction::UNUSED_79:
2849 case Instruction::UNUSED_7A:
2850 case Instruction::UNUSED_EC:
2851 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002852 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854
2855 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002856 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002857 * complain if an instruction is missing (which is desirable).
2858 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002860
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 if (failure_ != VERIFY_ERROR_NONE) {
2862 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002863 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002864 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002865 return false;
2866 } else {
2867 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002868 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002870 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002872 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002874 opcode_flag = Instruction::kThrow;
2875 }
2876 }
jeffhaobdb76512011-09-07 11:43:16 -07002877 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 * If we didn't just set the result register, clear it out. This ensures that you can only use
2879 * "move-result" immediately after the result is set. (We could check this statically, but it's
2880 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002881 */
2882 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002884 }
2885
jeffhaoa0a764a2011-09-16 10:43:38 -07002886 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002887 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2889 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2890 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002891 return false;
2892 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2894 // next instruction isn't one.
2895 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002896 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 }
2898 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2899 if (next_line != NULL) {
2900 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2901 // needed.
2902 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002903 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 }
jeffhaobdb76512011-09-07 11:43:16 -07002905 } else {
2906 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 * We're not recording register data for the next instruction, so we don't know what the prior
2908 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002909 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002911 }
2912 }
2913
2914 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002915 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002916 *
2917 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002918 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002919 * somebody could get a reference field, check it for zero, and if the
2920 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002921 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002922 * that, and will reject the code.
2923 *
2924 * TODO: avoid re-fetching the branch target
2925 */
2926 if ((opcode_flag & Instruction::kBranch) != 0) {
2927 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002929 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002931 return false;
2932 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002933 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002935 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 }
jeffhaobdb76512011-09-07 11:43:16 -07002937 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002939 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 }
jeffhaobdb76512011-09-07 11:43:16 -07002941 }
2942
2943 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002944 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002945 *
2946 * We've already verified that the table is structurally sound, so we
2947 * just need to walk through and tag the targets.
2948 */
2949 if ((opcode_flag & Instruction::kSwitch) != 0) {
2950 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2951 const uint16_t* switch_insns = insns + offset_to_switch;
2952 int switch_count = switch_insns[1];
2953 int offset_to_targets, targ;
2954
2955 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2956 /* 0 = sig, 1 = count, 2/3 = first key */
2957 offset_to_targets = 4;
2958 } else {
2959 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002960 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002961 offset_to_targets = 2 + 2 * switch_count;
2962 }
2963
2964 /* verify each switch target */
2965 for (targ = 0; targ < switch_count; targ++) {
2966 int offset;
2967 uint32_t abs_offset;
2968
2969 /* offsets are 32-bit, and only partly endian-swapped */
2970 offset = switch_insns[offset_to_targets + targ * 2] |
2971 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 abs_offset = work_insn_idx_ + offset;
2973 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2974 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002975 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 }
2977 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002978 return false;
2979 }
2980 }
2981
2982 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2984 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002985 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2987 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002988 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002989
Ian Rogers0571d352011-11-03 19:51:38 -07002990 for (; iterator.HasNext(); iterator.Next()) {
2991 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 within_catch_all = true;
2993 }
jeffhaobdb76512011-09-07 11:43:16 -07002994 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2996 * "work_regs", because at runtime the exception will be thrown before the instruction
2997 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002998 */
Ian Rogers0571d352011-11-03 19:51:38 -07002999 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003000 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 }
jeffhaobdb76512011-09-07 11:43:16 -07003002 }
3003
3004 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3006 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003007 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003009 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 * The state in work_line reflects the post-execution state. If the current instruction is a
3011 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003012 * it will do so before grabbing the lock).
3013 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
3015 Fail(VERIFY_ERROR_GENERIC)
3016 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003017 return false;
3018 }
3019 }
3020 }
3021
jeffhaod1f0fde2011-09-08 17:25:33 -07003022 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 if ((opcode_flag & Instruction::kReturn) != 0) {
3024 if(!work_line_->VerifyMonitorStackEmpty()) {
3025 return false;
3026 }
jeffhaobdb76512011-09-07 11:43:16 -07003027 }
3028
3029 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003030 * Update start_guess. Advance to the next instruction of that's
3031 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003032 * neither of those exists we're in a return or throw; leave start_guess
3033 * alone and let the caller sort it out.
3034 */
3035 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003037 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3038 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003040 }
3041
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3043 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003044
3045 return true;
3046}
3047
Ian Rogers28ad40d2011-10-27 15:19:26 -07003048const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003049 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003050 Class* referrer = method_->GetDeclaringClass();
3051 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3052 const RegType& result =
3053 klass != NULL ? reg_types_.FromClass(klass)
3054 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3055 if (klass == NULL && !result.IsUnresolvedTypes()) {
3056 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003058 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3059 // check at runtime if access is allowed and so pass here.
3060 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3061 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003062 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003063 << result << "'";
3064 return reg_types_.Unknown();
3065 } else {
3066 return result;
3067 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003068}
3069
Ian Rogers28ad40d2011-10-27 15:19:26 -07003070const RegType& DexVerifier::GetCaughtExceptionType() {
3071 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003073 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3075 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003076 CatchHandlerIterator iterator(handlers_ptr);
3077 for (; iterator.HasNext(); iterator.Next()) {
3078 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3079 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003080 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003082 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003083 if (common_super == NULL) {
3084 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3085 // as that is caught at runtime
3086 common_super = &exception;
3087 } else if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3088 // We don't know enough about the type and the common path merge will result in
3089 // Conflict. Fail here knowing the correct thing can be done at runtime.
Ian Rogers28ad40d2011-10-27 15:19:26 -07003090 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3091 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003092 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003093 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003095 common_super = &common_super->Merge(exception, &reg_types_);
3096 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 }
3098 }
3099 }
3100 }
Ian Rogers0571d352011-11-03 19:51:38 -07003101 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 }
3103 }
3104 if (common_super == NULL) {
3105 /* no catch blocks, or no catches with classes we can find */
3106 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003107 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003109 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003110}
3111
3112Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003113 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3114 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3115 if (failure_ != VERIFY_ERROR_NONE) {
3116 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3117 return NULL;
3118 }
3119 if(klass_type.IsUnresolvedTypes()) {
3120 return NULL; // Can't resolve Class so no more to do here
3121 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 Class* referrer = method_->GetDeclaringClass();
3123 DexCache* dex_cache = referrer->GetDexCache();
3124 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3125 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003126 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003127 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003128 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 if (is_direct) {
3130 res_method = klass->FindDirectMethod(name, signature);
3131 } else if (klass->IsInterface()) {
3132 res_method = klass->FindInterfaceMethod(name, signature);
3133 } else {
3134 res_method = klass->FindVirtualMethod(name, signature);
3135 }
3136 if (res_method != NULL) {
3137 dex_cache->SetResolvedMethod(method_idx, res_method);
3138 } else {
3139 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003140 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 << " " << signature;
3142 return NULL;
3143 }
3144 }
3145 /* Check if access is allowed. */
3146 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3147 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003148 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 return NULL;
3150 }
3151 return res_method;
3152}
3153
3154Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3155 MethodType method_type, bool is_range, bool is_super) {
3156 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3157 // we're making.
3158 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3159 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003160 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 return NULL;
3162 }
3163 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3164 // enforce them here.
3165 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3166 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3167 << PrettyMethod(res_method);
3168 return NULL;
3169 }
3170 // See if the method type implied by the invoke instruction matches the access flags for the
3171 // target method.
3172 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3173 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3174 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3175 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003176 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3177 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 return NULL;
3179 }
3180 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3181 // has a vtable entry for the target method.
3182 if (is_super) {
3183 DCHECK(method_type == METHOD_VIRTUAL);
3184 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003185 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 if (super == NULL) { // Only Object has no super class
3187 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3188 << " to super " << PrettyMethod(res_method);
3189 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003190 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003192 << " to super " << PrettyDescriptor(super)
3193 << "." << mh.GetName()
3194 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 }
3196 return NULL;
3197 }
3198 }
3199 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3200 // match the call to the signature. Also, we might might be calling through an abstract method
3201 // definition (which doesn't have register count values).
3202 int expected_args = dec_insn.vA_;
3203 /* caught by static verifier */
3204 DCHECK(is_range || expected_args <= 5);
3205 if (expected_args > code_item_->outs_size_) {
3206 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3207 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3208 return NULL;
3209 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003210 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 if (sig[0] != '(') {
3212 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3213 << " as descriptor doesn't start with '(': " << sig;
3214 return NULL;
3215 }
jeffhaobdb76512011-09-07 11:43:16 -07003216 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 * Check the "this" argument, which must be an instance of the class
3218 * that declared the method. For an interface class, we don't do the
3219 * full interface merge, so we can't do a rigorous check here (which
3220 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003221 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 int actual_args = 0;
3223 if (!res_method->IsStatic()) {
3224 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3225 if (failure_ != VERIFY_ERROR_NONE) {
3226 return NULL;
3227 }
3228 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3229 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3230 return NULL;
3231 }
3232 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003233 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3234 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3235 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3236 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 return NULL;
3238 }
3239 }
3240 actual_args++;
3241 }
3242 /*
3243 * Process the target method's signature. This signature may or may not
3244 * have been verified, so we can't assume it's properly formed.
3245 */
3246 size_t sig_offset = 0;
3247 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3248 if (actual_args >= expected_args) {
3249 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3250 << "'. Expected " << expected_args << " args, found more ("
3251 << sig.substr(sig_offset) << ")";
3252 return NULL;
3253 }
3254 std::string descriptor;
3255 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3256 size_t end;
3257 if (sig[sig_offset] == 'L') {
3258 end = sig.find(';', sig_offset);
3259 } else {
3260 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3261 if (sig[end] == 'L') {
3262 end = sig.find(';', end);
3263 }
3264 }
3265 if (end == std::string::npos) {
3266 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3267 << "bad signature component '" << sig << "' (missing ';')";
3268 return NULL;
3269 }
3270 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3271 sig_offset = end;
3272 } else {
3273 descriptor = sig[sig_offset];
3274 }
3275 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003276 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3277 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003278 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3279 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3280 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 }
3282 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3283 }
3284 if (sig[sig_offset] != ')') {
3285 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3286 return NULL;
3287 }
3288 if (actual_args != expected_args) {
3289 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3290 << " expected " << expected_args << " args, found " << actual_args;
3291 return NULL;
3292 } else {
3293 return res_method;
3294 }
3295}
3296
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003297const RegType& DexVerifier::GetMethodReturnType() {
3298 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3299 MethodHelper(method_).GetReturnTypeDescriptor());
3300}
3301
Ian Rogers0c4a5062012-02-03 15:18:59 -08003302void DexVerifier::VerifyNewArray(const Instruction::DecodedInstruction& dec_insn, bool is_filled,
3303 bool is_range) {
3304 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB_ : dec_insn.vC_);
3305 if (res_type.IsUnknown()) {
3306 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3307 } else {
3308 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3309 if (!res_type.IsArrayTypes()) {
3310 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
3311 } else if (!is_filled) {
3312 /* make sure "size" register is valid type */
3313 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
3314 /* set register type to array class */
3315 work_line_->SetRegisterType(dec_insn.vA_, res_type);
3316 } else {
3317 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3318 // the list and fail. It's legal, if silly, for arg_count to be zero.
3319 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3320 method_->GetDeclaringClass()->GetClassLoader());
3321 uint32_t arg_count = dec_insn.vA_;
3322 for (size_t ui = 0; ui < arg_count; ui++) {
3323 uint32_t get_reg = is_range ? dec_insn.vC_ + ui : dec_insn.arg_[ui];
3324 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3325 work_line_->SetResultRegisterType(reg_types_.Unknown());
3326 return;
3327 }
3328 }
3329 // filled-array result goes into "result" register
3330 work_line_->SetResultRegisterType(res_type);
3331 }
3332 }
3333}
3334
Ian Rogersd81871c2011-10-03 13:57:23 -07003335void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3336 const RegType& insn_type, bool is_primitive) {
3337 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3338 if (!index_type.IsArrayIndexTypes()) {
3339 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3340 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003341 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3342 if (array_type.IsZero()) {
3343 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3344 // instruction type. TODO: have a proper notion of bottom here.
3345 if (!is_primitive || insn_type.IsCategory1Types()) {
3346 // Reference or category 1
3347 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003349 // Category 2
3350 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3351 }
jeffhaofc3144e2012-02-01 17:21:15 -08003352 } else if (!array_type.IsArrayTypes()) {
3353 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003354 } else {
3355 /* verify the class */
3356 const RegType& component_type = reg_types_.GetComponentType(array_type,
3357 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003358 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003359 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3360 << " source for aget-object";
3361 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3362 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3363 << " source for category 1 aget";
3364 } else if (is_primitive && !insn_type.Equals(component_type) &&
3365 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3366 (insn_type.IsLong() && component_type.IsDouble()))) {
3367 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003369 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 // Use knowledge of the field type which is stronger than the type inferred from the
3371 // instruction, which can't differentiate object types and ints from floats, longs from
3372 // doubles.
3373 work_line_->SetRegisterType(dec_insn.vA_, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 }
3375 }
3376 }
3377}
3378
3379void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3380 const RegType& insn_type, bool is_primitive) {
3381 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3382 if (!index_type.IsArrayIndexTypes()) {
3383 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3384 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003385 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB_);
3386 if (array_type.IsZero()) {
3387 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3388 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003389 } else if (!array_type.IsArrayTypes()) {
3390 Fail(VERIFY_ERROR_GENERIC) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003391 } else {
3392 /* verify the class */
3393 const RegType& component_type = reg_types_.GetComponentType(array_type,
3394 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003395 if (!component_type.IsReferenceTypes() && !is_primitive) {
Ian Rogers89310de2012-02-01 13:47:30 -08003396 Fail(VERIFY_ERROR_GENERIC) << "primitive array type " << array_type
3397 << " source for aput-object";
3398 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
3399 Fail(VERIFY_ERROR_GENERIC) << "reference array type " << array_type
3400 << " source for category 1 aput";
3401 } else if (is_primitive && !insn_type.Equals(component_type) &&
3402 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3403 (insn_type.IsLong() && component_type.IsDouble()))) {
3404 Fail(VERIFY_ERROR_GENERIC) << "array type " << array_type
3405 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003407 // The instruction agrees with the type of array, confirm the value to be stored does too
3408 // Note: we use the instruction type (rather than the component type) for aput-object as
3409 // incompatible classes will be caught at runtime as an array store exception
3410 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 }
3412 }
3413 }
3414}
3415
3416Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003417 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3418 // Check access to class
3419 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3420 if (failure_ != VERIFY_ERROR_NONE) {
3421 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3422 << dex_file_->GetFieldName(field_id) << ") in "
3423 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3424 return NULL;
3425 }
3426 if(klass_type.IsUnresolvedTypes()) {
3427 return NULL; // Can't resolve Class so no more to do here
3428 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003429 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003431 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3432 << dex_file_->GetFieldName(field_id) << ") in "
3433 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 DCHECK(Thread::Current()->IsExceptionPending());
3435 Thread::Current()->ClearException();
3436 return NULL;
3437 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3438 field->GetAccessFlags())) {
3439 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3440 << " from " << PrettyClass(method_->GetDeclaringClass());
3441 return NULL;
3442 } else if (!field->IsStatic()) {
3443 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3444 return NULL;
3445 } else {
3446 return field;
3447 }
3448}
3449
Ian Rogersd81871c2011-10-03 13:57:23 -07003450Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003451 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3452 // Check access to class
3453 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3454 if (failure_ != VERIFY_ERROR_NONE) {
3455 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3456 << dex_file_->GetFieldName(field_id) << ") in "
3457 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3458 return NULL;
3459 }
3460 if(klass_type.IsUnresolvedTypes()) {
3461 return NULL; // Can't resolve Class so no more to do here
3462 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003463 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003465 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3466 << dex_file_->GetFieldName(field_id) << ") in "
3467 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 DCHECK(Thread::Current()->IsExceptionPending());
3469 Thread::Current()->ClearException();
3470 return NULL;
3471 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3472 field->GetAccessFlags())) {
3473 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3474 << " from " << PrettyClass(method_->GetDeclaringClass());
3475 return NULL;
3476 } else if (field->IsStatic()) {
3477 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3478 << " to not be static";
3479 return NULL;
3480 } else if (obj_type.IsZero()) {
3481 // Cannot infer and check type, however, access will cause null pointer exception
3482 return field;
3483 } else if(obj_type.IsUninitializedReference() &&
3484 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3485 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3486 // Field accesses through uninitialized references are only allowable for constructors where
3487 // the field is declared in this class
3488 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3489 << " of a not fully initialized object within the context of "
3490 << PrettyMethod(method_);
3491 return NULL;
3492 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3493 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3494 // of C1. For resolution to occur the declared class of the field must be compatible with
3495 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3496 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3497 << " from object of type " << PrettyClass(obj_type.GetClass());
3498 return NULL;
3499 } else {
3500 return field;
3501 }
3502}
3503
Ian Rogersb94a27b2011-10-26 00:33:41 -07003504void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3505 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003506 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003507 Field* field;
3508 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003509 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003510 } else {
3511 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003512 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003513 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003514 if (failure_ != VERIFY_ERROR_NONE) {
3515 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3516 } else {
3517 const char* descriptor;
3518 const ClassLoader* loader;
3519 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003520 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003521 loader = field->GetDeclaringClass()->GetClassLoader();
3522 } else {
3523 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3524 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3525 loader = method_->GetDeclaringClass()->GetClassLoader();
3526 }
3527 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003528 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003529 if (field_type.Equals(insn_type) ||
3530 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3531 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 // expected that read is of the correct primitive type or that int reads are reading
3533 // floats or long reads are reading doubles
3534 } else {
3535 // This is a global failure rather than a class change failure as the instructions and
3536 // the descriptors for the type should have been consistent within the same file at
3537 // compile time
3538 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003539 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003540 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 return;
3542 }
3543 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003544 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003546 << " to be compatible with type '" << insn_type
3547 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003548 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 return;
3550 }
3551 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003552 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003553 }
3554}
3555
Ian Rogersb94a27b2011-10-26 00:33:41 -07003556void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3557 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003558 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003559 Field* field;
3560 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003561 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003562 } else {
3563 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003564 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003565 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003566 if (failure_ != VERIFY_ERROR_NONE) {
3567 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3568 } else {
3569 const char* descriptor;
3570 const ClassLoader* loader;
3571 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003572 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003573 loader = field->GetDeclaringClass()->GetClassLoader();
3574 } else {
3575 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3576 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3577 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003579 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3580 if (field != NULL) {
3581 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3582 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3583 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3584 return;
3585 }
3586 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003588 // Primitive field assignability rules are weaker than regular assignability rules
3589 bool instruction_compatible;
3590 bool value_compatible;
3591 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3592 if (field_type.IsIntegralTypes()) {
3593 instruction_compatible = insn_type.IsIntegralTypes();
3594 value_compatible = value_type.IsIntegralTypes();
3595 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003596 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003597 value_compatible = value_type.IsFloatTypes();
3598 } else if (field_type.IsLong()) {
3599 instruction_compatible = insn_type.IsLong();
3600 value_compatible = value_type.IsLongTypes();
3601 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003602 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003603 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003605 instruction_compatible = false; // reference field with primitive store
3606 value_compatible = false; // unused
3607 }
3608 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 // This is a global failure rather than a class change failure as the instructions and
3610 // the descriptors for the type should have been consistent within the same file at
3611 // compile time
3612 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003613 << " to be of type '" << insn_type
3614 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003615 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 return;
3617 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003618 if (!value_compatible) {
3619 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3620 << " of type " << value_type
3621 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003622 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003623 return;
3624 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003626 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003628 << " to be compatible with type '" << insn_type
3629 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003630 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 return;
3632 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003633 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003635 }
3636}
3637
3638bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3639 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3640 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3641 return false;
3642 }
3643 return true;
3644}
3645
Ian Rogersd81871c2011-10-03 13:57:23 -07003646void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003647 if (Runtime::Current()->IsStarted()) {
3648 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3649 << " " << fail_messages_.str();
3650 return;
3651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3653 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3654 VerifyErrorRefType ref_type;
3655 switch (inst->Opcode()) {
3656 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003657 case Instruction::CHECK_CAST:
3658 case Instruction::INSTANCE_OF:
3659 case Instruction::NEW_INSTANCE:
3660 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003662 case Instruction::FILLED_NEW_ARRAY_RANGE:
3663 ref_type = VERIFY_ERROR_REF_CLASS;
3664 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003666 case Instruction::IGET_BOOLEAN:
3667 case Instruction::IGET_BYTE:
3668 case Instruction::IGET_CHAR:
3669 case Instruction::IGET_SHORT:
3670 case Instruction::IGET_WIDE:
3671 case Instruction::IGET_OBJECT:
3672 case Instruction::IPUT:
3673 case Instruction::IPUT_BOOLEAN:
3674 case Instruction::IPUT_BYTE:
3675 case Instruction::IPUT_CHAR:
3676 case Instruction::IPUT_SHORT:
3677 case Instruction::IPUT_WIDE:
3678 case Instruction::IPUT_OBJECT:
3679 case Instruction::SGET:
3680 case Instruction::SGET_BOOLEAN:
3681 case Instruction::SGET_BYTE:
3682 case Instruction::SGET_CHAR:
3683 case Instruction::SGET_SHORT:
3684 case Instruction::SGET_WIDE:
3685 case Instruction::SGET_OBJECT:
3686 case Instruction::SPUT:
3687 case Instruction::SPUT_BOOLEAN:
3688 case Instruction::SPUT_BYTE:
3689 case Instruction::SPUT_CHAR:
3690 case Instruction::SPUT_SHORT:
3691 case Instruction::SPUT_WIDE:
3692 case Instruction::SPUT_OBJECT:
3693 ref_type = VERIFY_ERROR_REF_FIELD;
3694 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003696 case Instruction::INVOKE_VIRTUAL_RANGE:
3697 case Instruction::INVOKE_SUPER:
3698 case Instruction::INVOKE_SUPER_RANGE:
3699 case Instruction::INVOKE_DIRECT:
3700 case Instruction::INVOKE_DIRECT_RANGE:
3701 case Instruction::INVOKE_STATIC:
3702 case Instruction::INVOKE_STATIC_RANGE:
3703 case Instruction::INVOKE_INTERFACE:
3704 case Instruction::INVOKE_INTERFACE_RANGE:
3705 ref_type = VERIFY_ERROR_REF_METHOD;
3706 break;
jeffhaobdb76512011-09-07 11:43:16 -07003707 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003708 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003709 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003710 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003711 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3712 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3713 // instruction, so assert it.
3714 size_t width = inst->SizeInCodeUnits();
3715 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003716 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003717 // NOPs
3718 for (size_t i = 2; i < width; i++) {
3719 insns[work_insn_idx_ + i] = Instruction::NOP;
3720 }
3721 // Encode the opcode, with the failure code in the high byte
3722 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3723 (failure_ << 8) | // AA - component
3724 (ref_type << (8 + kVerifyErrorRefTypeShift));
3725 insns[work_insn_idx_] = new_instruction;
3726 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3727 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003728 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3729 << fail_messages_.str();
3730 if (gDebugVerify) {
3731 std::cout << std::endl << info_messages_.str();
3732 Dump(std::cout);
3733 }
jeffhaobdb76512011-09-07 11:43:16 -07003734}
jeffhaoba5ebb92011-08-25 17:24:37 -07003735
Ian Rogersd81871c2011-10-03 13:57:23 -07003736bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3737 const bool merge_debug = true;
3738 bool changed = true;
3739 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3740 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003741 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 * We haven't processed this instruction before, and we haven't touched the registers here, so
3743 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3744 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003745 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003747 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3749 copy->CopyFromLine(target_line);
3750 changed = target_line->MergeRegisters(merge_line);
3751 if (failure_ != VERIFY_ERROR_NONE) {
3752 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003753 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003754 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3756 << *copy.get() << " MERGE" << std::endl
3757 << *merge_line << " ==" << std::endl
3758 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003759 }
3760 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 if (changed) {
3762 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003763 }
3764 return true;
3765}
3766
Ian Rogersd81871c2011-10-03 13:57:23 -07003767void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3768 size_t* log2_max_gc_pc) {
3769 size_t local_gc_points = 0;
3770 size_t max_insn = 0;
3771 size_t max_ref_reg = -1;
3772 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3773 if (insn_flags_[i].IsGcPoint()) {
3774 local_gc_points++;
3775 max_insn = i;
3776 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003777 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003778 }
3779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 *gc_points = local_gc_points;
3781 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3782 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003783 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 i++;
3785 }
3786 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003787}
3788
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003789const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 size_t num_entries, ref_bitmap_bits, pc_bits;
3791 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3792 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003793 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 // TODO: either a better GC map format or per method failures
3795 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3796 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003797 return NULL;
3798 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3800 // There are 2 bytes to encode the number of entries
3801 if (num_entries >= 65536) {
3802 // TODO: either a better GC map format or per method failures
3803 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3804 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003805 return NULL;
3806 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003808 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003809 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003810 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003811 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003812 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003813 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003815 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 // TODO: either a better GC map format or per method failures
3817 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3818 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3819 return NULL;
3820 }
3821 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003822 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 if (table == NULL) {
3824 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3825 return NULL;
3826 }
3827 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003828 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3829 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003830 table->push_back(num_entries & 0xFF);
3831 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3834 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003835 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003837 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 }
3839 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003840 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 }
3842 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003843 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 return table;
3845}
jeffhaoa0a764a2011-09-16 10:43:38 -07003846
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003847void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3849 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003850 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 size_t map_index = 0;
3852 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3853 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3854 if (insn_flags_[i].IsGcPoint()) {
3855 CHECK_LT(map_index, map.NumEntries());
3856 CHECK_EQ(map.GetPC(map_index), i);
3857 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3858 map_index++;
3859 RegisterLine* line = reg_table_.GetLine(i);
3860 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003861 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 CHECK_LT(j / 8, map.RegWidth());
3863 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3864 } else if ((j / 8) < map.RegWidth()) {
3865 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3866 } else {
3867 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3868 }
3869 }
3870 } else {
3871 CHECK(reg_bitmap == NULL);
3872 }
3873 }
3874}
jeffhaoa0a764a2011-09-16 10:43:38 -07003875
Ian Rogersd81871c2011-10-03 13:57:23 -07003876const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3877 size_t num_entries = NumEntries();
3878 // Do linear or binary search?
3879 static const size_t kSearchThreshold = 8;
3880 if (num_entries < kSearchThreshold) {
3881 for (size_t i = 0; i < num_entries; i++) {
3882 if (GetPC(i) == dex_pc) {
3883 return GetBitMap(i);
3884 }
3885 }
3886 } else {
3887 int lo = 0;
3888 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003889 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 int mid = (hi + lo) / 2;
3891 int mid_pc = GetPC(mid);
3892 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003893 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003894 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003895 hi = mid - 1;
3896 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003897 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003898 }
3899 }
3900 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003901 if (error_if_not_present) {
3902 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3903 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003904 return NULL;
3905}
3906
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003907Mutex DexVerifier::gc_maps_lock_("verifier gc maps lock");
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003908DexVerifier::GcMapTable DexVerifier::gc_maps_;
3909
3910void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003911 MutexLock mu(gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003912 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3913 if (existing_gc_map != NULL) {
3914 CHECK(*existing_gc_map == gc_map);
3915 delete existing_gc_map;
3916 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003917 gc_maps_[ref] = &gc_map;
3918 CHECK(GetGcMap(ref) != NULL);
3919}
3920
3921const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003922 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003923 GcMapTable::const_iterator it = gc_maps_.find(ref);
3924 if (it == gc_maps_.end()) {
3925 return NULL;
3926 }
3927 CHECK(it->second != NULL);
3928 return it->second;
3929}
3930
3931void DexVerifier::DeleteGcMaps() {
Elliott Hughesd9c67be2012-02-02 19:54:06 -08003932 MutexLock mu(gc_maps_lock_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003933 STLDeleteValues(&gc_maps_);
3934}
3935
Ian Rogersd81871c2011-10-03 13:57:23 -07003936} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003937} // namespace art