blob: c03b7242e0b8436fcd13dd3d459e43cf334302b4 [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()) {
213 return src.IsObjectArray(); // 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) {
556 CHECK(array.IsArrayClass());
557 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
578void RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
579 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";
586 } else if (new_type.IsConflict()) { // should only be set during a merge
587 verifier_->Fail(VERIFY_ERROR_GENERIC) << "Set register to unknown type " << new_type;
588 } else {
589 line_[vdst] = new_type.GetId();
590 }
591 // Clear the monitor entry bits for this register.
592 ClearAllRegToLockDepths(vdst);
593}
594
595void RegisterLine::SetResultTypeToUnknown() {
596 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
597 result_[0] = unknown_id;
598 result_[1] = unknown_id;
599}
600
601void RegisterLine::SetResultRegisterType(const RegType& new_type) {
602 result_[0] = new_type.GetId();
603 if(new_type.IsLowHalf()) {
604 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
605 result_[1] = new_type.GetId() + 1;
606 } else {
607 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
608 }
609}
610
611const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
612 // The register index was validated during the static pass, so we don't need to check it here.
613 DCHECK_LT(vsrc, num_regs_);
614 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
615}
616
617const RegType& RegisterLine::GetInvocationThis(const Instruction::DecodedInstruction& dec_insn) {
618 if (dec_insn.vA_ < 1) {
619 verifier_->Fail(VERIFY_ERROR_GENERIC) << "invoke lacks 'this'";
620 return verifier_->GetRegTypeCache()->Unknown();
621 }
622 /* get the element type of the array held in vsrc */
623 const RegType& this_type = GetRegisterType(dec_insn.vC_);
624 if (!this_type.IsReferenceTypes()) {
625 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-reference register v"
626 << dec_insn.vC_ << " (type=" << this_type << ")";
627 return verifier_->GetRegTypeCache()->Unknown();
628 }
629 return this_type;
630}
631
632Class* RegisterLine::GetClassFromRegister(uint32_t vsrc) const {
633 /* get the element type of the array held in vsrc */
634 const RegType& type = GetRegisterType(vsrc);
635 /* if "always zero", we allow it to fail at runtime */
636 if (type.IsZero()) {
637 return NULL;
638 } else if (!type.IsReferenceTypes()) {
639 verifier_->Fail(VERIFY_ERROR_GENERIC) << "tried to get class from non-ref register v" << vsrc
640 << " (type=" << type << ")";
641 return NULL;
642 } else if (type.IsUninitializedReference()) {
643 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register " << vsrc << " holds uninitialized reference";
644 return NULL;
645 } else {
646 return type.GetClass();
647 }
648}
649
650bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
651 // Verify the src register type against the check type refining the type of the register
652 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700653 if (!check_type.IsAssignableFrom(src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 verifier_->Fail(VERIFY_ERROR_GENERIC) << "register v" << vsrc << " has type " << src_type
655 << " but expected " << check_type;
656 return false;
657 }
658 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
659 // precise than the subtype in vsrc so leave it for reference types. For primitive types
660 // if they are a defined type then they are as precise as we can get, however, for constant
661 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
662 return true;
663}
664
665void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700666 DCHECK(uninit_type.IsUninitializedTypes());
667 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
668 size_t changed = 0;
669 for (size_t i = 0; i < num_regs_; i++) {
670 if (GetRegisterType(i).Equals(uninit_type)) {
671 line_[i] = init_type.GetId();
672 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700675 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676}
677
678void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
679 for (size_t i = 0; i < num_regs_; i++) {
680 if (GetRegisterType(i).Equals(uninit_type)) {
681 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
682 ClearAllRegToLockDepths(i);
683 }
684 }
685}
686
687void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
688 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
689 const RegType& type = GetRegisterType(vsrc);
690 SetRegisterType(vdst, type);
691 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
692 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
693 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
694 << " cat=" << static_cast<int>(cat);
695 } else if (cat == kTypeCategoryRef) {
696 CopyRegToLockDepth(vdst, vsrc);
697 }
698}
699
700void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
701 const RegType& type_l = GetRegisterType(vsrc);
702 const RegType& type_h = GetRegisterType(vsrc + 1);
703
704 if (!type_l.CheckWidePair(type_h)) {
705 verifier_->Fail(VERIFY_ERROR_GENERIC) << "copy2 v" << vdst << "<-v" << vsrc
706 << " type=" << type_l << "/" << type_h;
707 } else {
708 SetRegisterType(vdst, type_l); // implicitly sets the second half
709 }
710}
711
712void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
713 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
714 if ((!is_reference && !type.IsCategory1Types()) ||
715 (is_reference && !type.IsReferenceTypes())) {
716 verifier_->Fail(VERIFY_ERROR_GENERIC)
717 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
718 } else {
719 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
720 SetRegisterType(vdst, type);
721 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
722 }
723}
724
725/*
726 * Implement "move-result-wide". Copy the category-2 value from the result
727 * register to another register, and reset the result register.
728 */
729void RegisterLine::CopyResultRegister2(uint32_t vdst) {
730 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
731 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
732 if (!type_l.IsCategory2Types()) {
733 verifier_->Fail(VERIFY_ERROR_GENERIC)
734 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
735 } else {
736 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
737 SetRegisterType(vdst, type_l); // also sets the high
738 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
739 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
740 }
741}
742
743void RegisterLine::CheckUnaryOp(const Instruction::DecodedInstruction& dec_insn,
744 const RegType& dst_type, const RegType& src_type) {
745 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
746 SetRegisterType(dec_insn.vA_, dst_type);
747 }
748}
749
750void RegisterLine::CheckBinaryOp(const Instruction::DecodedInstruction& dec_insn,
751 const RegType& dst_type,
752 const RegType& src_type1, const RegType& src_type2,
753 bool check_boolean_op) {
754 if (VerifyRegisterType(dec_insn.vB_, src_type1) &&
755 VerifyRegisterType(dec_insn.vC_, src_type2)) {
756 if (check_boolean_op) {
757 DCHECK(dst_type.IsInteger());
758 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
759 GetRegisterType(dec_insn.vC_).IsBooleanTypes()) {
760 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
761 return;
762 }
763 }
764 SetRegisterType(dec_insn.vA_, dst_type);
765 }
766}
767
768void RegisterLine::CheckBinaryOp2addr(const Instruction::DecodedInstruction& dec_insn,
769 const RegType& dst_type, const RegType& src_type1,
770 const RegType& src_type2, bool check_boolean_op) {
771 if (VerifyRegisterType(dec_insn.vA_, src_type1) &&
772 VerifyRegisterType(dec_insn.vB_, src_type2)) {
773 if (check_boolean_op) {
774 DCHECK(dst_type.IsInteger());
775 if (GetRegisterType(dec_insn.vA_).IsBooleanTypes() &&
776 GetRegisterType(dec_insn.vB_).IsBooleanTypes()) {
777 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
778 return;
779 }
780 }
781 SetRegisterType(dec_insn.vA_, dst_type);
782 }
783}
784
785void RegisterLine::CheckLiteralOp(const Instruction::DecodedInstruction& dec_insn,
786 const RegType& dst_type, const RegType& src_type,
787 bool check_boolean_op) {
788 if (VerifyRegisterType(dec_insn.vB_, src_type)) {
789 if (check_boolean_op) {
790 DCHECK(dst_type.IsInteger());
791 /* check vB with the call, then check the constant manually */
792 if (GetRegisterType(dec_insn.vB_).IsBooleanTypes() &&
793 (dec_insn.vC_ == 0 || dec_insn.vC_ == 1)) {
794 SetRegisterType(dec_insn.vA_, verifier_->GetRegTypeCache()->Boolean());
795 return;
796 }
797 }
798 SetRegisterType(dec_insn.vA_, dst_type);
799 }
800}
801
802void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
803 const RegType& reg_type = GetRegisterType(reg_idx);
804 if (!reg_type.IsReferenceTypes()) {
805 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800806 } else if (monitors_.size() >= 32) {
807 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 } else {
809 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700810 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 }
812}
813
814void RegisterLine::PopMonitor(uint32_t reg_idx) {
815 const RegType& reg_type = GetRegisterType(reg_idx);
816 if (!reg_type.IsReferenceTypes()) {
817 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit on non-object (" << reg_type << ")";
818 } else if (monitors_.empty()) {
819 verifier_->Fail(VERIFY_ERROR_GENERIC) << "monitor-exit stack underflow";
820 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700821 monitors_.pop_back();
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 if(!IsSetLockDepth(reg_idx, monitors_.size())) {
823 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
824 // format "036" the constant collector may create unlocks on the same object but referenced
825 // via different registers.
826 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_GENERIC)
827 : verifier_->LogVerifyInfo())
828 << "monitor-exit not unlocking the top of the monitor stack";
829 } else {
830 // Record the register was unlocked
831 ClearRegToLockDepth(reg_idx, monitors_.size());
832 }
833 }
834}
835
836bool RegisterLine::VerifyMonitorStackEmpty() {
837 if (MonitorStackDepth() != 0) {
838 verifier_->Fail(VERIFY_ERROR_GENERIC) << "expected empty monitor stack";
839 return false;
840 } else {
841 return true;
842 }
843}
844
845bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
846 bool changed = false;
847 for (size_t idx = 0; idx < num_regs_; idx++) {
848 if (line_[idx] != incoming_line->line_[idx]) {
849 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
850 const RegType& cur_type = GetRegisterType(idx);
851 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
852 changed = changed || !cur_type.Equals(new_type);
853 line_[idx] = new_type.GetId();
854 }
855 }
Ian Rogers55d249f2011-11-02 16:48:09 -0700856 if(monitors_.size() != incoming_line->monitors_.size()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths (depth="
858 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
859 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
860 for (uint32_t idx = 0; idx < num_regs_; idx++) {
861 size_t depths = reg_to_lock_depths_.count(idx);
862 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
863 if (depths != incoming_depths) {
864 if (depths == 0 || incoming_depths == 0) {
865 reg_to_lock_depths_.erase(idx);
866 } else {
867 verifier_->Fail(VERIFY_ERROR_GENERIC) << "mismatched stack depths for register v" << idx
868 << ": " << depths << " != " << incoming_depths;
869 break;
870 }
871 }
872 }
873 }
874 return changed;
875}
876
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800877void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 for (size_t i = 0; i < num_regs_; i += 8) {
879 uint8_t val = 0;
880 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
881 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700882 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 val |= 1 << j;
884 }
885 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800886 if ((i / 8) >= max_bytes) {
887 DCHECK_EQ(0, val);
888 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800890 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
891 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 }
893}
894
895std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700896 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 return os;
898}
899
900
901void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
902 uint32_t insns_size, uint16_t registers_size,
903 DexVerifier* verifier) {
904 DCHECK_GT(insns_size, 0U);
905
906 for (uint32_t i = 0; i < insns_size; i++) {
907 bool interesting = false;
908 switch (mode) {
909 case kTrackRegsAll:
910 interesting = flags[i].IsOpcode();
911 break;
912 case kTrackRegsGcPoints:
913 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
914 break;
915 case kTrackRegsBranches:
916 interesting = flags[i].IsBranchTarget();
917 break;
918 default:
919 break;
920 }
921 if (interesting) {
922 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
923 }
924 }
925}
926
Ian Rogers1c5eb702012-02-01 09:18:34 -0800927bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700928 if (klass->IsVerified()) {
929 return true;
930 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800932 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800933 error = "Verifier rejected class ";
934 error += PrettyDescriptor(klass);
935 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800938 if (super != NULL && super->IsFinal()) {
939 error = "Verifier rejected class ";
940 error += PrettyDescriptor(klass);
941 error += " that attempts to sub-class final class ";
942 error += PrettyDescriptor(super);
943 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 }
jeffhaobdb76512011-09-07 11:43:16 -0700945 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
946 Method* method = klass->GetDirectMethod(i);
947 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800948 error = "Verifier rejected class ";
949 error += PrettyDescriptor(klass);
950 error += " due to bad method ";
951 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700952 return false;
953 }
954 }
955 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
956 Method* method = klass->GetVirtualMethod(i);
957 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800958 error = "Verifier rejected class ";
959 error += PrettyDescriptor(klass);
960 error += " due to bad method ";
961 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700962 return false;
963 }
964 }
965 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700966}
967
jeffhaobdb76512011-09-07 11:43:16 -0700968bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 DexVerifier verifier(method);
970 bool success = verifier.Verify();
Brian Carlstrom75412882012-01-18 01:26:54 -0800971 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
972
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 // We expect either success and no verification error, or failure and a generic failure to
974 // reject the class.
975 if (success) {
976 if (verifier.failure_ != VERIFY_ERROR_NONE) {
977 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
978 << verifier.fail_messages_;
979 }
980 } else {
981 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700982 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700983 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700984 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700985 verifier.Dump(std::cout);
986 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 DCHECK_EQ(verifier.failure_, VERIFY_ERROR_GENERIC);
988 }
989 return success;
990}
991
Shih-wei Liao371814f2011-10-27 16:52:10 -0700992void DexVerifier::VerifyMethodAndDump(Method* method) {
993 DexVerifier verifier(method);
994 verifier.Verify();
995
Elliott Hughese0918552011-10-28 17:18:29 -0700996 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
997 << verifier.fail_messages_.str() << std::endl
998 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700999}
1000
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001001DexVerifier::DexVerifier(Method* method)
1002 : work_insn_idx_(-1),
1003 method_(method),
1004 failure_(VERIFY_ERROR_NONE),
1005 new_instance_count_(0),
1006 monitor_enter_count_(0) {
1007 CHECK(method != NULL);
jeffhaobdb76512011-09-07 11:43:16 -07001008 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001009 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 dex_file_ = &class_linker->FindDexFile(dex_cache);
1011 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaoba5ebb92011-08-25 17:24:37 -07001012}
1013
Ian Rogersd81871c2011-10-03 13:57:23 -07001014bool DexVerifier::Verify() {
1015 // If there aren't any instructions, make sure that's expected, then exit successfully.
1016 if (code_item_ == NULL) {
1017 if (!method_->IsNative() && !method_->IsAbstract()) {
1018 Fail(VERIFY_ERROR_GENERIC) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001019 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 } else {
1021 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001022 }
jeffhaobdb76512011-09-07 11:43:16 -07001023 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1025 if (code_item_->ins_size_ > code_item_->registers_size_) {
1026 Fail(VERIFY_ERROR_GENERIC) << "bad register counts (ins=" << code_item_->ins_size_
1027 << " regs=" << code_item_->registers_size_;
1028 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001029 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 // Allocate and initialize an array to hold instruction data.
1031 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1032 // Run through the instructions and see if the width checks out.
1033 bool result = ComputeWidthsAndCountOps();
1034 // Flag instructions guarded by a "try" block and check exception handlers.
1035 result = result && ScanTryCatchBlocks();
1036 // Perform static instruction verification.
1037 result = result && VerifyInstructions();
1038 // Perform code flow analysis.
1039 result = result && VerifyCodeFlow();
jeffhaobdb76512011-09-07 11:43:16 -07001040 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001041}
1042
Ian Rogersd81871c2011-10-03 13:57:23 -07001043bool DexVerifier::ComputeWidthsAndCountOps() {
1044 const uint16_t* insns = code_item_->insns_;
1045 size_t insns_size = code_item_->insns_size_in_code_units_;
1046 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001047 size_t new_instance_count = 0;
1048 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001050
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001052 Instruction::Code opcode = inst->Opcode();
1053 if (opcode == Instruction::NEW_INSTANCE) {
1054 new_instance_count++;
1055 } else if (opcode == Instruction::MONITOR_ENTER) {
1056 monitor_enter_count++;
1057 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 size_t inst_size = inst->SizeInCodeUnits();
1059 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1060 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001061 inst = inst->Next();
1062 }
1063
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 if (dex_pc != insns_size) {
1065 Fail(VERIFY_ERROR_GENERIC) << "code did not end where expected ("
1066 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001067 return false;
1068 }
1069
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 new_instance_count_ = new_instance_count;
1071 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001072 return true;
1073}
1074
Ian Rogersd81871c2011-10-03 13:57:23 -07001075bool DexVerifier::ScanTryCatchBlocks() {
1076 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001077 if (tries_size == 0) {
1078 return true;
1079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001081 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001082
1083 for (uint32_t idx = 0; idx < tries_size; idx++) {
1084 const DexFile::TryItem* try_item = &tries[idx];
1085 uint32_t start = try_item->start_addr_;
1086 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001087 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 Fail(VERIFY_ERROR_GENERIC) << "bad exception entry: startAddr=" << start
1089 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001090 return false;
1091 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 if (!insn_flags_[start].IsOpcode()) {
1093 Fail(VERIFY_ERROR_GENERIC) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001094 return false;
1095 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 for (uint32_t dex_pc = start; dex_pc < end;
1097 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1098 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001099 }
1100 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001101 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001102 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001103 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001104 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001105 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001106 CatchHandlerIterator iterator(handlers_ptr);
1107 for (; iterator.HasNext(); iterator.Next()) {
1108 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 if (!insn_flags_[dex_pc].IsOpcode()) {
1110 Fail(VERIFY_ERROR_GENERIC) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001111 return false;
1112 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001114 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1115 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001116 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
1117 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001118 if (exception_type == NULL) {
1119 DCHECK(Thread::Current()->IsExceptionPending());
1120 Thread::Current()->ClearException();
1121 }
1122 }
jeffhaobdb76512011-09-07 11:43:16 -07001123 }
Ian Rogers0571d352011-11-03 19:51:38 -07001124 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001125 }
jeffhaobdb76512011-09-07 11:43:16 -07001126 return true;
1127}
1128
Ian Rogersd81871c2011-10-03 13:57:23 -07001129bool DexVerifier::VerifyInstructions() {
1130 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001131
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 /* Flag the start of the method as a branch target. */
1133 insn_flags_[0].SetBranchTarget();
1134
1135 uint32_t insns_size = code_item_->insns_size_in_code_units_;
1136 for(uint32_t dex_pc = 0; dex_pc < insns_size;) {
1137 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001138 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1139 fail_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_) << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 return false;
1141 }
1142 /* Flag instructions that are garbage collection points */
1143 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1144 insn_flags_[dex_pc].SetGcPoint();
1145 }
1146 dex_pc += inst->SizeInCodeUnits();
1147 inst = inst->Next();
1148 }
1149 return true;
1150}
1151
1152bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
1153 Instruction::DecodedInstruction dec_insn(inst);
1154 bool result = true;
1155 switch (inst->GetVerifyTypeArgumentA()) {
1156 case Instruction::kVerifyRegA:
1157 result = result && CheckRegisterIndex(dec_insn.vA_);
1158 break;
1159 case Instruction::kVerifyRegAWide:
1160 result = result && CheckWideRegisterIndex(dec_insn.vA_);
1161 break;
1162 }
1163 switch (inst->GetVerifyTypeArgumentB()) {
1164 case Instruction::kVerifyRegB:
1165 result = result && CheckRegisterIndex(dec_insn.vB_);
1166 break;
1167 case Instruction::kVerifyRegBField:
1168 result = result && CheckFieldIndex(dec_insn.vB_);
1169 break;
1170 case Instruction::kVerifyRegBMethod:
1171 result = result && CheckMethodIndex(dec_insn.vB_);
1172 break;
1173 case Instruction::kVerifyRegBNewInstance:
1174 result = result && CheckNewInstance(dec_insn.vB_);
1175 break;
1176 case Instruction::kVerifyRegBString:
1177 result = result && CheckStringIndex(dec_insn.vB_);
1178 break;
1179 case Instruction::kVerifyRegBType:
1180 result = result && CheckTypeIndex(dec_insn.vB_);
1181 break;
1182 case Instruction::kVerifyRegBWide:
1183 result = result && CheckWideRegisterIndex(dec_insn.vB_);
1184 break;
1185 }
1186 switch (inst->GetVerifyTypeArgumentC()) {
1187 case Instruction::kVerifyRegC:
1188 result = result && CheckRegisterIndex(dec_insn.vC_);
1189 break;
1190 case Instruction::kVerifyRegCField:
1191 result = result && CheckFieldIndex(dec_insn.vC_);
1192 break;
1193 case Instruction::kVerifyRegCNewArray:
1194 result = result && CheckNewArray(dec_insn.vC_);
1195 break;
1196 case Instruction::kVerifyRegCType:
1197 result = result && CheckTypeIndex(dec_insn.vC_);
1198 break;
1199 case Instruction::kVerifyRegCWide:
1200 result = result && CheckWideRegisterIndex(dec_insn.vC_);
1201 break;
1202 }
1203 switch (inst->GetVerifyExtraFlags()) {
1204 case Instruction::kVerifyArrayData:
1205 result = result && CheckArrayData(code_offset);
1206 break;
1207 case Instruction::kVerifyBranchTarget:
1208 result = result && CheckBranchTarget(code_offset);
1209 break;
1210 case Instruction::kVerifySwitchTargets:
1211 result = result && CheckSwitchTargets(code_offset);
1212 break;
1213 case Instruction::kVerifyVarArg:
1214 result = result && CheckVarArgRegs(dec_insn.vA_, dec_insn.arg_);
1215 break;
1216 case Instruction::kVerifyVarArgRange:
1217 result = result && CheckVarArgRangeRegs(dec_insn.vA_, dec_insn.vC_);
1218 break;
1219 case Instruction::kVerifyError:
1220 Fail(VERIFY_ERROR_GENERIC) << "unexpected opcode " << inst->Name();
1221 result = false;
1222 break;
1223 }
1224 return result;
1225}
1226
1227bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1228 if (idx >= code_item_->registers_size_) {
1229 Fail(VERIFY_ERROR_GENERIC) << "register index out of range (" << idx << " >= "
1230 << code_item_->registers_size_ << ")";
1231 return false;
1232 }
1233 return true;
1234}
1235
1236bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1237 if (idx + 1 >= code_item_->registers_size_) {
1238 Fail(VERIFY_ERROR_GENERIC) << "wide register index out of range (" << idx
1239 << "+1 >= " << code_item_->registers_size_ << ")";
1240 return false;
1241 }
1242 return true;
1243}
1244
1245bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1246 if (idx >= dex_file_->GetHeader().field_ids_size_) {
1247 Fail(VERIFY_ERROR_GENERIC) << "bad field index " << idx << " (max "
1248 << dex_file_->GetHeader().field_ids_size_ << ")";
1249 return false;
1250 }
1251 return true;
1252}
1253
1254bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1255 if (idx >= dex_file_->GetHeader().method_ids_size_) {
1256 Fail(VERIFY_ERROR_GENERIC) << "bad method index " << idx << " (max "
1257 << dex_file_->GetHeader().method_ids_size_ << ")";
1258 return false;
1259 }
1260 return true;
1261}
1262
1263bool DexVerifier::CheckNewInstance(uint32_t idx) {
1264 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1265 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1266 << dex_file_->GetHeader().type_ids_size_ << ")";
1267 return false;
1268 }
1269 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001270 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 if (descriptor[0] != 'L') {
1272 Fail(VERIFY_ERROR_GENERIC) << "can't call new-instance on type '" << descriptor << "'";
1273 return false;
1274 }
1275 return true;
1276}
1277
1278bool DexVerifier::CheckStringIndex(uint32_t idx) {
1279 if (idx >= dex_file_->GetHeader().string_ids_size_) {
1280 Fail(VERIFY_ERROR_GENERIC) << "bad string index " << idx << " (max "
1281 << dex_file_->GetHeader().string_ids_size_ << ")";
1282 return false;
1283 }
1284 return true;
1285}
1286
1287bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1288 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1289 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1290 << dex_file_->GetHeader().type_ids_size_ << ")";
1291 return false;
1292 }
1293 return true;
1294}
1295
1296bool DexVerifier::CheckNewArray(uint32_t idx) {
1297 if (idx >= dex_file_->GetHeader().type_ids_size_) {
1298 Fail(VERIFY_ERROR_GENERIC) << "bad type index " << idx << " (max "
1299 << dex_file_->GetHeader().type_ids_size_ << ")";
1300 return false;
1301 }
1302 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001303 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 const char* cp = descriptor;
1305 while (*cp++ == '[') {
1306 bracket_count++;
1307 }
1308 if (bracket_count == 0) {
1309 /* The given class must be an array type. */
1310 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (not an array)";
1311 return false;
1312 } else if (bracket_count > 255) {
1313 /* It is illegal to create an array of more than 255 dimensions. */
1314 Fail(VERIFY_ERROR_GENERIC) << "can't new-array class '" << descriptor << "' (exceeds limit)";
1315 return false;
1316 }
1317 return true;
1318}
1319
1320bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1321 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1322 const uint16_t* insns = code_item_->insns_ + cur_offset;
1323 const uint16_t* array_data;
1324 int32_t array_data_offset;
1325
1326 DCHECK_LT(cur_offset, insn_count);
1327 /* make sure the start of the array data table is in range */
1328 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1329 if ((int32_t) cur_offset + array_data_offset < 0 ||
1330 cur_offset + array_data_offset + 2 >= insn_count) {
1331 Fail(VERIFY_ERROR_GENERIC) << "invalid array data start: at " << cur_offset
1332 << ", data offset " << array_data_offset << ", count " << insn_count;
1333 return false;
1334 }
1335 /* offset to array data table is a relative branch-style offset */
1336 array_data = insns + array_data_offset;
1337 /* make sure the table is 32-bit aligned */
1338 if ((((uint32_t) array_data) & 0x03) != 0) {
1339 Fail(VERIFY_ERROR_GENERIC) << "unaligned array data table: at " << cur_offset
1340 << ", data offset " << array_data_offset;
1341 return false;
1342 }
1343 uint32_t value_width = array_data[1];
1344 uint32_t value_count = *(uint32_t*) (&array_data[2]);
1345 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1346 /* make sure the end of the switch is in range */
1347 if (cur_offset + array_data_offset + table_size > insn_count) {
1348 Fail(VERIFY_ERROR_GENERIC) << "invalid array data end: at " << cur_offset
1349 << ", data offset " << array_data_offset << ", end "
1350 << cur_offset + array_data_offset + table_size
1351 << ", count " << insn_count;
1352 return false;
1353 }
1354 return true;
1355}
1356
1357bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1358 int32_t offset;
1359 bool isConditional, selfOkay;
1360 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1361 return false;
1362 }
1363 if (!selfOkay && offset == 0) {
1364 Fail(VERIFY_ERROR_GENERIC) << "branch offset of zero not allowed at" << (void*) cur_offset;
1365 return false;
1366 }
1367 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the VM to have
1368 // identical "wrap-around" behavior, but it's unwise to depend on that.
1369 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
1370 Fail(VERIFY_ERROR_GENERIC) << "branch target overflow " << (void*) cur_offset << " +" << offset;
1371 return false;
1372 }
1373 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1374 int32_t abs_offset = cur_offset + offset;
1375 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1376 Fail(VERIFY_ERROR_GENERIC) << "invalid branch target " << offset << " (-> "
1377 << (void*) abs_offset << ") at " << (void*) cur_offset;
1378 return false;
1379 }
1380 insn_flags_[abs_offset].SetBranchTarget();
1381 return true;
1382}
1383
1384bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1385 bool* selfOkay) {
1386 const uint16_t* insns = code_item_->insns_ + cur_offset;
1387 *pConditional = false;
1388 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001389 switch (*insns & 0xff) {
1390 case Instruction::GOTO:
1391 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001392 break;
1393 case Instruction::GOTO_32:
1394 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 *selfOkay = true;
1396 break;
1397 case Instruction::GOTO_16:
1398 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 break;
1400 case Instruction::IF_EQ:
1401 case Instruction::IF_NE:
1402 case Instruction::IF_LT:
1403 case Instruction::IF_GE:
1404 case Instruction::IF_GT:
1405 case Instruction::IF_LE:
1406 case Instruction::IF_EQZ:
1407 case Instruction::IF_NEZ:
1408 case Instruction::IF_LTZ:
1409 case Instruction::IF_GEZ:
1410 case Instruction::IF_GTZ:
1411 case Instruction::IF_LEZ:
1412 *pOffset = (int16_t) insns[1];
1413 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 break;
1415 default:
1416 return false;
1417 break;
1418 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001419 return true;
1420}
1421
Ian Rogersd81871c2011-10-03 13:57:23 -07001422bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1423 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001424 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001426 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1428 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
1429 Fail(VERIFY_ERROR_GENERIC) << "invalid switch start: at " << cur_offset
1430 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 return false;
1432 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 /* make sure the table is 32-bit aligned */
1436 if ((((uint32_t) switch_insns) & 0x03) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 Fail(VERIFY_ERROR_GENERIC) << "unaligned switch table: at " << cur_offset
1438 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 return false;
1440 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 uint32_t switch_count = switch_insns[1];
1442 int32_t keys_offset, targets_offset;
1443 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001444 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1445 /* 0=sig, 1=count, 2/3=firstKey */
1446 targets_offset = 4;
1447 keys_offset = -1;
1448 expected_signature = Instruction::kPackedSwitchSignature;
1449 } else {
1450 /* 0=sig, 1=count, 2..count*2 = keys */
1451 keys_offset = 2;
1452 targets_offset = 2 + 2 * switch_count;
1453 expected_signature = Instruction::kSparseSwitchSignature;
1454 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 if (switch_insns[0] != expected_signature) {
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001457 Fail(VERIFY_ERROR_GENERIC) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1458 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 return false;
1460 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 /* make sure the end of the switch is in range */
1462 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 Fail(VERIFY_ERROR_GENERIC) << "invalid switch end: at " << cur_offset << ", switch offset "
1464 << switch_offset << ", end "
1465 << (cur_offset + switch_offset + table_size)
1466 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 return false;
1468 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001469 /* for a sparse switch, verify the keys are in ascending order */
1470 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1472 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001473 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1474 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1475 if (key <= last_key) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 Fail(VERIFY_ERROR_GENERIC) << "invalid packed switch: last key=" << last_key
1477 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 return false;
1479 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 last_key = key;
1481 }
1482 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001483 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 for (uint32_t targ = 0; targ < switch_count; targ++) {
1485 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1486 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1487 int32_t abs_offset = cur_offset + offset;
1488 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
1489 Fail(VERIFY_ERROR_GENERIC) << "invalid switch target " << offset << " (-> "
1490 << (void*) abs_offset << ") at "
1491 << (void*) cur_offset << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001492 return false;
1493 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 insn_flags_[abs_offset].SetBranchTarget();
1495 }
1496 return true;
1497}
1498
1499bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1500 if (vA > 5) {
1501 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << vA << ") in non-range invoke)";
1502 return false;
1503 }
1504 uint16_t registers_size = code_item_->registers_size_;
1505 for (uint32_t idx = 0; idx < vA; idx++) {
1506 if (arg[idx] > registers_size) {
1507 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index (" << arg[idx]
1508 << ") in non-range invoke (> " << registers_size << ")";
1509 return false;
1510 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001511 }
1512
1513 return true;
1514}
1515
Ian Rogersd81871c2011-10-03 13:57:23 -07001516bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1517 uint16_t registers_size = code_item_->registers_size_;
1518 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1519 // integer overflow when adding them here.
1520 if (vA + vC > registers_size) {
1521 Fail(VERIFY_ERROR_GENERIC) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1522 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 return false;
1524 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001525 return true;
1526}
1527
Brian Carlstrom75412882012-01-18 01:26:54 -08001528const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1529 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1530 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1531 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1532 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1533 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1534 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1535 gc_map.begin(),
1536 gc_map.end());
1537 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1538 DCHECK_EQ(gc_map.size(),
1539 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1540 (length_prefixed_gc_map->at(1) << 16) |
1541 (length_prefixed_gc_map->at(2) << 8) |
1542 (length_prefixed_gc_map->at(3) << 0)));
1543 return length_prefixed_gc_map;
1544}
1545
Ian Rogersd81871c2011-10-03 13:57:23 -07001546bool DexVerifier::VerifyCodeFlow() {
1547 uint16_t registers_size = code_item_->registers_size_;
1548 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001549
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 if (registers_size * insns_size > 4*1024*1024) {
1551 Fail(VERIFY_ERROR_GENERIC) << "warning: method is huge (regs=" << registers_size
1552 << " insns_size=" << insns_size << ")";
1553 }
1554 /* Create and initialize table holding register status */
1555 reg_table_.Init(PcToRegisterLineTable::kTrackRegsGcPoints, insn_flags_.get(), insns_size,
1556 registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001557
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 work_line_.reset(new RegisterLine(registers_size, this));
1559 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001560
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 /* Initialize register types of method arguments. */
1562 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001563 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1564 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 return false;
1566 }
1567 /* Perform code flow verification. */
1568 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001569 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001571 }
1572
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001574 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1575 if (map.get() == NULL) {
1576 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 return false; // Not a real failure, but a failure to encode
1578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001579#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001580 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001581#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001582 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1583 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1584 verifier::DexVerifier::SetGcMap(ref, *gc_map);
1585 method_->SetGcMap(&gc_map->at(0));
jeffhaobdb76512011-09-07 11:43:16 -07001586 return true;
1587}
1588
Ian Rogersd81871c2011-10-03 13:57:23 -07001589void DexVerifier::Dump(std::ostream& os) {
1590 if (method_->IsNative()) {
1591 os << "Native method" << std::endl;
1592 return;
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 DCHECK(code_item_ != NULL);
1595 const Instruction* inst = Instruction::At(code_item_->insns_);
1596 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1597 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001598 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001599 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1601 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001602 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001603 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001604 inst = inst->Next();
1605 }
jeffhaobdb76512011-09-07 11:43:16 -07001606}
1607
Ian Rogersd81871c2011-10-03 13:57:23 -07001608static bool IsPrimitiveDescriptor(char descriptor) {
1609 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001610 case 'I':
1611 case 'C':
1612 case 'S':
1613 case 'B':
1614 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001615 case 'F':
1616 case 'D':
1617 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001619 default:
1620 return false;
1621 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001622}
1623
Ian Rogersd81871c2011-10-03 13:57:23 -07001624bool DexVerifier::SetTypesFromSignature() {
1625 RegisterLine* reg_line = reg_table_.GetLine(0);
1626 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1627 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001628
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1630 //Include the "this" pointer.
1631 size_t cur_arg = 0;
1632 if (!method_->IsStatic()) {
1633 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1634 // argument as uninitialized. This restricts field access until the superclass constructor is
1635 // called.
1636 Class* declaring_class = method_->GetDeclaringClass();
1637 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1638 reg_line->SetRegisterType(arg_start + cur_arg,
1639 reg_types_.UninitializedThisArgument(declaring_class));
1640 } else {
1641 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001644 }
1645
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001646 const DexFile::ProtoId& proto_id =
1647 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001648 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001649
1650 for (; iterator.HasNext(); iterator.Next()) {
1651 const char* descriptor = iterator.GetDescriptor();
1652 if (descriptor == NULL) {
1653 LOG(FATAL) << "Null descriptor";
1654 }
1655 if (cur_arg >= expected_args) {
1656 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args
1657 << " args, found more (" << descriptor << ")";
1658 return false;
1659 }
1660 switch (descriptor[0]) {
1661 case 'L':
1662 case '[':
1663 // We assume that reference arguments are initialized. The only way it could be otherwise
1664 // (assuming the caller was verified) is if the current method is <init>, but in that case
1665 // it's effectively considered initialized the instant we reach here (in the sense that we
1666 // can return without doing anything or call virtual methods).
1667 {
1668 const RegType& reg_type =
1669 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001670 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 }
1672 break;
1673 case 'Z':
1674 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1675 break;
1676 case 'C':
1677 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1678 break;
1679 case 'B':
1680 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1681 break;
1682 case 'I':
1683 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1684 break;
1685 case 'S':
1686 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1687 break;
1688 case 'F':
1689 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1690 break;
1691 case 'J':
1692 case 'D': {
1693 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1694 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1695 cur_arg++;
1696 break;
1697 }
1698 default:
1699 Fail(VERIFY_ERROR_GENERIC) << "unexpected signature type char '" << descriptor << "'";
1700 return false;
1701 }
1702 cur_arg++;
1703 }
1704 if (cur_arg != expected_args) {
1705 Fail(VERIFY_ERROR_GENERIC) << "expected " << expected_args << " arguments, found " << cur_arg;
1706 return false;
1707 }
1708 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1709 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1710 // format. Only major difference from the method argument format is that 'V' is supported.
1711 bool result;
1712 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1713 result = descriptor[1] == '\0';
1714 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1715 size_t i = 0;
1716 do {
1717 i++;
1718 } while (descriptor[i] == '['); // process leading [
1719 if (descriptor[i] == 'L') { // object array
1720 do {
1721 i++; // find closing ;
1722 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1723 result = descriptor[i] == ';';
1724 } else { // primitive array
1725 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1726 }
1727 } else if (descriptor[0] == 'L') {
1728 // could be more thorough here, but shouldn't be required
1729 size_t i = 0;
1730 do {
1731 i++;
1732 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1733 result = descriptor[i] == ';';
1734 } else {
1735 result = false;
1736 }
1737 if (!result) {
1738 Fail(VERIFY_ERROR_GENERIC) << "unexpected char in return type descriptor '"
1739 << descriptor << "'";
1740 }
1741 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001742}
1743
Ian Rogersd81871c2011-10-03 13:57:23 -07001744bool DexVerifier::CodeFlowVerifyMethod() {
1745 const uint16_t* insns = code_item_->insns_;
1746 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001747
jeffhaobdb76512011-09-07 11:43:16 -07001748 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 insn_flags_[0].SetChanged();
1750 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001751
jeffhaobdb76512011-09-07 11:43:16 -07001752 /* Continue until no instructions are marked "changed". */
1753 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1755 uint32_t insn_idx = start_guess;
1756 for (; insn_idx < insns_size; insn_idx++) {
1757 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
1759 }
jeffhaobdb76512011-09-07 11:43:16 -07001760 if (insn_idx == insns_size) {
1761 if (start_guess != 0) {
1762 /* try again, starting from the top */
1763 start_guess = 0;
1764 continue;
1765 } else {
1766 /* all flags are clear */
1767 break;
1768 }
1769 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 // We carry the working set of registers from instruction to instruction. If this address can
1771 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1772 // "changed" flags, we need to load the set of registers from the table.
1773 // Because we always prefer to continue on to the next instruction, we should never have a
1774 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1775 // target.
1776 work_insn_idx_ = insn_idx;
1777 if (insn_flags_[insn_idx].IsBranchTarget()) {
1778 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001779 } else {
1780#ifndef NDEBUG
1781 /*
1782 * Sanity check: retrieve the stored register line (assuming
1783 * a full table) and make sure it actually matches.
1784 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1786 if (register_line != NULL) {
1787 if (work_line_->CompareLine(register_line) != 0) {
1788 Dump(std::cout);
1789 std::cout << info_messages_.str();
1790 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
1791 << "@" << (void*)work_insn_idx_ << std::endl
1792 << " work_line=" << *work_line_ << std::endl
1793 << " expected=" << *register_line;
1794 }
jeffhaobdb76512011-09-07 11:43:16 -07001795 }
1796#endif
1797 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 if (!CodeFlowVerifyInstruction(&start_guess)) {
1799 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001800 return false;
1801 }
jeffhaobdb76512011-09-07 11:43:16 -07001802 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 insn_flags_[insn_idx].SetVisited();
1804 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001805 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001806
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001808 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001809 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001810 * (besides the wasted space), but it indicates a flaw somewhere
1811 * down the line, possibly in the verifier.
1812 *
1813 * If we've substituted "always throw" instructions into the stream,
1814 * we are almost certainly going to have some dead code.
1815 */
1816 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 uint32_t insn_idx = 0;
1818 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001819 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001820 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001821 * may or may not be preceded by a padding NOP (for alignment).
1822 */
1823 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1824 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1825 insns[insn_idx] == Instruction::kArrayDataSignature ||
1826 (insns[insn_idx] == Instruction::NOP &&
1827 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1828 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1829 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001831 }
1832
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001834 if (dead_start < 0)
1835 dead_start = insn_idx;
1836 } else if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001838 dead_start = -1;
1839 }
1840 }
1841 if (dead_start >= 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 LogVerifyInfo() << "dead code " << (void*) dead_start << "-" << (void*) (insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001843 }
1844 }
jeffhaobdb76512011-09-07 11:43:16 -07001845 return true;
1846}
1847
Ian Rogersd81871c2011-10-03 13:57:23 -07001848bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001849#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001851 gDvm.verifierStats.instrsReexamined++;
1852 } else {
1853 gDvm.verifierStats.instrsExamined++;
1854 }
1855#endif
1856
1857 /*
1858 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001860 * control to another statement:
1861 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001862 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001863 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001864 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001865 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001866 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001867 * throw an exception that is handled by an encompassing "try"
1868 * block.
1869 *
1870 * We can also return, in which case there is no successor instruction
1871 * from this point.
1872 *
1873 * The behavior can be determined from the OpcodeFlags.
1874 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1876 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001877 Instruction::DecodedInstruction dec_insn(inst);
1878 int opcode_flag = inst->Flag();
1879
jeffhaobdb76512011-09-07 11:43:16 -07001880 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001881 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001882 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07001884 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1885 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 }
jeffhaobdb76512011-09-07 11:43:16 -07001887
1888 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001889 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001890 * can throw an exception, we will copy/merge this into the "catch"
1891 * address rather than work_line, because we don't want the result
1892 * from the "successful" code path (e.g. a check-cast that "improves"
1893 * a type) to be visible to the exception handler.
1894 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 if ((opcode_flag & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
1896 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001897 } else {
1898#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001900#endif
1901 }
1902
1903 switch (dec_insn.opcode_) {
1904 case Instruction::NOP:
1905 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001906 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001907 * a signature that looks like a NOP; if we see one of these in
1908 * the course of executing code then we have a problem.
1909 */
1910 if (dec_insn.vA_ != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 Fail(VERIFY_ERROR_GENERIC) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001912 }
1913 break;
1914
1915 case Instruction::MOVE:
1916 case Instruction::MOVE_FROM16:
1917 case Instruction::MOVE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
1920 case Instruction::MOVE_WIDE:
1921 case Instruction::MOVE_WIDE_FROM16:
1922 case Instruction::MOVE_WIDE_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 work_line_->CopyRegister2(dec_insn.vA_, dec_insn.vB_);
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
1925 case Instruction::MOVE_OBJECT:
1926 case Instruction::MOVE_OBJECT_FROM16:
1927 case Instruction::MOVE_OBJECT_16:
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 work_line_->CopyRegister1(dec_insn.vA_, dec_insn.vB_, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001929 break;
1930
1931 /*
1932 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001933 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001934 * might want to hold the result in an actual CPU register, so the
1935 * Dalvik spec requires that these only appear immediately after an
1936 * invoke or filled-new-array.
1937 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001938 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001939 * redundant with the reset done below, but it can make the debug info
1940 * easier to read in some cases.)
1941 */
1942 case Instruction::MOVE_RESULT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 work_line_->CopyResultRegister1(dec_insn.vA_, false);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945 case Instruction::MOVE_RESULT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 work_line_->CopyResultRegister2(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 work_line_->CopyResultRegister1(dec_insn.vA_, true);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001953 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 * This statement can only appear as the first instruction in an exception handler (though not
1955 * all exception handlers need to have one of these). We verify that as part of extracting the
jeffhaobdb76512011-09-07 11:43:16 -07001956 * exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001957 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001958 const RegType& res_type = GetCaughtExceptionType();
1959 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001960 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 }
jeffhaobdb76512011-09-07 11:43:16 -07001962 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
1964 if (!GetMethodReturnType().IsUnknown()) {
1965 Fail(VERIFY_ERROR_GENERIC) << "return-void not expected";
1966 }
jeffhaobdb76512011-09-07 11:43:16 -07001967 }
1968 break;
1969 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001971 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 const RegType& return_type = GetMethodReturnType();
1973 if (!return_type.IsCategory1Types()) {
1974 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-category 1 return type " << return_type;
1975 } else {
1976 // Compilers may generate synthetic functions that write byte values into boolean fields.
1977 // Also, it may use integer values for boolean, byte, short, and character return types.
1978 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA_);
1979 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1980 ((return_type.IsBoolean() || return_type.IsByte() ||
1981 return_type.IsShort() || return_type.IsChar()) &&
1982 src_type.IsInteger()));
1983 /* check the register contents */
1984 work_line_->VerifyRegisterType(dec_insn.vA_, use_src ? src_type : return_type);
1985 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07001986 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
jeffhaobdb76512011-09-07 11:43:16 -07001988 }
1989 }
1990 break;
1991 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001993 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 const RegType& return_type = GetMethodReturnType();
1995 if (!return_type.IsCategory2Types()) {
1996 Fail(VERIFY_ERROR_GENERIC) << "return-wide not expected";
1997 } else {
1998 /* check the register contents */
1999 work_line_->VerifyRegisterType(dec_insn.vA_, return_type);
2000 if (failure_ != VERIFY_ERROR_NONE) {
Ian Rogers84fa0742011-10-25 18:13:30 -07002001 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 }
jeffhaobdb76512011-09-07 11:43:16 -07002003 }
2004 }
2005 break;
2006 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2008 const RegType& return_type = GetMethodReturnType();
2009 if (!return_type.IsReferenceTypes()) {
2010 Fail(VERIFY_ERROR_GENERIC) << "return-object not expected";
2011 } else {
2012 /* return_type is the *expected* return type, not register value */
2013 DCHECK(!return_type.IsZero());
2014 DCHECK(!return_type.IsUninitializedReference());
Ian Rogers9074b992011-10-26 17:41:55 -07002015 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2016 // Disallow returning uninitialized values and verify that the reference in vAA is an
2017 // instance of the "return_type"
2018 if (reg_type.IsUninitializedTypes()) {
2019 Fail(VERIFY_ERROR_GENERIC) << "returning uninitialized object '" << reg_type << "'";
2020 } else if (!return_type.IsAssignableFrom(reg_type)) {
2021 Fail(VERIFY_ERROR_GENERIC) << "returning '" << reg_type
2022 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002023 }
2024 }
2025 }
2026 break;
2027
2028 case Instruction::CONST_4:
2029 case Instruction::CONST_16:
2030 case Instruction::CONST:
2031 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.FromCat1Const((int32_t) dec_insn.vB_));
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::CONST_HIGH16:
2035 /* could be boolean, int, float, or a null reference */
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 work_line_->SetRegisterType(dec_insn.vA_,
2037 reg_types_.FromCat1Const((int32_t) dec_insn.vB_ << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::CONST_WIDE_16:
2040 case Instruction::CONST_WIDE_32:
2041 case Instruction::CONST_WIDE:
2042 case Instruction::CONST_WIDE_HIGH16:
2043 /* could be long or double; resolved upon use */
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::CONST_STRING:
2047 case Instruction::CONST_STRING_JUMBO:
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002051 // Get type from instruction if unresolved then we need an access check
2052 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2053 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2054 // Register holds class, ie its type is class, but on error we keep it Unknown
2055 work_line_->SetRegisterType(dec_insn.vA_,
2056 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 }
jeffhaobdb76512011-09-07 11:43:16 -07002059 case Instruction::MONITOR_ENTER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 work_line_->PushMonitor(dec_insn.vA_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::MONITOR_EXIT:
2063 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002064 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002065 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002067 * to the need to handle asynchronous exceptions, a now-deprecated
2068 * feature that Dalvik doesn't support.)
2069 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002070 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002071 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002072 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002073 * structured locking checks are working, the former would have
2074 * failed on the -enter instruction, and the latter is impossible.
2075 *
2076 * This is fortunate, because issue 3221411 prevents us from
2077 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002078 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002079 * some catch blocks (which will show up as "dead" code when
2080 * we skip them here); if we can't, then the code path could be
2081 * "live" so we still need to check it.
2082 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 opcode_flag &= ~Instruction::kThrow;
2084 work_line_->PopMonitor(dec_insn.vA_);
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086
Ian Rogers28ad40d2011-10-27 15:19:26 -07002087 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002089 /*
2090 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2091 * could be a "upcast" -- not expected, so we don't try to address it.)
2092 *
2093 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
2094 * dec_insn.vA_ when branching to a handler.
2095 */
2096 bool is_checkcast = dec_insn.opcode_ == Instruction::CHECK_CAST;
2097 const RegType& res_type =
2098 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB_ : dec_insn.vC_);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002099 if (res_type.IsUnknown()) {
2100 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2101 break; // couldn't resolve class
2102 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002103 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2104 const RegType& orig_type =
2105 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA_ : dec_insn.vB_);
2106 if (!res_type.IsNonZeroReferenceTypes()) {
2107 Fail(VERIFY_ERROR_GENERIC) << "check-cast on unexpected class " << res_type;
2108 } else if (!orig_type.IsReferenceTypes()) {
2109 Fail(VERIFY_ERROR_GENERIC) << "check-cast on non-reference in v" << dec_insn.vA_;
jeffhao2a8a90e2011-09-26 14:25:31 -07002110 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002111 if (is_checkcast) {
2112 work_line_->SetRegisterType(dec_insn.vA_, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002115 }
jeffhaobdb76512011-09-07 11:43:16 -07002116 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002117 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 }
2119 case Instruction::ARRAY_LENGTH: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002120 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB_);
2121 if (res_type.IsReferenceTypes()) {
Ian Rogers90f2b302011-10-29 15:05:54 -07002122 if (!res_type.IsArrayClass() && !res_type.IsZero()) { // ie not an array or null
Ian Rogers28ad40d2011-10-27 15:19:26 -07002123 Fail(VERIFY_ERROR_GENERIC) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 } else {
2125 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
2126 }
2127 }
2128 break;
2129 }
2130 case Instruction::NEW_INSTANCE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002131 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2132 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2133 // can't create an instance of an interface or abstract class */
2134 if (!res_type.IsInstantiableTypes()) {
2135 Fail(VERIFY_ERROR_INSTANTIATION)
2136 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002138 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2139 // Any registers holding previous allocations from this address that have not yet been
2140 // initialized must be marked invalid.
2141 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2142 // add the new uninitialized reference to the register state
2143 work_line_->SetRegisterType(dec_insn.vA_, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 }
2145 break;
2146 }
2147 case Instruction::NEW_ARRAY: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002148 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vC_);
2149 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2150 if (!res_type.IsArrayClass()) {
2151 Fail(VERIFY_ERROR_GENERIC) << "new-array on non-array class " << res_type;
jeffhaobdb76512011-09-07 11:43:16 -07002152 } else {
2153 /* make sure "size" register is valid type */
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002155 /* set register type to array class */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002156 work_line_->SetRegisterType(dec_insn.vA_, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002157 }
2158 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 }
jeffhaobdb76512011-09-07 11:43:16 -07002160 case Instruction::FILLED_NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002162 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB_);
2163 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2164 if (!res_type.IsArrayClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array on non-array class";
jeffhaobdb76512011-09-07 11:43:16 -07002166 } else {
jeffhaoe0cfb6f2011-09-22 16:42:56 -07002167 bool is_range = (dec_insn.opcode_ == Instruction::FILLED_NEW_ARRAY_RANGE);
jeffhaobdb76512011-09-07 11:43:16 -07002168 /* check the arguments to the instruction */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002169 VerifyFilledNewArrayRegs(dec_insn, res_type, is_range);
jeffhaobdb76512011-09-07 11:43:16 -07002170 /* filled-array result goes into "result" register */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002171 work_line_->SetResultRegisterType(res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002172 just_set_result = true;
2173 }
2174 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 }
jeffhaobdb76512011-09-07 11:43:16 -07002176 case Instruction::CMPL_FLOAT:
2177 case Instruction::CMPG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Float());
2179 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Float());
2180 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::CMPL_DOUBLE:
2183 case Instruction::CMPG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Double());
2185 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Double());
2186 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
2188 case Instruction::CMP_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 work_line_->VerifyRegisterType(dec_insn.vB_, reg_types_.Long());
2190 work_line_->VerifyRegisterType(dec_insn.vC_, reg_types_.Long());
2191 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 case Instruction::THROW: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002194 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA_);
2195 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
2196 Fail(VERIFY_ERROR_GENERIC) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002197 }
2198 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 case Instruction::GOTO:
2201 case Instruction::GOTO_16:
2202 case Instruction::GOTO_32:
2203 /* no effect on or use of registers */
2204 break;
2205
2206 case Instruction::PACKED_SWITCH:
2207 case Instruction::SPARSE_SWITCH:
2208 /* verify that vAA is an integer, or can be converted to one */
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 work_line_->VerifyRegisterType(dec_insn.vA_, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 case Instruction::FILL_ARRAY_DATA: {
2213 /* Similar to the verification done for APUT */
2214 Class* res_class = work_line_->GetClassFromRegister(dec_insn.vA_);
2215 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002216 /* res_class can be null if the reg type is Zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 if (res_class != NULL) {
2218 Class* component_type = res_class->GetComponentType();
2219 if (!res_class->IsArrayClass() || !component_type->IsPrimitive() ||
2220 component_type->IsPrimitiveVoid()) {
2221 Fail(VERIFY_ERROR_GENERIC) << "invalid fill-array-data on "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002222 << PrettyDescriptor(res_class);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 } else {
2224 const RegType& value_type = reg_types_.FromClass(component_type);
2225 DCHECK(!value_type.IsUnknown());
2226 // Now verify if the element width in the table matches the element width declared in
2227 // the array
2228 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2229 if (array_data[0] != Instruction::kArrayDataSignature) {
2230 Fail(VERIFY_ERROR_GENERIC) << "invalid magic for array-data";
2231 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002232 size_t elem_width = Primitive::ComponentSize(component_type->GetPrimitiveType());
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 // Since we don't compress the data in Dex, expect to see equal width of data stored
2234 // in the table and expected from the array class.
2235 if (array_data[1] != elem_width) {
2236 Fail(VERIFY_ERROR_GENERIC) << "array-data size mismatch (" << array_data[1]
2237 << " vs " << elem_width << ")";
2238 }
2239 }
2240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 }
2242 }
2243 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 }
jeffhaobdb76512011-09-07 11:43:16 -07002245 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 case Instruction::IF_NE: {
2247 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2248 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2249 bool mismatch = false;
2250 if (reg_type1.IsZero()) { // zero then integral or reference expected
2251 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2252 } else if (reg_type1.IsReferenceTypes()) { // both references?
2253 mismatch = !reg_type2.IsReferenceTypes();
2254 } else { // both integral?
2255 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2256 }
2257 if (mismatch) {
2258 Fail(VERIFY_ERROR_GENERIC) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2259 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002260 }
2261 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 }
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::IF_LT:
2264 case Instruction::IF_GE:
2265 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::IF_LE: {
2267 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA_);
2268 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB_);
2269 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
2270 Fail(VERIFY_ERROR_GENERIC) << "args to 'if' (" << reg_type1 << ","
2271 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002272 }
2273 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 }
jeffhaobdb76512011-09-07 11:43:16 -07002275 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 case Instruction::IF_NEZ: {
2277 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2278 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
2279 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
2280 }
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 }
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IF_LTZ:
2284 case Instruction::IF_GEZ:
2285 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 case Instruction::IF_LEZ: {
2287 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA_);
2288 if (!reg_type.IsIntegralTypes()) {
2289 Fail(VERIFY_ERROR_GENERIC) << "type " << reg_type
2290 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
2291 }
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 }
jeffhaobdb76512011-09-07 11:43:16 -07002294 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2296 break;
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 VerifyAGet(dec_insn, reg_types_.Char(), true);
2302 break;
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 case Instruction::AGET:
2307 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2308 break;
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 VerifyAGet(dec_insn, reg_types_.Long(), true);
2311 break;
2312 case Instruction::AGET_OBJECT:
2313 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 case Instruction::APUT_BOOLEAN:
2317 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2318 break;
2319 case Instruction::APUT_BYTE:
2320 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2321 break;
2322 case Instruction::APUT_CHAR:
2323 VerifyAPut(dec_insn, reg_types_.Char(), true);
2324 break;
2325 case Instruction::APUT_SHORT:
2326 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337
jeffhaobdb76512011-09-07 11:43:16 -07002338 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002339 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 break;
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002342 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 break;
jeffhaobdb76512011-09-07 11:43:16 -07002344 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002345 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002348 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 break;
2350 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002351 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002354 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002357 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 break;
jeffhaobdb76512011-09-07 11:43:16 -07002359
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002361 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
2363 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002364 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
2366 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002367 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 break;
2369 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002370 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002373 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002376 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
jeffhaobdb76512011-09-07 11:43:16 -07002378 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002379 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381
jeffhaobdb76512011-09-07 11:43:16 -07002382 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002383 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
jeffhaobdb76512011-09-07 11:43:16 -07002385 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002386 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
jeffhaobdb76512011-09-07 11:43:16 -07002388 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002389 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 break;
jeffhaobdb76512011-09-07 11:43:16 -07002391 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002392 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 break;
2394 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002395 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002398 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002401 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 break;
2403
2404 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002405 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 break;
2407 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002408 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 break;
2410 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002411 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 break;
2413 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002414 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
2416 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002417 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002420 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002423 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425
2426 case Instruction::INVOKE_VIRTUAL:
2427 case Instruction::INVOKE_VIRTUAL_RANGE:
2428 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 case Instruction::INVOKE_SUPER_RANGE: {
2430 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_VIRTUAL_RANGE ||
2431 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2432 bool is_super = (dec_insn.opcode_ == Instruction::INVOKE_SUPER ||
2433 dec_insn.opcode_ == Instruction::INVOKE_SUPER_RANGE);
2434 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2435 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002436 const char* descriptor;
2437 if (called_method == NULL) {
2438 uint32_t method_idx = dec_insn.vB_;
2439 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2440 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002441 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002442 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002443 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002444 }
Ian Rogers9074b992011-10-26 17:41:55 -07002445 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002446 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002448 just_set_result = true;
2449 }
2450 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 }
jeffhaobdb76512011-09-07 11:43:16 -07002452 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 case Instruction::INVOKE_DIRECT_RANGE: {
2454 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_DIRECT_RANGE);
2455 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2456 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002457 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 * Some additional checks when calling a constructor. We know from the invocation arg check
2459 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2460 * that to require that called_method->klass is the same as this->klass or this->super,
2461 * allowing the latter only if the "this" argument is the same as the "this" argument to
2462 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002463 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 bool is_constructor;
2465 if (called_method != NULL) {
2466 is_constructor = called_method->IsConstructor();
2467 } else {
2468 uint32_t method_idx = dec_insn.vB_;
2469 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2470 const char* name = dex_file_->GetMethodName(method_id);
2471 is_constructor = strcmp(name, "<init>") == 0;
2472 }
2473 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2475 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477
2478 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 if (this_type.IsZero()) {
2480 Fail(VERIFY_ERROR_GENERIC) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 if (called_method != NULL) {
2484 Class* this_class = this_type.GetClass();
2485 DCHECK(this_class != NULL);
2486 /* must be in same class or in superclass */
2487 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2488 if (this_class != method_->GetDeclaringClass()) {
2489 Fail(VERIFY_ERROR_GENERIC)
2490 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2491 break;
2492 }
2493 } else if (called_method->GetDeclaringClass() != this_class) {
2494 Fail(VERIFY_ERROR_GENERIC) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 }
jeffhaobdb76512011-09-07 11:43:16 -07002497 }
2498
2499 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002500 if (!this_type.IsUninitializedTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 Fail(VERIFY_ERROR_GENERIC) << "Expected initialization on uninitialized reference "
2502 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 }
2505
2506 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002507 * Replace the uninitialized reference with an initialized one. We need to do this for all
2508 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002509 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 work_line_->MarkRefsAsInitialized(this_type);
2511 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002512 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002513 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002514 const char* descriptor;
2515 if (called_method == NULL) {
2516 uint32_t method_idx = dec_insn.vB_;
2517 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2518 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002519 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002520 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002521 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002522 }
Ian Rogers9074b992011-10-26 17:41:55 -07002523 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002524 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002526 just_set_result = true;
2527 }
2528 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 }
jeffhaobdb76512011-09-07 11:43:16 -07002530 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 case Instruction::INVOKE_STATIC_RANGE: {
2532 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_STATIC_RANGE);
2533 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2534 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002535 const char* descriptor;
2536 if (called_method == NULL) {
2537 uint32_t method_idx = dec_insn.vB_;
2538 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2539 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002540 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002541 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002542 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002543 }
Ian Rogers9074b992011-10-26 17:41:55 -07002544 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002545 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 work_line_->SetResultRegisterType(return_type);
2547 just_set_result = true;
2548 }
jeffhaobdb76512011-09-07 11:43:16 -07002549 }
2550 break;
2551 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 case Instruction::INVOKE_INTERFACE_RANGE: {
2553 bool is_range = (dec_insn.opcode_ == Instruction::INVOKE_INTERFACE_RANGE);
2554 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2555 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002556 if (abs_method != NULL) {
2557 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002558 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002559 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2560 << PrettyMethod(abs_method) << "'";
2561 break;
2562 }
2563 }
2564 /* Get the type of the "this" arg, which should either be a sub-interface of called
2565 * interface or Object (see comments in RegType::JoinClass).
2566 */
2567 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2568 if (failure_ == VERIFY_ERROR_NONE) {
2569 if (this_type.IsZero()) {
2570 /* null pointer always passes (and always fails at runtime) */
2571 } else {
2572 if (this_type.IsUninitializedTypes()) {
2573 Fail(VERIFY_ERROR_GENERIC) << "interface call on uninitialized object "
2574 << this_type;
2575 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002577 // In the past we have tried to assert that "called_interface" is assignable
2578 // from "this_type.GetClass()", however, as we do an imprecise Join
2579 // (RegType::JoinClass) we don't have full information on what interfaces are
2580 // implemented by "this_type". For example, two classes may implement the same
2581 // interfaces and have a common parent that doesn't implement the interface. The
2582 // join will set "this_type" to the parent class and a test that this implements
2583 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002584 }
2585 }
jeffhaobdb76512011-09-07 11:43:16 -07002586 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 * We don't have an object instance, so we can't find the concrete method. However, all of
2588 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002589 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002590 const char* descriptor;
2591 if (abs_method == NULL) {
2592 uint32_t method_idx = dec_insn.vB_;
2593 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2594 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002595 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002596 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002597 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002598 }
Ian Rogers9074b992011-10-26 17:41:55 -07002599 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002600 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2601 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002603 just_set_result = true;
2604 }
2605 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 }
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::NEG_INT:
2608 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::NEG_LONG:
2612 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666
2667 case Instruction::ADD_INT:
2668 case Instruction::SUB_INT:
2669 case Instruction::MUL_INT:
2670 case Instruction::REM_INT:
2671 case Instruction::DIV_INT:
2672 case Instruction::SHL_INT:
2673 case Instruction::SHR_INT:
2674 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::AND_INT:
2678 case Instruction::OR_INT:
2679 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
2682 case Instruction::ADD_LONG:
2683 case Instruction::SUB_LONG:
2684 case Instruction::MUL_LONG:
2685 case Instruction::DIV_LONG:
2686 case Instruction::REM_LONG:
2687 case Instruction::AND_LONG:
2688 case Instruction::OR_LONG:
2689 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002691 break;
2692 case Instruction::SHL_LONG:
2693 case Instruction::SHR_LONG:
2694 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 /* shift distance is Int, making these different from other binary operations */
2696 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::ADD_FLOAT:
2699 case Instruction::SUB_FLOAT:
2700 case Instruction::MUL_FLOAT:
2701 case Instruction::DIV_FLOAT:
2702 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::ADD_DOUBLE:
2706 case Instruction::SUB_DOUBLE:
2707 case Instruction::MUL_DOUBLE:
2708 case Instruction::DIV_DOUBLE:
2709 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::ADD_INT_2ADDR:
2713 case Instruction::SUB_INT_2ADDR:
2714 case Instruction::MUL_INT_2ADDR:
2715 case Instruction::REM_INT_2ADDR:
2716 case Instruction::SHL_INT_2ADDR:
2717 case Instruction::SHR_INT_2ADDR:
2718 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002720 break;
2721 case Instruction::AND_INT_2ADDR:
2722 case Instruction::OR_INT_2ADDR:
2723 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::ADD_LONG_2ADDR:
2730 case Instruction::SUB_LONG_2ADDR:
2731 case Instruction::MUL_LONG_2ADDR:
2732 case Instruction::DIV_LONG_2ADDR:
2733 case Instruction::REM_LONG_2ADDR:
2734 case Instruction::AND_LONG_2ADDR:
2735 case Instruction::OR_LONG_2ADDR:
2736 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::SHL_LONG_2ADDR:
2740 case Instruction::SHR_LONG_2ADDR:
2741 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744 case Instruction::ADD_FLOAT_2ADDR:
2745 case Instruction::SUB_FLOAT_2ADDR:
2746 case Instruction::MUL_FLOAT_2ADDR:
2747 case Instruction::DIV_FLOAT_2ADDR:
2748 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751 case Instruction::ADD_DOUBLE_2ADDR:
2752 case Instruction::SUB_DOUBLE_2ADDR:
2753 case Instruction::MUL_DOUBLE_2ADDR:
2754 case Instruction::DIV_DOUBLE_2ADDR:
2755 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::ADD_INT_LIT16:
2759 case Instruction::RSUB_INT:
2760 case Instruction::MUL_INT_LIT16:
2761 case Instruction::DIV_INT_LIT16:
2762 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002764 break;
2765 case Instruction::AND_INT_LIT16:
2766 case Instruction::OR_INT_LIT16:
2767 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::ADD_INT_LIT8:
2771 case Instruction::RSUB_INT_LIT8:
2772 case Instruction::MUL_INT_LIT8:
2773 case Instruction::DIV_INT_LIT8:
2774 case Instruction::REM_INT_LIT8:
2775 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002776 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002777 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002779 break;
2780 case Instruction::AND_INT_LIT8:
2781 case Instruction::OR_INT_LIT8:
2782 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785
2786 /*
2787 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002788 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002789 * inserted in the course of verification, we can expect to see it here.
2790 */
jeffhaob4df5142011-09-19 20:25:32 -07002791 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::UNUSED_EE:
2796 case Instruction::UNUSED_EF:
2797 case Instruction::UNUSED_F2:
2798 case Instruction::UNUSED_F3:
2799 case Instruction::UNUSED_F4:
2800 case Instruction::UNUSED_F5:
2801 case Instruction::UNUSED_F6:
2802 case Instruction::UNUSED_F7:
2803 case Instruction::UNUSED_F8:
2804 case Instruction::UNUSED_F9:
2805 case Instruction::UNUSED_FA:
2806 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002807 case Instruction::UNUSED_F0:
2808 case Instruction::UNUSED_F1:
2809 case Instruction::UNUSED_E3:
2810 case Instruction::UNUSED_E8:
2811 case Instruction::UNUSED_E7:
2812 case Instruction::UNUSED_E4:
2813 case Instruction::UNUSED_E9:
2814 case Instruction::UNUSED_FC:
2815 case Instruction::UNUSED_E5:
2816 case Instruction::UNUSED_EA:
2817 case Instruction::UNUSED_FD:
2818 case Instruction::UNUSED_E6:
2819 case Instruction::UNUSED_EB:
2820 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002821 case Instruction::UNUSED_3E:
2822 case Instruction::UNUSED_3F:
2823 case Instruction::UNUSED_40:
2824 case Instruction::UNUSED_41:
2825 case Instruction::UNUSED_42:
2826 case Instruction::UNUSED_43:
2827 case Instruction::UNUSED_73:
2828 case Instruction::UNUSED_79:
2829 case Instruction::UNUSED_7A:
2830 case Instruction::UNUSED_EC:
2831 case Instruction::UNUSED_FF:
Ian Rogers2c8a8572011-10-24 17:11:36 -07002832 Fail(VERIFY_ERROR_GENERIC) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002833 break;
2834
2835 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002836 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002837 * complain if an instruction is missing (which is desirable).
2838 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 } // end - switch (dec_insn.opcode_)
jeffhaobdb76512011-09-07 11:43:16 -07002840
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 if (failure_ != VERIFY_ERROR_NONE) {
2842 if (failure_ == VERIFY_ERROR_GENERIC) {
jeffhaobdb76512011-09-07 11:43:16 -07002843 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002844 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002845 return false;
2846 } else {
2847 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002848 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002850 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002852 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 failure_ = VERIFY_ERROR_NONE;
jeffhaobdb76512011-09-07 11:43:16 -07002854 opcode_flag = Instruction::kThrow;
2855 }
2856 }
jeffhaobdb76512011-09-07 11:43:16 -07002857 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 * If we didn't just set the result register, clear it out. This ensures that you can only use
2859 * "move-result" immediately after the result is set. (We could check this statically, but it's
2860 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002861 */
2862 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002864 }
2865
jeffhaoa0a764a2011-09-16 10:43:38 -07002866 /* Handle "continue". Tag the next consecutive instruction. */
jeffhaobdb76512011-09-07 11:43:16 -07002867 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2869 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2870 Fail(VERIFY_ERROR_GENERIC) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002871 return false;
2872 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2874 // next instruction isn't one.
2875 if (!CheckMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002876 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 }
2878 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2879 if (next_line != NULL) {
2880 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2881 // needed.
2882 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002883 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 }
jeffhaobdb76512011-09-07 11:43:16 -07002885 } else {
2886 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 * We're not recording register data for the next instruction, so we don't know what the prior
2888 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002889 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002891 }
2892 }
2893
2894 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002895 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002896 *
2897 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002898 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002899 * somebody could get a reference field, check it for zero, and if the
2900 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002901 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002902 * that, and will reject the code.
2903 *
2904 * TODO: avoid re-fetching the branch target
2905 */
2906 if ((opcode_flag & Instruction::kBranch) != 0) {
2907 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002909 /* should never happen after static verification */
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 Fail(VERIFY_ERROR_GENERIC) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002911 return false;
2912 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002913 DCHECK_EQ(isConditional, (opcode_flag & Instruction::kContinue) != 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 if (!CheckMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002915 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 }
jeffhaobdb76512011-09-07 11:43:16 -07002917 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002919 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 }
jeffhaobdb76512011-09-07 11:43:16 -07002921 }
2922
2923 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002924 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002925 *
2926 * We've already verified that the table is structurally sound, so we
2927 * just need to walk through and tag the targets.
2928 */
2929 if ((opcode_flag & Instruction::kSwitch) != 0) {
2930 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2931 const uint16_t* switch_insns = insns + offset_to_switch;
2932 int switch_count = switch_insns[1];
2933 int offset_to_targets, targ;
2934
2935 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2936 /* 0 = sig, 1 = count, 2/3 = first key */
2937 offset_to_targets = 4;
2938 } else {
2939 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002940 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002941 offset_to_targets = 2 + 2 * switch_count;
2942 }
2943
2944 /* verify each switch target */
2945 for (targ = 0; targ < switch_count; targ++) {
2946 int offset;
2947 uint32_t abs_offset;
2948
2949 /* offsets are 32-bit, and only partly endian-swapped */
2950 offset = switch_insns[offset_to_targets + targ * 2] |
2951 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 abs_offset = work_insn_idx_ + offset;
2953 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
2954 if (!CheckMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002955 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 }
2957 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002958 return false;
2959 }
2960 }
2961
2962 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2964 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002965 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 if ((opcode_flag & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
2967 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002968 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002969
Ian Rogers0571d352011-11-03 19:51:38 -07002970 for (; iterator.HasNext(); iterator.Next()) {
2971 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 within_catch_all = true;
2973 }
jeffhaobdb76512011-09-07 11:43:16 -07002974 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2976 * "work_regs", because at runtime the exception will be thrown before the instruction
2977 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002978 */
Ian Rogers0571d352011-11-03 19:51:38 -07002979 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002980 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 }
jeffhaobdb76512011-09-07 11:43:16 -07002982 }
2983
2984 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2986 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002987 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002989 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 * The state in work_line reflects the post-execution state. If the current instruction is a
2991 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002992 * it will do so before grabbing the lock).
2993 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002994 if (dec_insn.opcode_ != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
2995 Fail(VERIFY_ERROR_GENERIC)
2996 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002997 return false;
2998 }
2999 }
3000 }
3001
jeffhaod1f0fde2011-09-08 17:25:33 -07003002 /* If we're returning from the method, make sure monitor stack is empty. */
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 if ((opcode_flag & Instruction::kReturn) != 0) {
3004 if(!work_line_->VerifyMonitorStackEmpty()) {
3005 return false;
3006 }
jeffhaobdb76512011-09-07 11:43:16 -07003007 }
3008
3009 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003010 * Update start_guess. Advance to the next instruction of that's
3011 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003012 * neither of those exists we're in a return or throw; leave start_guess
3013 * alone and let the caller sort it out.
3014 */
3015 if ((opcode_flag & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
jeffhaobdb76512011-09-07 11:43:16 -07003017 } else if ((opcode_flag & Instruction::kBranch) != 0) {
3018 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003020 }
3021
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3023 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003024
3025 return true;
3026}
3027
Ian Rogers28ad40d2011-10-27 15:19:26 -07003028const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003029 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003030 Class* referrer = method_->GetDeclaringClass();
3031 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3032 const RegType& result =
3033 klass != NULL ? reg_types_.FromClass(klass)
3034 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3035 if (klass == NULL && !result.IsUnresolvedTypes()) {
3036 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003038 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3039 // check at runtime if access is allowed and so pass here.
3040 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3041 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003042 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003043 << result << "'";
3044 return reg_types_.Unknown();
3045 } else {
3046 return result;
3047 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003048}
3049
Ian Rogers28ad40d2011-10-27 15:19:26 -07003050const RegType& DexVerifier::GetCaughtExceptionType() {
3051 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003053 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3055 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003056 CatchHandlerIterator iterator(handlers_ptr);
3057 for (; iterator.HasNext(); iterator.Next()) {
3058 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3059 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003060 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003062 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 /* TODO: on error do we want to keep going? If we don't fail this we run the risk of
3064 * having a non-Throwable introduced at runtime. However, that won't pass an instanceof
3065 * test, so is essentially harmless.
3066 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003067 if(!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
3068 Fail(VERIFY_ERROR_GENERIC) << "unexpected non-exception class " << exception;
3069 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 } else if (common_super == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003071 common_super = &exception;
3072 } else if (common_super->Equals(exception)) {
3073 // nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003075 common_super = &common_super->Merge(exception, &reg_types_);
3076 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 }
3078 }
3079 }
3080 }
Ian Rogers0571d352011-11-03 19:51:38 -07003081 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 }
3083 }
3084 if (common_super == NULL) {
3085 /* no catch blocks, or no catches with classes we can find */
3086 Fail(VERIFY_ERROR_GENERIC) << "unable to find exception handler";
3087 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003088 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003089}
3090
3091Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, bool is_direct) {
Ian Rogers90040192011-12-16 08:54:29 -08003092 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3093 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3094 if (failure_ != VERIFY_ERROR_NONE) {
3095 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3096 return NULL;
3097 }
3098 if(klass_type.IsUnresolvedTypes()) {
3099 return NULL; // Can't resolve Class so no more to do here
3100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 Class* referrer = method_->GetDeclaringClass();
3102 DexCache* dex_cache = referrer->GetDexCache();
3103 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3104 if (res_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003105 Class* klass = klass_type.GetClass();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003106 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003107 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 if (is_direct) {
3109 res_method = klass->FindDirectMethod(name, signature);
3110 } else if (klass->IsInterface()) {
3111 res_method = klass->FindInterfaceMethod(name, signature);
3112 } else {
3113 res_method = klass->FindVirtualMethod(name, signature);
3114 }
3115 if (res_method != NULL) {
3116 dex_cache->SetResolvedMethod(method_idx, res_method);
3117 } else {
3118 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003119 << PrettyDescriptor(klass) << "." << name
Ian Rogersd81871c2011-10-03 13:57:23 -07003120 << " " << signature;
3121 return NULL;
3122 }
3123 }
3124 /* Check if access is allowed. */
3125 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3126 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003127 << " from " << PrettyDescriptor(referrer) << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 return NULL;
3129 }
3130 return res_method;
3131}
3132
3133Method* DexVerifier::VerifyInvocationArgs(const Instruction::DecodedInstruction& dec_insn,
3134 MethodType method_type, bool is_range, bool is_super) {
3135 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3136 // we're making.
3137 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB_,
3138 (method_type == METHOD_DIRECT || method_type == METHOD_STATIC));
Ian Rogers28ad40d2011-10-27 15:19:26 -07003139 if (res_method == NULL) { // error or class is unresolved
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 return NULL;
3141 }
3142 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3143 // enforce them here.
3144 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
3145 Fail(VERIFY_ERROR_GENERIC) << "rejecting non-direct call to constructor "
3146 << PrettyMethod(res_method);
3147 return NULL;
3148 }
3149 // See if the method type implied by the invoke instruction matches the access flags for the
3150 // target method.
3151 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3152 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3153 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3154 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003155 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3156 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 return NULL;
3158 }
3159 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3160 // has a vtable entry for the target method.
3161 if (is_super) {
3162 DCHECK(method_type == METHOD_VIRTUAL);
3163 Class* super = method_->GetDeclaringClass()->GetSuperClass();
3164 if (super == NULL || res_method->GetMethodIndex() > super->GetVTable()->GetLength()) {
3165 if (super == NULL) { // Only Object has no super class
3166 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3167 << " to super " << PrettyMethod(res_method);
3168 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003169 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003171 << " to super " << PrettyDescriptor(super)
3172 << "." << mh.GetName()
3173 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 }
3175 return NULL;
3176 }
3177 }
3178 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3179 // match the call to the signature. Also, we might might be calling through an abstract method
3180 // definition (which doesn't have register count values).
3181 int expected_args = dec_insn.vA_;
3182 /* caught by static verifier */
3183 DCHECK(is_range || expected_args <= 5);
3184 if (expected_args > code_item_->outs_size_) {
3185 Fail(VERIFY_ERROR_GENERIC) << "invalid arg count (" << expected_args
3186 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3187 return NULL;
3188 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003189 std::string sig(MethodHelper(res_method).GetSignature());
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 if (sig[0] != '(') {
3191 Fail(VERIFY_ERROR_GENERIC) << "rejecting call to " << res_method
3192 << " as descriptor doesn't start with '(': " << sig;
3193 return NULL;
3194 }
jeffhaobdb76512011-09-07 11:43:16 -07003195 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 * Check the "this" argument, which must be an instance of the class
3197 * that declared the method. For an interface class, we don't do the
3198 * full interface merge, so we can't do a rigorous check here (which
3199 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003200 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 int actual_args = 0;
3202 if (!res_method->IsStatic()) {
3203 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3204 if (failure_ != VERIFY_ERROR_NONE) {
3205 return NULL;
3206 }
3207 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3208 Fail(VERIFY_ERROR_GENERIC) << "'this' arg must be initialized";
3209 return NULL;
3210 }
3211 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003212 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3213 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3214 Fail(VERIFY_ERROR_GENERIC) << "'this' arg '" << actual_arg_type << "' not instance of '"
3215 << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 return NULL;
3217 }
3218 }
3219 actual_args++;
3220 }
3221 /*
3222 * Process the target method's signature. This signature may or may not
3223 * have been verified, so we can't assume it's properly formed.
3224 */
3225 size_t sig_offset = 0;
3226 for (sig_offset = 1; sig_offset < sig.size() && sig[sig_offset] != ')'; sig_offset++) {
3227 if (actual_args >= expected_args) {
3228 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3229 << "'. Expected " << expected_args << " args, found more ("
3230 << sig.substr(sig_offset) << ")";
3231 return NULL;
3232 }
3233 std::string descriptor;
3234 if ((sig[sig_offset] == 'L') || (sig[sig_offset] == '[')) {
3235 size_t end;
3236 if (sig[sig_offset] == 'L') {
3237 end = sig.find(';', sig_offset);
3238 } else {
3239 for(end = sig_offset + 1; sig[end] == '['; end++) ;
3240 if (sig[end] == 'L') {
3241 end = sig.find(';', end);
3242 }
3243 }
3244 if (end == std::string::npos) {
3245 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3246 << "bad signature component '" << sig << "' (missing ';')";
3247 return NULL;
3248 }
3249 descriptor = sig.substr(sig_offset, end - sig_offset + 1);
3250 sig_offset = end;
3251 } else {
3252 descriptor = sig[sig_offset];
3253 }
3254 const RegType& reg_type =
Ian Rogers672297c2012-01-10 14:50:55 -08003255 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3256 descriptor.c_str());
Ian Rogers84fa0742011-10-25 18:13:30 -07003257 uint32_t get_reg = is_range ? dec_insn.vC_ + actual_args : dec_insn.arg_[actual_args];
3258 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3259 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003260 }
3261 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3262 }
3263 if (sig[sig_offset] != ')') {
3264 Fail(VERIFY_ERROR_GENERIC) << "invocation target: bad signature" << PrettyMethod(res_method);
3265 return NULL;
3266 }
3267 if (actual_args != expected_args) {
3268 Fail(VERIFY_ERROR_GENERIC) << "Rejecting invocation of " << PrettyMethod(res_method)
3269 << " expected " << expected_args << " args, found " << actual_args;
3270 return NULL;
3271 } else {
3272 return res_method;
3273 }
3274}
3275
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003276const RegType& DexVerifier::GetMethodReturnType() {
3277 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3278 MethodHelper(method_).GetReturnTypeDescriptor());
3279}
3280
Ian Rogersd81871c2011-10-03 13:57:23 -07003281void DexVerifier::VerifyAGet(const Instruction::DecodedInstruction& dec_insn,
3282 const RegType& insn_type, bool is_primitive) {
3283 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3284 if (!index_type.IsArrayIndexTypes()) {
3285 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3286 } else {
3287 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3288 if (failure_ == VERIFY_ERROR_NONE) {
3289 if (array_class == NULL) {
3290 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3291 // instruction type. TODO: have a proper notion of bottom here.
3292 if (!is_primitive || insn_type.IsCategory1Types()) {
3293 // Reference or category 1
3294 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Zero());
3295 } else {
3296 // Category 2
3297 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.ConstLo());
3298 }
3299 } else {
3300 /* verify the class */
3301 Class* component_class = array_class->GetComponentType();
3302 const RegType& component_type = reg_types_.FromClass(component_class);
3303 if (!array_class->IsArrayClass()) {
3304 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003305 << PrettyDescriptor(array_class) << " with aget";
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 } else if (component_class->IsPrimitive() && !is_primitive) {
3307 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003308 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 << " source for aget-object";
3310 } else if (!component_class->IsPrimitive() && is_primitive) {
3311 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003312 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 << " source for category 1 aget";
3314 } else if (is_primitive && !insn_type.Equals(component_type) &&
3315 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3316 (insn_type.IsLong() && component_type.IsDouble()))) {
3317 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003318 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 << " incompatible with aget of type " << insn_type;
3320 } else {
3321 // Use knowledge of the field type which is stronger than the type inferred from the
3322 // instruction, which can't differentiate object types and ints from floats, longs from
3323 // doubles.
3324 work_line_->SetRegisterType(dec_insn.vA_, component_type);
3325 }
3326 }
3327 }
3328 }
3329}
3330
3331void DexVerifier::VerifyAPut(const Instruction::DecodedInstruction& dec_insn,
3332 const RegType& insn_type, bool is_primitive) {
3333 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC_);
3334 if (!index_type.IsArrayIndexTypes()) {
3335 Fail(VERIFY_ERROR_GENERIC) << "Invalid reg type for array index (" << index_type << ")";
3336 } else {
3337 Class* array_class = work_line_->GetClassFromRegister(dec_insn.vB_);
3338 if (failure_ == VERIFY_ERROR_NONE) {
3339 if (array_class == NULL) {
3340 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3341 // instruction type.
3342 } else {
3343 /* verify the class */
3344 Class* component_class = array_class->GetComponentType();
3345 const RegType& component_type = reg_types_.FromClass(component_class);
3346 if (!array_class->IsArrayClass()) {
3347 Fail(VERIFY_ERROR_GENERIC) << "not array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003348 << PrettyDescriptor(array_class) << " with aput";
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 } else if (component_class->IsPrimitive() && !is_primitive) {
3350 Fail(VERIFY_ERROR_GENERIC) << "primitive array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003351 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 << " source for aput-object";
3353 } else if (!component_class->IsPrimitive() && is_primitive) {
3354 Fail(VERIFY_ERROR_GENERIC) << "reference array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003355 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 << " source for category 1 aput";
3357 } else if (is_primitive && !insn_type.Equals(component_type) &&
3358 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3359 (insn_type.IsLong() && component_type.IsDouble()))) {
3360 Fail(VERIFY_ERROR_GENERIC) << "array type "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003361 << PrettyDescriptor(array_class)
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 << " incompatible with aput of type " << insn_type;
3363 } else {
3364 // The instruction agrees with the type of array, confirm the value to be stored does too
Ian Rogers26fee742011-12-13 13:28:31 -08003365 // Note: we use the instruction type (rather than the component type) for aput-object as
3366 // incompatible classes will be caught at runtime as an array store exception
3367 work_line_->VerifyRegisterType(dec_insn.vA_, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 }
3369 }
3370 }
3371 }
3372}
3373
3374Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003375 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3376 // Check access to class
3377 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3378 if (failure_ != VERIFY_ERROR_NONE) {
3379 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3380 << dex_file_->GetFieldName(field_id) << ") in "
3381 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3382 return NULL;
3383 }
3384 if(klass_type.IsUnresolvedTypes()) {
3385 return NULL; // Can't resolve Class so no more to do here
3386 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003387 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003389 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3390 << dex_file_->GetFieldName(field_id) << ") in "
3391 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 DCHECK(Thread::Current()->IsExceptionPending());
3393 Thread::Current()->ClearException();
3394 return NULL;
3395 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3396 field->GetAccessFlags())) {
3397 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3398 << " from " << PrettyClass(method_->GetDeclaringClass());
3399 return NULL;
3400 } else if (!field->IsStatic()) {
3401 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3402 return NULL;
3403 } else {
3404 return field;
3405 }
3406}
3407
Ian Rogersd81871c2011-10-03 13:57:23 -07003408Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003409 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3410 // Check access to class
3411 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3412 if (failure_ != VERIFY_ERROR_NONE) {
3413 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3414 << dex_file_->GetFieldName(field_id) << ") in "
3415 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3416 return NULL;
3417 }
3418 if(klass_type.IsUnresolvedTypes()) {
3419 return NULL; // Can't resolve Class so no more to do here
3420 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003421 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003423 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3424 << dex_file_->GetFieldName(field_id) << ") in "
3425 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 DCHECK(Thread::Current()->IsExceptionPending());
3427 Thread::Current()->ClearException();
3428 return NULL;
3429 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3430 field->GetAccessFlags())) {
3431 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3432 << " from " << PrettyClass(method_->GetDeclaringClass());
3433 return NULL;
3434 } else if (field->IsStatic()) {
3435 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3436 << " to not be static";
3437 return NULL;
3438 } else if (obj_type.IsZero()) {
3439 // Cannot infer and check type, however, access will cause null pointer exception
3440 return field;
3441 } else if(obj_type.IsUninitializedReference() &&
3442 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3443 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
3444 // Field accesses through uninitialized references are only allowable for constructors where
3445 // the field is declared in this class
3446 Fail(VERIFY_ERROR_GENERIC) << "cannot access instance field " << PrettyField(field)
3447 << " of a not fully initialized object within the context of "
3448 << PrettyMethod(method_);
3449 return NULL;
3450 } else if(!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
3451 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3452 // of C1. For resolution to occur the declared class of the field must be compatible with
3453 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3454 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3455 << " from object of type " << PrettyClass(obj_type.GetClass());
3456 return NULL;
3457 } else {
3458 return field;
3459 }
3460}
3461
Ian Rogersb94a27b2011-10-26 00:33:41 -07003462void DexVerifier::VerifyISGet(const Instruction::DecodedInstruction& dec_insn,
3463 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003464 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003465 Field* field;
3466 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003467 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003468 } else {
3469 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003470 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003471 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003472 if (failure_ != VERIFY_ERROR_NONE) {
3473 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3474 } else {
3475 const char* descriptor;
3476 const ClassLoader* loader;
3477 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003478 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003479 loader = field->GetDeclaringClass()->GetClassLoader();
3480 } else {
3481 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3482 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3483 loader = method_->GetDeclaringClass()->GetClassLoader();
3484 }
3485 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003487 if (field_type.Equals(insn_type) ||
3488 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3489 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 // expected that read is of the correct primitive type or that int reads are reading
3491 // floats or long reads are reading doubles
3492 } else {
3493 // This is a global failure rather than a class change failure as the instructions and
3494 // the descriptors for the type should have been consistent within the same file at
3495 // compile time
3496 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003497 << " to be of type '" << insn_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003498 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 return;
3500 }
3501 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003502 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003503 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003504 << " to be compatible with type '" << insn_type
3505 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003506 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003507 return;
3508 }
3509 }
Ian Rogersb5e95b92011-10-25 23:28:55 -07003510 work_line_->SetRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 }
3512}
3513
Ian Rogersb94a27b2011-10-26 00:33:41 -07003514void DexVerifier::VerifyISPut(const Instruction::DecodedInstruction& dec_insn,
3515 const RegType& insn_type, bool is_primitive, bool is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003516 uint32_t field_idx = is_static ? dec_insn.vB_ : dec_insn.vC_;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003517 Field* field;
3518 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003519 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003520 } else {
3521 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB_);
Ian Rogers55d249f2011-11-02 16:48:09 -07003522 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003523 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003524 if (failure_ != VERIFY_ERROR_NONE) {
3525 work_line_->SetRegisterType(dec_insn.vA_, reg_types_.Unknown());
3526 } else {
3527 const char* descriptor;
3528 const ClassLoader* loader;
3529 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003530 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003531 loader = field->GetDeclaringClass()->GetClassLoader();
3532 } else {
3533 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3534 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3535 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003537 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3538 if (field != NULL) {
3539 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3540 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3541 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3542 return;
3543 }
3544 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003546 // Primitive field assignability rules are weaker than regular assignability rules
3547 bool instruction_compatible;
3548 bool value_compatible;
3549 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA_);
3550 if (field_type.IsIntegralTypes()) {
3551 instruction_compatible = insn_type.IsIntegralTypes();
3552 value_compatible = value_type.IsIntegralTypes();
3553 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003554 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003555 value_compatible = value_type.IsFloatTypes();
3556 } else if (field_type.IsLong()) {
3557 instruction_compatible = insn_type.IsLong();
3558 value_compatible = value_type.IsLongTypes();
3559 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003560 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003561 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003563 instruction_compatible = false; // reference field with primitive store
3564 value_compatible = false; // unused
3565 }
3566 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003567 // This is a global failure rather than a class change failure as the instructions and
3568 // the descriptors for the type should have been consistent within the same file at
3569 // compile time
3570 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003571 << " to be of type '" << insn_type
3572 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003573 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003574 return;
3575 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003576 if (!value_compatible) {
3577 Fail(VERIFY_ERROR_GENERIC) << "unexpected value in v" << dec_insn.vA_
3578 << " of type " << value_type
3579 << " but expected " << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003580 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003581 return;
3582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003584 if (!insn_type.IsAssignableFrom(field_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 Fail(VERIFY_ERROR_GENERIC) << "expected field " << PrettyField(field)
Ian Rogersb5e95b92011-10-25 23:28:55 -07003586 << " to be compatible with type '" << insn_type
3587 << "' but found type '" << field_type
Ian Rogersb94a27b2011-10-26 00:33:41 -07003588 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 return;
3590 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003591 work_line_->VerifyRegisterType(dec_insn.vA_, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003592 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003593 }
3594}
3595
3596bool DexVerifier::CheckMoveException(const uint16_t* insns, int insn_idx) {
3597 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
3598 Fail(VERIFY_ERROR_GENERIC) << "invalid use of move-exception";
3599 return false;
3600 }
3601 return true;
3602}
3603
3604void DexVerifier::VerifyFilledNewArrayRegs(const Instruction::DecodedInstruction& dec_insn,
Ian Rogers28ad40d2011-10-27 15:19:26 -07003605 const RegType& res_type, bool is_range) {
3606 DCHECK(res_type.IsArrayClass()) << res_type; // Checked before calling.
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 /*
3608 * Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of the
3609 * list and fail. It's legal, if silly, for arg_count to be zero.
3610 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07003611 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3612 method_->GetDeclaringClass()->GetClassLoader());
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 uint32_t arg_count = dec_insn.vA_;
3614 for (size_t ui = 0; ui < arg_count; ui++) {
3615 uint32_t get_reg;
3616
3617 if (is_range)
3618 get_reg = dec_insn.vC_ + ui;
3619 else
3620 get_reg = dec_insn.arg_[ui];
3621
3622 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3623 Fail(VERIFY_ERROR_GENERIC) << "filled-new-array arg " << ui << "(" << get_reg
3624 << ") not valid";
3625 return;
3626 }
3627 }
3628}
3629
3630void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003631 if (Runtime::Current()->IsStarted()) {
3632 LOG(ERROR) << "Verification attempting to replacing instructions in " << PrettyMethod(method_)
3633 << " " << fail_messages_.str();
3634 return;
3635 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3637 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3638 VerifyErrorRefType ref_type;
3639 switch (inst->Opcode()) {
3640 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003641 case Instruction::CHECK_CAST:
3642 case Instruction::INSTANCE_OF:
3643 case Instruction::NEW_INSTANCE:
3644 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003645 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003646 case Instruction::FILLED_NEW_ARRAY_RANGE:
3647 ref_type = VERIFY_ERROR_REF_CLASS;
3648 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003650 case Instruction::IGET_BOOLEAN:
3651 case Instruction::IGET_BYTE:
3652 case Instruction::IGET_CHAR:
3653 case Instruction::IGET_SHORT:
3654 case Instruction::IGET_WIDE:
3655 case Instruction::IGET_OBJECT:
3656 case Instruction::IPUT:
3657 case Instruction::IPUT_BOOLEAN:
3658 case Instruction::IPUT_BYTE:
3659 case Instruction::IPUT_CHAR:
3660 case Instruction::IPUT_SHORT:
3661 case Instruction::IPUT_WIDE:
3662 case Instruction::IPUT_OBJECT:
3663 case Instruction::SGET:
3664 case Instruction::SGET_BOOLEAN:
3665 case Instruction::SGET_BYTE:
3666 case Instruction::SGET_CHAR:
3667 case Instruction::SGET_SHORT:
3668 case Instruction::SGET_WIDE:
3669 case Instruction::SGET_OBJECT:
3670 case Instruction::SPUT:
3671 case Instruction::SPUT_BOOLEAN:
3672 case Instruction::SPUT_BYTE:
3673 case Instruction::SPUT_CHAR:
3674 case Instruction::SPUT_SHORT:
3675 case Instruction::SPUT_WIDE:
3676 case Instruction::SPUT_OBJECT:
3677 ref_type = VERIFY_ERROR_REF_FIELD;
3678 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003680 case Instruction::INVOKE_VIRTUAL_RANGE:
3681 case Instruction::INVOKE_SUPER:
3682 case Instruction::INVOKE_SUPER_RANGE:
3683 case Instruction::INVOKE_DIRECT:
3684 case Instruction::INVOKE_DIRECT_RANGE:
3685 case Instruction::INVOKE_STATIC:
3686 case Instruction::INVOKE_STATIC_RANGE:
3687 case Instruction::INVOKE_INTERFACE:
3688 case Instruction::INVOKE_INTERFACE_RANGE:
3689 ref_type = VERIFY_ERROR_REF_METHOD;
3690 break;
jeffhaobdb76512011-09-07 11:43:16 -07003691 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003692 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003693 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003694 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3696 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3697 // instruction, so assert it.
3698 size_t width = inst->SizeInCodeUnits();
3699 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003700 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003701 // NOPs
3702 for (size_t i = 2; i < width; i++) {
3703 insns[work_insn_idx_ + i] = Instruction::NOP;
3704 }
3705 // Encode the opcode, with the failure code in the high byte
3706 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3707 (failure_ << 8) | // AA - component
3708 (ref_type << (8 + kVerifyErrorRefTypeShift));
3709 insns[work_insn_idx_] = new_instruction;
3710 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3711 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003712 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3713 << fail_messages_.str();
3714 if (gDebugVerify) {
3715 std::cout << std::endl << info_messages_.str();
3716 Dump(std::cout);
3717 }
jeffhaobdb76512011-09-07 11:43:16 -07003718}
jeffhaoba5ebb92011-08-25 17:24:37 -07003719
Ian Rogersd81871c2011-10-03 13:57:23 -07003720bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
3721 const bool merge_debug = true;
3722 bool changed = true;
3723 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3724 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003725 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003726 * We haven't processed this instruction before, and we haven't touched the registers here, so
3727 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3728 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003729 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003730 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003731 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 UniquePtr<RegisterLine> copy(merge_debug ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3733 copy->CopyFromLine(target_line);
3734 changed = target_line->MergeRegisters(merge_line);
3735 if (failure_ != VERIFY_ERROR_NONE) {
3736 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003737 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003738 if (gDebugVerify && changed) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003739 LogVerifyInfo() << "Merging at [" << (void*)work_insn_idx_ << "] to [" <<(void*)next_insn << "]: " << std::endl
3740 << *copy.get() << " MERGE" << std::endl
3741 << *merge_line << " ==" << std::endl
3742 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003743 }
3744 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003745 if (changed) {
3746 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003747 }
3748 return true;
3749}
3750
Ian Rogersd81871c2011-10-03 13:57:23 -07003751void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3752 size_t* log2_max_gc_pc) {
3753 size_t local_gc_points = 0;
3754 size_t max_insn = 0;
3755 size_t max_ref_reg = -1;
3756 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3757 if (insn_flags_[i].IsGcPoint()) {
3758 local_gc_points++;
3759 max_insn = i;
3760 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003761 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003762 }
3763 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 *gc_points = local_gc_points;
3765 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3766 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003767 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 i++;
3769 }
3770 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003771}
3772
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003773const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 size_t num_entries, ref_bitmap_bits, pc_bits;
3775 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3776 // There's a single byte to encode the size of each bitmap
3777 if (ref_bitmap_bits >= (8 /* bits per byte */ * 256 /* max unsigned byte + 1 */ )) {
3778 // TODO: either a better GC map format or per method failures
3779 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3780 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003781 return NULL;
3782 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3784 // There are 2 bytes to encode the number of entries
3785 if (num_entries >= 65536) {
3786 // TODO: either a better GC map format or per method failures
3787 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3788 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003789 return NULL;
3790 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003792 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003793 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003794 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003796 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003797 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003799 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 // TODO: either a better GC map format or per method failures
3801 Fail(VERIFY_ERROR_GENERIC) << "Cannot encode GC map for method with "
3802 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3803 return NULL;
3804 }
3805 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries ) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003806 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 if (table == NULL) {
3808 Fail(VERIFY_ERROR_GENERIC) << "Failed to encode GC map (size=" << table_size << ")";
3809 return NULL;
3810 }
3811 // Write table header
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003812 table->push_back(format);
3813 table->push_back(ref_bitmap_bytes);
3814 table->push_back(num_entries & 0xFF);
3815 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3818 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003819 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003821 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 }
3823 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003824 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003825 }
3826 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003827 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 return table;
3829}
jeffhaoa0a764a2011-09-16 10:43:38 -07003830
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003831void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3833 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003834 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 size_t map_index = 0;
3836 for(size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3837 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3838 if (insn_flags_[i].IsGcPoint()) {
3839 CHECK_LT(map_index, map.NumEntries());
3840 CHECK_EQ(map.GetPC(map_index), i);
3841 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3842 map_index++;
3843 RegisterLine* line = reg_table_.GetLine(i);
3844 for(size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003845 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003846 CHECK_LT(j / 8, map.RegWidth());
3847 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3848 } else if ((j / 8) < map.RegWidth()) {
3849 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3850 } else {
3851 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3852 }
3853 }
3854 } else {
3855 CHECK(reg_bitmap == NULL);
3856 }
3857 }
3858}
jeffhaoa0a764a2011-09-16 10:43:38 -07003859
Ian Rogersd81871c2011-10-03 13:57:23 -07003860const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3861 size_t num_entries = NumEntries();
3862 // Do linear or binary search?
3863 static const size_t kSearchThreshold = 8;
3864 if (num_entries < kSearchThreshold) {
3865 for (size_t i = 0; i < num_entries; i++) {
3866 if (GetPC(i) == dex_pc) {
3867 return GetBitMap(i);
3868 }
3869 }
3870 } else {
3871 int lo = 0;
3872 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003873 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 int mid = (hi + lo) / 2;
3875 int mid_pc = GetPC(mid);
3876 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003877 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07003878 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07003879 hi = mid - 1;
3880 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07003882 }
3883 }
3884 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003885 if (error_if_not_present) {
3886 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
3887 }
jeffhaoa0a764a2011-09-16 10:43:38 -07003888 return NULL;
3889}
3890
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003891DexVerifier::GcMapTable DexVerifier::gc_maps_;
3892
3893void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003894 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
3895 if (existing_gc_map != NULL) {
3896 CHECK(*existing_gc_map == gc_map);
3897 delete existing_gc_map;
3898 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003899 gc_maps_[ref] = &gc_map;
3900 CHECK(GetGcMap(ref) != NULL);
3901}
3902
3903const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
3904 GcMapTable::const_iterator it = gc_maps_.find(ref);
3905 if (it == gc_maps_.end()) {
3906 return NULL;
3907 }
3908 CHECK(it->second != NULL);
3909 return it->second;
3910}
3911
3912void DexVerifier::DeleteGcMaps() {
3913 STLDeleteValues(&gc_maps_);
3914}
3915
Ian Rogersd81871c2011-10-03 13:57:23 -07003916} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003917} // namespace art