blob: 91d6ccd4ac802fe0aa3187019b90389b678d0985 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080022#include "compiler.h"
jeffhaob4df5142011-09-19 20:25:32 -070023#include "dex_cache.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "dex_file.h"
25#include "dex_instruction.h"
26#include "dex_instruction_visitor.h"
jeffhaobdb76512011-09-07 11:43:16 -070027#include "dex_verifier.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070028#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "leb128.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070030#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "runtime.h"
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#include "stringpiece.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Logan Chienfca7e872011-12-20 20:08:22 +080035#if defined(ART_USE_LLVM_COMPILER)
36#include "compiler_llvm/backend_types.h"
37#include "compiler_llvm/inferred_reg_category_map.h"
38using namespace art::compiler_llvm;
39#endif
40
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070042namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Ian Rogers2c8a8572011-10-24 17:11:36 -070044static const bool gDebugVerify = false;
45
Ian Rogers84fa0742011-10-25 18:13:30 -070046static const char* type_strings[] = {
47 "Unknown",
48 "Conflict",
49 "Boolean",
50 "Byte",
51 "Short",
52 "Char",
53 "Integer",
54 "Float",
55 "Long (Low Half)",
56 "Long (High Half)",
57 "Double (Low Half)",
58 "Double (High Half)",
59 "64-bit Constant (Low Half)",
60 "64-bit Constant (High Half)",
61 "32-bit Constant",
62 "Unresolved Reference",
63 "Uninitialized Reference",
64 "Uninitialized This Reference",
Ian Rogers28ad40d2011-10-27 15:19:26 -070065 "Unresolved And Uninitialized Reference",
Ian Rogers84fa0742011-10-25 18:13:30 -070066 "Reference",
67};
Ian Rogersd81871c2011-10-03 13:57:23 -070068
Ian Rogers2c8a8572011-10-24 17:11:36 -070069std::string RegType::Dump() const {
Ian Rogers84fa0742011-10-25 18:13:30 -070070 DCHECK(type_ >= kRegTypeUnknown && type_ <= kRegTypeReference);
71 std::string result;
72 if (IsConstant()) {
73 uint32_t val = ConstantValue();
74 if (val == 0) {
75 result = "Zero";
Ian Rogersd81871c2011-10-03 13:57:23 -070076 } else {
Elliott Hughesb25c3f62012-03-26 16:35:06 -070077 if (IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070078 result = StringPrintf("32-bit Constant: %d", val);
79 } else {
80 result = StringPrintf("32-bit Constant: 0x%x", val);
81 }
82 }
83 } else {
84 result = type_strings[type_];
85 if (IsReferenceTypes()) {
86 result += ": ";
Ian Rogers28ad40d2011-10-27 15:19:26 -070087 if (IsUnresolvedTypes()) {
Ian Rogers84fa0742011-10-25 18:13:30 -070088 result += PrettyDescriptor(GetDescriptor());
89 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 result += PrettyDescriptor(GetClass());
Ian Rogers84fa0742011-10-25 18:13:30 -070091 }
Ian Rogersd81871c2011-10-03 13:57:23 -070092 }
93 }
Ian Rogers84fa0742011-10-25 18:13:30 -070094 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -070095}
96
97const RegType& RegType::HighHalf(RegTypeCache* cache) const {
98 CHECK(IsLowHalf());
99 if (type_ == kRegTypeLongLo) {
100 return cache->FromType(kRegTypeLongHi);
101 } else if (type_ == kRegTypeDoubleLo) {
102 return cache->FromType(kRegTypeDoubleHi);
103 } else {
104 return cache->FromType(kRegTypeConstHi);
105 }
106}
107
108/*
109 * A basic Join operation on classes. For a pair of types S and T the Join, written S v T = J, is
110 * S <: J, T <: J and for-all U such that S <: U, T <: U then J <: U. That is J is the parent of
111 * 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
112 * is the deepest (lowest upper bound) parent of S and T).
113 *
114 * This operation applies for regular classes and arrays, however, for interface types there needn't
115 * be a partial ordering on the types. We could solve the problem of a lack of a partial order by
116 * introducing sets of types, however, the only operation permissible on an interface is
117 * invoke-interface. In the tradition of Java verifiers we defer the verification of interface
118 * types until an invoke-interface call on the interface typed reference at runtime and allow
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700119 * the perversion of any Object being assignable to an interface type (note, however, that we don't
120 * allow assignment of Object or Interface to any concrete subclass of Object and are therefore type
121 * safe; further the Join on a Object cannot result in a sub-class by definition).
Ian Rogersd81871c2011-10-03 13:57:23 -0700122 */
123Class* RegType::ClassJoin(Class* s, Class* t) {
124 DCHECK(!s->IsPrimitive()) << PrettyClass(s);
125 DCHECK(!t->IsPrimitive()) << PrettyClass(t);
126 if (s == t) {
127 return s;
128 } else if (s->IsAssignableFrom(t)) {
129 return s;
130 } else if (t->IsAssignableFrom(s)) {
131 return t;
132 } else if (s->IsArrayClass() && t->IsArrayClass()) {
133 Class* s_ct = s->GetComponentType();
134 Class* t_ct = t->GetComponentType();
135 if (s_ct->IsPrimitive() || t_ct->IsPrimitive()) {
136 // Given the types aren't the same, if either array is of primitive types then the only
137 // common parent is java.lang.Object
138 Class* result = s->GetSuperClass(); // short-cut to java.lang.Object
139 DCHECK(result->IsObjectClass());
140 return result;
141 }
142 Class* common_elem = ClassJoin(s_ct, t_ct);
143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
144 const ClassLoader* class_loader = s->GetClassLoader();
Elliott Hughes95572412011-12-13 18:14:20 -0800145 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800146 descriptor += ClassHelper(common_elem).GetDescriptor();
Ian Rogersd81871c2011-10-03 13:57:23 -0700147 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
148 DCHECK(array_class != NULL);
149 return array_class;
150 } else {
151 size_t s_depth = s->Depth();
152 size_t t_depth = t->Depth();
153 // Get s and t to the same depth in the hierarchy
154 if (s_depth > t_depth) {
155 while (s_depth > t_depth) {
156 s = s->GetSuperClass();
157 s_depth--;
158 }
159 } else {
160 while (t_depth > s_depth) {
161 t = t->GetSuperClass();
162 t_depth--;
163 }
164 }
165 // Go up the hierarchy until we get to the common parent
166 while (s != t) {
167 s = s->GetSuperClass();
168 t = t->GetSuperClass();
169 }
170 return s;
171 }
172}
173
Ian Rogersb5e95b92011-10-25 23:28:55 -0700174bool RegType::IsAssignableFrom(const RegType& src) const {
175 if (Equals(src)) {
176 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700177 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700178 switch (GetType()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700179 case RegType::kRegTypeBoolean: return src.IsBooleanTypes();
180 case RegType::kRegTypeByte: return src.IsByteTypes();
181 case RegType::kRegTypeShort: return src.IsShortTypes();
182 case RegType::kRegTypeChar: return src.IsCharTypes();
183 case RegType::kRegTypeInteger: return src.IsIntegralTypes();
184 case RegType::kRegTypeFloat: return src.IsFloatTypes();
185 case RegType::kRegTypeLongLo: return src.IsLongTypes();
186 case RegType::kRegTypeDoubleLo: return src.IsDoubleTypes();
Ian Rogers84fa0742011-10-25 18:13:30 -0700187 default:
Ian Rogersb5e95b92011-10-25 23:28:55 -0700188 if (!IsReferenceTypes()) {
189 LOG(FATAL) << "Unexpected register type in IsAssignableFrom: '" << src << "'";
Ian Rogers84fa0742011-10-25 18:13:30 -0700190 }
Ian Rogersb5e95b92011-10-25 23:28:55 -0700191 if (src.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700192 return true; // all reference types can be assigned null
193 } else if (!src.IsReferenceTypes()) {
194 return false; // expect src to be a reference type
195 } else if (IsJavaLangObject()) {
196 return true; // all reference types can be assigned to Object
197 } else if (!IsUnresolvedTypes() && GetClass()->IsInterface()) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700198 return true; // We allow assignment to any interface, see comment in ClassJoin
Ian Rogers5d86e522012-02-01 11:45:24 -0800199 } else if (IsJavaLangObjectArray()) {
Ian Rogers89310de2012-02-01 13:47:30 -0800200 return src.IsObjectArrayTypes(); // All reference arrays may be assigned to Object[]
Ian Rogers9074b992011-10-26 17:41:55 -0700201 } else if (!IsUnresolvedTypes() && !src.IsUnresolvedTypes() &&
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700202 GetClass()->IsAssignableFrom(src.GetClass())) {
203 // We're assignable from the Class point-of-view
Ian Rogersb5e95b92011-10-25 23:28:55 -0700204 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700205 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -0700206 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700207 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700208 }
209 }
210}
211
Ian Rogers84fa0742011-10-25 18:13:30 -0700212static const RegType& SelectNonConstant(const RegType& a, const RegType& b) {
213 return a.IsConstant() ? b : a;
214}
jeffhaobdb76512011-09-07 11:43:16 -0700215
Ian Rogersd81871c2011-10-03 13:57:23 -0700216const RegType& RegType::Merge(const RegType& incoming_type, RegTypeCache* reg_types) const {
217 DCHECK(!Equals(incoming_type)); // Trivial equality handled by caller
Ian Rogers84fa0742011-10-25 18:13:30 -0700218 if (IsUnknown() && incoming_type.IsUnknown()) {
219 return *this; // Unknown MERGE Unknown => Unknown
220 } else if (IsConflict()) {
221 return *this; // Conflict MERGE * => Conflict
222 } else if (incoming_type.IsConflict()) {
223 return incoming_type; // * MERGE Conflict => Conflict
224 } else if (IsUnknown() || incoming_type.IsUnknown()) {
225 return reg_types->Conflict(); // Unknown MERGE * => Conflict
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700226 } else if (IsConstant() && incoming_type.IsConstant()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700227 int32_t val1 = ConstantValue();
228 int32_t val2 = incoming_type.ConstantValue();
229 if (val1 >= 0 && val2 >= 0) {
230 // +ve1 MERGE +ve2 => MAX(+ve1, +ve2)
231 if (val1 >= val2) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700232 return *this;
Ian Rogers84fa0742011-10-25 18:13:30 -0700233 } else {
234 return incoming_type;
235 }
236 } else if (val1 < 0 && val2 < 0) {
237 // -ve1 MERGE -ve2 => MIN(-ve1, -ve2)
238 if (val1 <= val2) {
239 return *this;
240 } else {
241 return incoming_type;
242 }
243 } else {
244 // Values are +ve and -ve, choose smallest signed type in which they both fit
245 if (IsConstantByte()) {
246 if (incoming_type.IsConstantByte()) {
247 return reg_types->ByteConstant();
248 } else if (incoming_type.IsConstantShort()) {
249 return reg_types->ShortConstant();
250 } else {
251 return reg_types->IntConstant();
252 }
253 } else if (IsConstantShort()) {
Ian Rogers1592bc72011-10-27 20:08:53 -0700254 if (incoming_type.IsConstantShort()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700255 return reg_types->ShortConstant();
256 } else {
257 return reg_types->IntConstant();
258 }
259 } else {
260 return reg_types->IntConstant();
261 }
262 }
263 } else if (IsIntegralTypes() && incoming_type.IsIntegralTypes()) {
264 if (IsBooleanTypes() && incoming_type.IsBooleanTypes()) {
265 return reg_types->Boolean(); // boolean MERGE boolean => boolean
266 }
267 if (IsByteTypes() && incoming_type.IsByteTypes()) {
268 return reg_types->Byte(); // byte MERGE byte => byte
269 }
270 if (IsShortTypes() && incoming_type.IsShortTypes()) {
271 return reg_types->Short(); // short MERGE short => short
272 }
273 if (IsCharTypes() && incoming_type.IsCharTypes()) {
274 return reg_types->Char(); // char MERGE char => char
275 }
276 return reg_types->Integer(); // int MERGE * => int
277 } else if ((IsFloatTypes() && incoming_type.IsFloatTypes()) ||
278 (IsLongTypes() && incoming_type.IsLongTypes()) ||
279 (IsLongHighTypes() && incoming_type.IsLongHighTypes()) ||
280 (IsDoubleTypes() && incoming_type.IsDoubleTypes()) ||
281 (IsDoubleHighTypes() && incoming_type.IsDoubleHighTypes())) {
282 // check constant case was handled prior to entry
283 DCHECK(!IsConstant() || !incoming_type.IsConstant());
284 // float/long/double MERGE float/long/double_constant => float/long/double
285 return SelectNonConstant(*this, incoming_type);
286 } else if (IsReferenceTypes() && incoming_type.IsReferenceTypes()) {
Ian Rogers9074b992011-10-26 17:41:55 -0700287 if (IsZero() || incoming_type.IsZero()) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700288 return SelectNonConstant(*this, incoming_type); // 0 MERGE ref => ref
Ian Rogers9074b992011-10-26 17:41:55 -0700289 } else if (IsJavaLangObject() || incoming_type.IsJavaLangObject()) {
290 return reg_types->JavaLangObject(); // Object MERGE ref => Object
291 } else if (IsUninitializedTypes() || incoming_type.IsUninitializedTypes() ||
292 IsUnresolvedTypes() || incoming_type.IsUnresolvedTypes()) {
293 // Can only merge an unresolved or uninitialized type with itself, 0 or Object, we've already
294 // checked these so => Conflict
Ian Rogers84fa0742011-10-25 18:13:30 -0700295 return reg_types->Conflict();
296 } else { // Two reference types, compute Join
297 Class* c1 = GetClass();
298 Class* c2 = incoming_type.GetClass();
299 DCHECK(c1 != NULL && !c1->IsPrimitive());
300 DCHECK(c2 != NULL && !c2->IsPrimitive());
301 Class* join_class = ClassJoin(c1, c2);
302 if (c1 == join_class) {
303 return *this;
304 } else if (c2 == join_class) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700305 return incoming_type;
306 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700307 return reg_types->FromClass(join_class);
Ian Rogersd81871c2011-10-03 13:57:23 -0700308 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700310 } else {
311 return reg_types->Conflict(); // Unexpected types => Conflict
Ian Rogersd81871c2011-10-03 13:57:23 -0700312 }
313}
314
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700315static RegType::Type RegTypeFromPrimitiveType(Primitive::Type prim_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700316 switch (prim_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700317 case Primitive::kPrimBoolean: return RegType::kRegTypeBoolean;
318 case Primitive::kPrimByte: return RegType::kRegTypeByte;
319 case Primitive::kPrimShort: return RegType::kRegTypeShort;
320 case Primitive::kPrimChar: return RegType::kRegTypeChar;
321 case Primitive::kPrimInt: return RegType::kRegTypeInteger;
322 case Primitive::kPrimLong: return RegType::kRegTypeLongLo;
323 case Primitive::kPrimFloat: return RegType::kRegTypeFloat;
324 case Primitive::kPrimDouble: return RegType::kRegTypeDoubleLo;
325 case Primitive::kPrimVoid:
326 default: return RegType::kRegTypeUnknown;
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 }
328}
329
330static RegType::Type RegTypeFromDescriptor(const std::string& descriptor) {
331 if (descriptor.length() == 1) {
332 switch (descriptor[0]) {
333 case 'Z': return RegType::kRegTypeBoolean;
334 case 'B': return RegType::kRegTypeByte;
335 case 'S': return RegType::kRegTypeShort;
336 case 'C': return RegType::kRegTypeChar;
337 case 'I': return RegType::kRegTypeInteger;
338 case 'J': return RegType::kRegTypeLongLo;
339 case 'F': return RegType::kRegTypeFloat;
340 case 'D': return RegType::kRegTypeDoubleLo;
341 case 'V':
342 default: return RegType::kRegTypeUnknown;
343 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700344 } else if (descriptor[0] == 'L' || descriptor[0] == '[') {
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 return RegType::kRegTypeReference;
346 } else {
347 return RegType::kRegTypeUnknown;
348 }
349}
350
351std::ostream& operator<<(std::ostream& os, const RegType& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700352 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700353 return os;
354}
355
356const RegType& RegTypeCache::FromDescriptor(const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800357 const char* descriptor) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700358 return From(RegTypeFromDescriptor(descriptor), loader, descriptor);
359}
360
361const RegType& RegTypeCache::From(RegType::Type type, const ClassLoader* loader,
Ian Rogers672297c2012-01-10 14:50:55 -0800362 const char* descriptor) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700363 if (type <= RegType::kRegTypeLastFixedLocation) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700364 // entries should be sized greater than primitive types
365 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
366 RegType* entry = entries_[type];
367 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700368 Class* klass = NULL;
Ian Rogers672297c2012-01-10 14:50:55 -0800369 if (strlen(descriptor) != 0) {
370 klass = Runtime::Current()->GetClassLinker()->FindSystemClass(descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700372 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 entries_[type] = entry;
374 }
375 return *entry;
376 } else {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700377 DCHECK(type == RegType::kRegTypeReference);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378 ClassHelper kh;
Ian Rogers84fa0742011-10-25 18:13:30 -0700379 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700380 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700381 // check resolved and unresolved references, ignore uninitialized references
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 if (cur_entry->IsReference()) {
383 kh.ChangeClass(cur_entry->GetClass());
Ian Rogers672297c2012-01-10 14:50:55 -0800384 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800385 return *cur_entry;
386 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700387 } else if (cur_entry->IsUnresolvedReference() &&
388 cur_entry->GetDescriptor()->Equals(descriptor)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700389 return *cur_entry;
390 }
391 }
Ian Rogers672297c2012-01-10 14:50:55 -0800392 Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, loader);
Ian Rogers2c8a8572011-10-24 17:11:36 -0700393 if (klass != NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700394 // Able to resolve so create resolved register type
395 RegType* entry = new RegType(type, klass, 0, entries_.size());
Ian Rogers2c8a8572011-10-24 17:11:36 -0700396 entries_.push_back(entry);
397 return *entry;
398 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700399 // TODO: we assume unresolved, but we may be able to do better by validating whether the
400 // descriptor string is valid
Ian Rogers84fa0742011-10-25 18:13:30 -0700401 // Unable to resolve so create unresolved register type
Ian Rogers2c8a8572011-10-24 17:11:36 -0700402 DCHECK(Thread::Current()->IsExceptionPending());
Ian Rogers84fa0742011-10-25 18:13:30 -0700403 Thread::Current()->ClearException();
Ian Rogers672297c2012-01-10 14:50:55 -0800404 if (IsValidDescriptor(descriptor)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700405 String* string_descriptor =
Ian Rogers672297c2012-01-10 14:50:55 -0800406 Runtime::Current()->GetInternTable()->InternStrong(descriptor);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700407 RegType* entry = new RegType(RegType::kRegTypeUnresolvedReference, string_descriptor, 0,
408 entries_.size());
409 entries_.push_back(entry);
410 return *entry;
411 } else {
412 // The descriptor is broken return the unknown type as there's nothing sensible that
413 // could be done at runtime
414 return Unknown();
415 }
Ian Rogers2c8a8572011-10-24 17:11:36 -0700416 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700417 }
418}
419
420const RegType& RegTypeCache::FromClass(Class* klass) {
421 if (klass->IsPrimitive()) {
422 RegType::Type type = RegTypeFromPrimitiveType(klass->GetPrimitiveType());
423 // entries should be sized greater than primitive types
424 DCHECK_GT(entries_.size(), static_cast<size_t>(type));
425 RegType* entry = entries_[type];
426 if (entry == NULL) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700427 entry = new RegType(type, klass, 0, type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 entries_[type] = entry;
429 }
430 return *entry;
431 } else {
Ian Rogers84fa0742011-10-25 18:13:30 -0700432 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 RegType* cur_entry = entries_[i];
Ian Rogers84fa0742011-10-25 18:13:30 -0700434 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 return *cur_entry;
436 }
437 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700438 RegType* entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 entries_.push_back(entry);
440 return *entry;
441 }
442}
443
Ian Rogers28ad40d2011-10-27 15:19:26 -0700444const RegType& RegTypeCache::Uninitialized(const RegType& type, uint32_t allocation_pc) {
445 RegType* entry;
446 if (type.IsUnresolvedTypes()) {
447 String* descriptor = type.GetDescriptor();
448 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
449 RegType* cur_entry = entries_[i];
450 if (cur_entry->IsUnresolvedAndUninitializedReference() &&
451 cur_entry->GetAllocationPc() == allocation_pc &&
452 cur_entry->GetDescriptor() == descriptor) {
453 return *cur_entry;
454 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700455 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700456 entry = new RegType(RegType::kRegTypeUnresolvedAndUninitializedReference,
457 descriptor, allocation_pc, entries_.size());
458 } else {
459 Class* klass = type.GetClass();
460 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
461 RegType* cur_entry = entries_[i];
462 if (cur_entry->IsUninitializedReference() &&
463 cur_entry->GetAllocationPc() == allocation_pc &&
464 cur_entry->GetClass() == klass) {
465 return *cur_entry;
466 }
467 }
468 entry = new RegType(RegType::kRegTypeUninitializedReference,
469 klass, allocation_pc, entries_.size());
Ian Rogersd81871c2011-10-03 13:57:23 -0700470 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700471 entries_.push_back(entry);
472 return *entry;
473}
474
475const RegType& RegTypeCache::FromUninitialized(const RegType& uninit_type) {
476 RegType* entry;
477 if (uninit_type.IsUnresolvedTypes()) {
478 String* descriptor = uninit_type.GetDescriptor();
479 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
480 RegType* cur_entry = entries_[i];
481 if (cur_entry->IsUnresolvedReference() && cur_entry->GetDescriptor() == descriptor) {
482 return *cur_entry;
483 }
484 }
485 entry = new RegType(RegType::kRegTypeUnresolvedReference, descriptor, 0, entries_.size());
486 } else {
487 Class* klass = uninit_type.GetClass();
488 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
489 RegType* cur_entry = entries_[i];
490 if (cur_entry->IsReference() && cur_entry->GetClass() == klass) {
491 return *cur_entry;
492 }
493 }
494 entry = new RegType(RegType::kRegTypeReference, klass, 0, entries_.size());
495 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 entries_.push_back(entry);
497 return *entry;
498}
499
500const RegType& RegTypeCache::UninitializedThisArgument(Class* klass) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700501 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700502 RegType* cur_entry = entries_[i];
503 if (cur_entry->IsUninitializedThisReference() && cur_entry->GetClass() == klass) {
504 return *cur_entry;
505 }
506 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700507 RegType* entry = new RegType(RegType::kRegTypeUninitializedThisReference, klass, 0,
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 entries_.size());
509 entries_.push_back(entry);
510 return *entry;
511}
512
513const RegType& RegTypeCache::FromType(RegType::Type type) {
514 CHECK(type < RegType::kRegTypeReference);
515 switch (type) {
516 case RegType::kRegTypeBoolean: return From(type, NULL, "Z");
517 case RegType::kRegTypeByte: return From(type, NULL, "B");
518 case RegType::kRegTypeShort: return From(type, NULL, "S");
519 case RegType::kRegTypeChar: return From(type, NULL, "C");
520 case RegType::kRegTypeInteger: return From(type, NULL, "I");
521 case RegType::kRegTypeFloat: return From(type, NULL, "F");
522 case RegType::kRegTypeLongLo:
523 case RegType::kRegTypeLongHi: return From(type, NULL, "J");
524 case RegType::kRegTypeDoubleLo:
525 case RegType::kRegTypeDoubleHi: return From(type, NULL, "D");
526 default: return From(type, NULL, "");
527 }
528}
529
530const RegType& RegTypeCache::FromCat1Const(int32_t value) {
Ian Rogers84fa0742011-10-25 18:13:30 -0700531 for (size_t i = RegType::kRegTypeLastFixedLocation + 1; i < entries_.size(); i++) {
532 RegType* cur_entry = entries_[i];
533 if (cur_entry->IsConstant() && cur_entry->ConstantValue() == value) {
534 return *cur_entry;
535 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 }
Ian Rogers84fa0742011-10-25 18:13:30 -0700537 RegType* entry = new RegType(RegType::kRegTypeConst, NULL, value, entries_.size());
538 entries_.push_back(entry);
539 return *entry;
Ian Rogersd81871c2011-10-03 13:57:23 -0700540}
541
Ian Rogers28ad40d2011-10-27 15:19:26 -0700542const RegType& RegTypeCache::GetComponentType(const RegType& array, const ClassLoader* loader) {
Ian Rogers89310de2012-02-01 13:47:30 -0800543 CHECK(array.IsArrayTypes());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700544 if (array.IsUnresolvedTypes()) {
Elliott Hughes95572412011-12-13 18:14:20 -0800545 std::string descriptor(array.GetDescriptor()->ToModifiedUtf8());
546 std::string component(descriptor.substr(1, descriptor.size() - 1));
Ian Rogers672297c2012-01-10 14:50:55 -0800547 return FromDescriptor(loader, component.c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700548 } else {
549 return FromClass(array.GetClass()->GetComponentType());
550 }
551}
552
553
Ian Rogersd81871c2011-10-03 13:57:23 -0700554bool RegisterLine::CheckConstructorReturn() const {
555 for (size_t i = 0; i < num_regs_; i++) {
556 if (GetRegisterType(i).IsUninitializedThisReference()) {
jeffhaod5347e02012-03-22 17:25:05 -0700557 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT)
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 << "Constructor returning without calling superclass constructor";
559 return false;
560 }
561 }
562 return true;
563}
564
jeffhao60f83e32012-02-13 17:16:30 -0800565bool RegisterLine::SetRegisterType(uint32_t vdst, const RegType& new_type) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 DCHECK(vdst < num_regs_);
567 if (new_type.IsLowHalf()) {
568 line_[vdst] = new_type.GetId();
569 line_[vdst + 1] = new_type.HighHalf(verifier_->GetRegTypeCache()).GetId();
570 } else if (new_type.IsHighHalf()) {
571 /* should never set these explicitly */
jeffhaod5347e02012-03-22 17:25:05 -0700572 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Explicit set of high register type";
jeffhao60f83e32012-02-13 17:16:30 -0800573 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 } else if (new_type.IsConflict()) { // should only be set during a merge
jeffhaod5347e02012-03-22 17:25:05 -0700575 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Set register to unknown type " << new_type;
jeffhao60f83e32012-02-13 17:16:30 -0800576 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 } else {
578 line_[vdst] = new_type.GetId();
579 }
580 // Clear the monitor entry bits for this register.
581 ClearAllRegToLockDepths(vdst);
jeffhao60f83e32012-02-13 17:16:30 -0800582 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -0700583}
584
585void RegisterLine::SetResultTypeToUnknown() {
586 uint16_t unknown_id = verifier_->GetRegTypeCache()->Unknown().GetId();
587 result_[0] = unknown_id;
588 result_[1] = unknown_id;
589}
590
591void RegisterLine::SetResultRegisterType(const RegType& new_type) {
592 result_[0] = new_type.GetId();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700593 if (new_type.IsLowHalf()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 DCHECK_EQ(new_type.HighHalf(verifier_->GetRegTypeCache()).GetId(), new_type.GetId() + 1);
595 result_[1] = new_type.GetId() + 1;
596 } else {
597 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
598 }
599}
600
601const RegType& RegisterLine::GetRegisterType(uint32_t vsrc) const {
602 // The register index was validated during the static pass, so we don't need to check it here.
603 DCHECK_LT(vsrc, num_regs_);
604 return verifier_->GetRegTypeCache()->GetFromId(line_[vsrc]);
605}
606
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607const RegType& RegisterLine::GetInvocationThis(const DecodedInstruction& dec_insn) {
608 if (dec_insn.vA < 1) {
jeffhaod5347e02012-03-22 17:25:05 -0700609 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke lacks 'this'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 return verifier_->GetRegTypeCache()->Unknown();
611 }
612 /* get the element type of the array held in vsrc */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 const RegType& this_type = GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 if (!this_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700615 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "tried to get class from non-reference register v"
616 << dec_insn.vC << " (type=" << this_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 return verifier_->GetRegTypeCache()->Unknown();
618 }
619 return this_type;
620}
621
Ian Rogersd81871c2011-10-03 13:57:23 -0700622bool RegisterLine::VerifyRegisterType(uint32_t vsrc, const RegType& check_type) {
623 // Verify the src register type against the check type refining the type of the register
624 const RegType& src_type = GetRegisterType(vsrc);
Ian Rogersb5e95b92011-10-25 23:28:55 -0700625 if (!check_type.IsAssignableFrom(src_type)) {
jeffhaod5347e02012-03-22 17:25:05 -0700626 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "register v" << vsrc << " has type " << src_type
627 << " but expected " << check_type;
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 return false;
629 }
jeffhao457cc512012-02-02 16:55:13 -0800630 if (check_type.IsLowHalf()) {
631 const RegType& src_type_h = GetRegisterType(vsrc + 1);
632 if (!src_type.CheckWidePair(src_type_h)) {
jeffhaod5347e02012-03-22 17:25:05 -0700633 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "wide register v" << vsrc << " has type "
634 << src_type << "/" << src_type_h;
jeffhao457cc512012-02-02 16:55:13 -0800635 return false;
636 }
637 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
639 // precise than the subtype in vsrc so leave it for reference types. For primitive types
640 // if they are a defined type then they are as precise as we can get, however, for constant
641 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
642 return true;
643}
644
645void RegisterLine::MarkRefsAsInitialized(const RegType& uninit_type) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700646 DCHECK(uninit_type.IsUninitializedTypes());
647 const RegType& init_type = verifier_->GetRegTypeCache()->FromUninitialized(uninit_type);
648 size_t changed = 0;
649 for (size_t i = 0; i < num_regs_; i++) {
650 if (GetRegisterType(i).Equals(uninit_type)) {
651 line_[i] = init_type.GetId();
652 changed++;
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700655 DCHECK_GT(changed, 0u);
Ian Rogersd81871c2011-10-03 13:57:23 -0700656}
657
658void RegisterLine::MarkUninitRefsAsInvalid(const RegType& uninit_type) {
659 for (size_t i = 0; i < num_regs_; i++) {
660 if (GetRegisterType(i).Equals(uninit_type)) {
661 line_[i] = verifier_->GetRegTypeCache()->Conflict().GetId();
662 ClearAllRegToLockDepths(i);
663 }
664 }
665}
666
667void RegisterLine::CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) {
668 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
669 const RegType& type = GetRegisterType(vsrc);
jeffhao60f83e32012-02-13 17:16:30 -0800670 if (!SetRegisterType(vdst, type)) {
671 return;
672 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 if ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
674 (cat == kTypeCategoryRef && !type.IsReferenceTypes())) {
jeffhaod5347e02012-03-22 17:25:05 -0700675 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
676 << " cat=" << static_cast<int>(cat);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 } else if (cat == kTypeCategoryRef) {
678 CopyRegToLockDepth(vdst, vsrc);
679 }
680}
681
682void RegisterLine::CopyRegister2(uint32_t vdst, uint32_t vsrc) {
683 const RegType& type_l = GetRegisterType(vsrc);
684 const RegType& type_h = GetRegisterType(vsrc + 1);
685
686 if (!type_l.CheckWidePair(type_h)) {
jeffhaod5347e02012-03-22 17:25:05 -0700687 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy2 v" << vdst << "<-v" << vsrc
688 << " type=" << type_l << "/" << type_h;
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 } else {
690 SetRegisterType(vdst, type_l); // implicitly sets the second half
691 }
692}
693
694void RegisterLine::CopyResultRegister1(uint32_t vdst, bool is_reference) {
695 const RegType& type = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
696 if ((!is_reference && !type.IsCategory1Types()) ||
697 (is_reference && !type.IsReferenceTypes())) {
jeffhaod5347e02012-03-22 17:25:05 -0700698 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
700 } else {
701 DCHECK(verifier_->GetRegTypeCache()->GetFromId(result_[1]).IsUnknown());
702 SetRegisterType(vdst, type);
703 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
704 }
705}
706
707/*
708 * Implement "move-result-wide". Copy the category-2 value from the result
709 * register to another register, and reset the result register.
710 */
711void RegisterLine::CopyResultRegister2(uint32_t vdst) {
712 const RegType& type_l = verifier_->GetRegTypeCache()->GetFromId(result_[0]);
713 const RegType& type_h = verifier_->GetRegTypeCache()->GetFromId(result_[1]);
714 if (!type_l.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -0700715 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
717 } else {
718 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
719 SetRegisterType(vdst, type_l); // also sets the high
720 result_[0] = verifier_->GetRegTypeCache()->Unknown().GetId();
721 result_[1] = verifier_->GetRegTypeCache()->Unknown().GetId();
722 }
723}
724
Elliott Hughesadb8c672012-03-06 16:49:32 -0800725void RegisterLine::CheckUnaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 const RegType& dst_type, const RegType& src_type) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800727 if (VerifyRegisterType(dec_insn.vB, src_type)) {
728 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 }
730}
731
Elliott Hughesadb8c672012-03-06 16:49:32 -0800732void RegisterLine::CheckBinaryOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 const RegType& dst_type,
734 const RegType& src_type1, const RegType& src_type2,
735 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800736 if (VerifyRegisterType(dec_insn.vB, src_type1) &&
737 VerifyRegisterType(dec_insn.vC, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 if (check_boolean_op) {
739 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800740 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
741 GetRegisterType(dec_insn.vC).IsBooleanTypes()) {
742 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 return;
744 }
745 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800746 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 }
748}
749
Elliott Hughesadb8c672012-03-06 16:49:32 -0800750void RegisterLine::CheckBinaryOp2addr(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 const RegType& dst_type, const RegType& src_type1,
752 const RegType& src_type2, bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800753 if (VerifyRegisterType(dec_insn.vA, src_type1) &&
754 VerifyRegisterType(dec_insn.vB, src_type2)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 if (check_boolean_op) {
756 DCHECK(dst_type.IsInteger());
Elliott Hughesadb8c672012-03-06 16:49:32 -0800757 if (GetRegisterType(dec_insn.vA).IsBooleanTypes() &&
758 GetRegisterType(dec_insn.vB).IsBooleanTypes()) {
759 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return;
761 }
762 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800763 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 }
765}
766
Elliott Hughesadb8c672012-03-06 16:49:32 -0800767void RegisterLine::CheckLiteralOp(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 const RegType& dst_type, const RegType& src_type,
769 bool check_boolean_op) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800770 if (VerifyRegisterType(dec_insn.vB, src_type)) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 if (check_boolean_op) {
772 DCHECK(dst_type.IsInteger());
773 /* check vB with the call, then check the constant manually */
Elliott Hughesadb8c672012-03-06 16:49:32 -0800774 if (GetRegisterType(dec_insn.vB).IsBooleanTypes() &&
775 (dec_insn.vC == 0 || dec_insn.vC == 1)) {
776 SetRegisterType(dec_insn.vA, verifier_->GetRegTypeCache()->Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 return;
778 }
779 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800780 SetRegisterType(dec_insn.vA, dst_type);
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 }
782}
783
784void RegisterLine::PushMonitor(uint32_t reg_idx, int32_t insn_idx) {
785 const RegType& reg_type = GetRegisterType(reg_idx);
786 if (!reg_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700787 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter on non-object (" << reg_type << ")";
Elliott Hughesfbef9462011-12-14 14:24:40 -0800788 } else if (monitors_.size() >= 32) {
jeffhaod5347e02012-03-22 17:25:05 -0700789 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter stack overflow: " << monitors_.size();
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 } else {
791 SetRegToLockDepth(reg_idx, monitors_.size());
Ian Rogers55d249f2011-11-02 16:48:09 -0700792 monitors_.push_back(insn_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 }
794}
795
796void RegisterLine::PopMonitor(uint32_t reg_idx) {
797 const RegType& reg_type = GetRegisterType(reg_idx);
798 if (!reg_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -0700799 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit on non-object (" << reg_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 } else if (monitors_.empty()) {
jeffhaod5347e02012-03-22 17:25:05 -0700801 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit stack underflow";
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 } else {
Ian Rogers55d249f2011-11-02 16:48:09 -0700803 monitors_.pop_back();
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700804 if (!IsSetLockDepth(reg_idx, monitors_.size())) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 // Bug 3215458: Locks and unlocks are on objects, if that object is a literal then before
806 // format "036" the constant collector may create unlocks on the same object but referenced
807 // via different registers.
jeffhaod5347e02012-03-22 17:25:05 -0700808 ((verifier_->DexFileVersion() >= 36) ? verifier_->Fail(VERIFY_ERROR_BAD_CLASS_SOFT)
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 : verifier_->LogVerifyInfo())
810 << "monitor-exit not unlocking the top of the monitor stack";
811 } else {
812 // Record the register was unlocked
813 ClearRegToLockDepth(reg_idx, monitors_.size());
814 }
815 }
816}
817
818bool RegisterLine::VerifyMonitorStackEmpty() {
819 if (MonitorStackDepth() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700820 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected empty monitor stack";
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 return false;
822 } else {
823 return true;
824 }
825}
826
827bool RegisterLine::MergeRegisters(const RegisterLine* incoming_line) {
828 bool changed = false;
829 for (size_t idx = 0; idx < num_regs_; idx++) {
830 if (line_[idx] != incoming_line->line_[idx]) {
831 const RegType& incoming_reg_type = incoming_line->GetRegisterType(idx);
832 const RegType& cur_type = GetRegisterType(idx);
833 const RegType& new_type = cur_type.Merge(incoming_reg_type, verifier_->GetRegTypeCache());
834 changed = changed || !cur_type.Equals(new_type);
835 line_[idx] = new_type.GetId();
836 }
837 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700838 if (monitors_.size() != incoming_line->monitors_.size()) {
jeffhaod5347e02012-03-22 17:25:05 -0700839 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths (depth="
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 << MonitorStackDepth() << ", incoming depth=" << incoming_line->MonitorStackDepth() << ")";
841 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
842 for (uint32_t idx = 0; idx < num_regs_; idx++) {
843 size_t depths = reg_to_lock_depths_.count(idx);
844 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
845 if (depths != incoming_depths) {
846 if (depths == 0 || incoming_depths == 0) {
847 reg_to_lock_depths_.erase(idx);
848 } else {
jeffhaod5347e02012-03-22 17:25:05 -0700849 verifier_->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "mismatched stack depths for register v" << idx
850 << ": " << depths << " != " << incoming_depths;
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 }
853 }
854 }
855 }
856 return changed;
857}
858
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800859void RegisterLine::WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 for (size_t i = 0; i < num_regs_; i += 8) {
861 uint8_t val = 0;
862 for (size_t j = 0; j < 8 && (i + j) < num_regs_; j++) {
863 // Note: we write 1 for a Reference but not for Null
Ian Rogers84fa0742011-10-25 18:13:30 -0700864 if (GetRegisterType(i + j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 val |= 1 << j;
866 }
867 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800868 if ((i / 8) >= max_bytes) {
869 DCHECK_EQ(0, val);
870 continue;
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800872 DCHECK_LT(i / 8, max_bytes) << "val=" << static_cast<uint32_t>(val);
873 data.push_back(val);
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 }
875}
876
877std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs) {
Ian Rogers2c8a8572011-10-24 17:11:36 -0700878 os << rhs.Dump();
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 return os;
880}
881
882
883void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InsnFlags* flags,
884 uint32_t insns_size, uint16_t registers_size,
885 DexVerifier* verifier) {
886 DCHECK_GT(insns_size, 0U);
887
888 for (uint32_t i = 0; i < insns_size; i++) {
889 bool interesting = false;
890 switch (mode) {
891 case kTrackRegsAll:
892 interesting = flags[i].IsOpcode();
893 break;
894 case kTrackRegsGcPoints:
895 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
896 break;
897 case kTrackRegsBranches:
898 interesting = flags[i].IsBranchTarget();
899 break;
900 default:
901 break;
902 }
903 if (interesting) {
904 pc_to_register_line_[i] = new RegisterLine(registers_size, verifier);
905 }
906 }
907}
908
Ian Rogers1c5eb702012-02-01 09:18:34 -0800909bool DexVerifier::VerifyClass(const Class* klass, std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -0700910 if (klass->IsVerified()) {
911 return true;
912 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -0800914 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800915 error = "Verifier rejected class ";
916 error += PrettyDescriptor(klass);
917 error += " that has no super class";
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 return false;
919 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800920 if (super != NULL && super->IsFinal()) {
921 error = "Verifier rejected class ";
922 error += PrettyDescriptor(klass);
923 error += " that attempts to sub-class final class ";
924 error += PrettyDescriptor(super);
925 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 }
jeffhaobdb76512011-09-07 11:43:16 -0700927 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
928 Method* method = klass->GetDirectMethod(i);
929 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800930 error = "Verifier rejected class ";
931 error += PrettyDescriptor(klass);
932 error += " due to bad method ";
933 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700934 return false;
935 }
936 }
937 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
938 Method* method = klass->GetVirtualMethod(i);
939 if (!VerifyMethod(method)) {
Ian Rogers1c5eb702012-02-01 09:18:34 -0800940 error = "Verifier rejected class ";
941 error += PrettyDescriptor(klass);
942 error += " due to bad method ";
943 error += PrettyMethod(method, true);
jeffhaobdb76512011-09-07 11:43:16 -0700944 return false;
945 }
946 }
947 return true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948}
949
jeffhaobdb76512011-09-07 11:43:16 -0700950bool DexVerifier::VerifyMethod(Method* method) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800952 bool success = verifier.VerifyAll();
Brian Carlstrom75412882012-01-18 01:26:54 -0800953 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
954
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 // We expect either success and no verification error, or failure and a generic failure to
956 // reject the class.
957 if (success) {
958 if (verifier.failure_ != VERIFY_ERROR_NONE) {
959 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method) << std::endl
960 << verifier.fail_messages_;
961 }
962 } else {
963 LOG(INFO) << "Verification error in " << PrettyMethod(method) << " "
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700964 << verifier.fail_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700965 if (gDebugVerify) {
Ian Rogers5ed29bf2011-10-26 12:22:21 -0700966 std::cout << std::endl << verifier.info_messages_.str();
Ian Rogers2c8a8572011-10-24 17:11:36 -0700967 verifier.Dump(std::cout);
968 }
jeffhaod5347e02012-03-22 17:25:05 -0700969 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
970 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 }
972 return success;
973}
974
Shih-wei Liao371814f2011-10-27 16:52:10 -0700975void DexVerifier::VerifyMethodAndDump(Method* method) {
976 DexVerifier verifier(method);
jeffhaof56197c2012-03-05 18:01:54 -0800977 verifier.VerifyAll();
Shih-wei Liao371814f2011-10-27 16:52:10 -0700978
Elliott Hughese0918552011-10-28 17:18:29 -0700979 LOG(INFO) << "Dump of method " << PrettyMethod(method) << " "
980 << verifier.fail_messages_.str() << std::endl
981 << verifier.info_messages_.str() << Dumpable<DexVerifier>(verifier);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700982}
983
jeffhaof56197c2012-03-05 18:01:54 -0800984bool DexVerifier::VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
985 const ClassLoader* class_loader, uint32_t class_def_idx, std::string& error) {
986 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
987 const byte* class_data = dex_file->GetClassData(class_def);
988 ClassDataItemIterator it(*dex_file, class_data);
989 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
990 it.Next();
991 }
992 while (it.HasNextDirectMethod()) {
993 uint32_t method_idx = it.GetMemberIndex();
994 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
995 it.GetMethodCodeItem())) {
996 error = "Verifier rejected class";
997 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
998 error += " due to bad method ";
999 error += PrettyMethod(method_idx, *dex_file);
1000 return false;
1001 }
1002 it.Next();
1003 }
1004 while (it.HasNextVirtualMethod()) {
1005 uint32_t method_idx = it.GetMemberIndex();
1006 if (!VerifyMethod(method_idx, dex_file, dex_cache, class_loader, class_def_idx,
1007 it.GetMethodCodeItem())) {
1008 error = "Verifier rejected class";
1009 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
1010 error += " due to bad method ";
1011 error += PrettyMethod(method_idx, *dex_file);
1012 return false;
1013 }
1014 it.Next();
1015 }
1016 return true;
1017}
1018
1019bool DexVerifier::VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
1020 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item) {
1021 DexVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item);
1022
1023 // Without a method*, we can only verify the struture.
1024 bool success = verifier.VerifyStructure();
1025 CHECK_EQ(success, verifier.failure_ == VERIFY_ERROR_NONE);
1026
1027 // We expect either success and no verification error, or failure and a generic failure to
1028 // reject the class.
1029 if (success) {
1030 if (verifier.failure_ != VERIFY_ERROR_NONE) {
1031 LOG(FATAL) << "Unhandled failure in verification of " << PrettyMethod(method_idx, *dex_file)
1032 << std::endl << verifier.fail_messages_;
1033 }
1034 } else {
1035 LOG(INFO) << "Verification error in " << PrettyMethod(method_idx, *dex_file) << " "
1036 << verifier.fail_messages_.str();
1037 if (gDebugVerify) {
1038 std::cout << std::endl << verifier.info_messages_.str();
1039 verifier.Dump(std::cout);
1040 }
jeffhaod5347e02012-03-22 17:25:05 -07001041 DCHECK((verifier.failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) ||
1042 (verifier.failure_ == VERIFY_ERROR_BAD_CLASS_HARD)) << verifier.failure_;
jeffhaof56197c2012-03-05 18:01:54 -08001043 }
1044 return success;
1045}
1046
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001047DexVerifier::DexVerifier(Method* method)
1048 : work_insn_idx_(-1),
1049 method_(method),
1050 failure_(VERIFY_ERROR_NONE),
1051 new_instance_count_(0),
1052 monitor_enter_count_(0) {
1053 CHECK(method != NULL);
jeffhaof56197c2012-03-05 18:01:54 -08001054 dex_cache_ = method->GetDeclaringClass()->GetDexCache();
1055 class_loader_ = method->GetDeclaringClass()->GetClassLoader();
Brian Carlstromc12a17a2012-01-17 18:02:32 -08001056 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
jeffhaof56197c2012-03-05 18:01:54 -08001057 dex_file_ = &class_linker->FindDexFile(dex_cache_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 code_item_ = dex_file_->GetCodeItem(method->GetCodeItemOffset());
jeffhaof56197c2012-03-05 18:01:54 -08001059 const DexFile::ClassDef* class_def = ClassHelper(method_->GetDeclaringClass()).GetClassDef();
1060 class_def_idx_ = dex_file_->GetIndexForClassDef(*class_def);
jeffhaoba5ebb92011-08-25 17:24:37 -07001061}
1062
jeffhaof56197c2012-03-05 18:01:54 -08001063DexVerifier::DexVerifier(const DexFile* dex_file, DexCache* dex_cache,
1064 const ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item)
1065 : work_insn_idx_(-1),
1066 method_(NULL),
1067 dex_file_(dex_file),
1068 dex_cache_(dex_cache),
1069 class_loader_(class_loader),
1070 class_def_idx_(class_def_idx),
1071 code_item_(code_item),
1072 failure_(VERIFY_ERROR_NONE),
1073 new_instance_count_(0),
1074 monitor_enter_count_(0) {
1075}
1076
1077bool DexVerifier::VerifyAll() {
1078 CHECK(method_ != NULL);
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 // If there aren't any instructions, make sure that's expected, then exit successfully.
1080 if (code_item_ == NULL) {
1081 if (!method_->IsNative() && !method_->IsAbstract()) {
jeffhaod5347e02012-03-22 17:25:05 -07001082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -07001083 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 } else {
1085 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001086 }
jeffhaobdb76512011-09-07 11:43:16 -07001087 }
jeffhaof56197c2012-03-05 18:01:54 -08001088 return VerifyStructure() && VerifyCodeFlow();
1089}
1090
1091bool DexVerifier::VerifyStructure() {
1092 if (code_item_ == NULL) {
1093 return true;
1094 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001095 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
1096 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
1098 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 // Allocate and initialize an array to hold instruction data.
1102 insn_flags_.reset(new InsnFlags[code_item_->insns_size_in_code_units_]());
1103 // Run through the instructions and see if the width checks out.
1104 bool result = ComputeWidthsAndCountOps();
1105 // Flag instructions guarded by a "try" block and check exception handlers.
1106 result = result && ScanTryCatchBlocks();
1107 // Perform static instruction verification.
1108 result = result && VerifyInstructions();
jeffhaobdb76512011-09-07 11:43:16 -07001109 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001110}
1111
Ian Rogers47a05882012-02-03 12:23:33 -08001112std::ostream& DexVerifier::Fail(VerifyError error) {
1113 CHECK_EQ(failure_, VERIFY_ERROR_NONE);
Ian Rogers47a05882012-02-03 12:23:33 -08001114 if (Runtime::Current()->IsCompiler()) {
jeffhaod1224c72012-02-29 13:43:08 -08001115 switch (error) {
1116 // If we're optimistically running verification at compile time, turn NO_xxx and ACCESS_xxx
jeffhaod5347e02012-03-22 17:25:05 -07001117 // errors into soft verification errors so that we re-verify at runtime. We may fail to find
1118 // or to agree on access because of not yet available class loaders, or class loaders that
1119 // will differ at runtime.
Ian Rogers47a05882012-02-03 12:23:33 -08001120 case VERIFY_ERROR_NO_CLASS:
1121 case VERIFY_ERROR_NO_FIELD:
1122 case VERIFY_ERROR_NO_METHOD:
Ian Rogers9ada79c2012-02-03 14:29:52 -08001123 case VERIFY_ERROR_ACCESS_CLASS:
1124 case VERIFY_ERROR_ACCESS_FIELD:
1125 case VERIFY_ERROR_ACCESS_METHOD:
jeffhaod5347e02012-03-22 17:25:05 -07001126 error = VERIFY_ERROR_BAD_CLASS_SOFT;
Ian Rogers47a05882012-02-03 12:23:33 -08001127 break;
jeffhaod5347e02012-03-22 17:25:05 -07001128 // Hard verification failures at compile time will still fail at runtime, so the class is
1129 // marked as rejected to prevent it from being compiled.
1130 case VERIFY_ERROR_BAD_CLASS_HARD: {
jeffhaof56197c2012-03-05 18:01:54 -08001131 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -08001132 AddRejectedClass(ref);
1133 break;
1134 }
Ian Rogers47a05882012-02-03 12:23:33 -08001135 default:
1136 break;
1137 }
1138 }
1139 failure_ = error;
1140 return fail_messages_ << "VFY: " << PrettyMethod(method_)
1141 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
1142}
1143
Ian Rogersd81871c2011-10-03 13:57:23 -07001144bool DexVerifier::ComputeWidthsAndCountOps() {
1145 const uint16_t* insns = code_item_->insns_;
1146 size_t insns_size = code_item_->insns_size_in_code_units_;
1147 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -07001148 size_t new_instance_count = 0;
1149 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001151
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001153 Instruction::Code opcode = inst->Opcode();
1154 if (opcode == Instruction::NEW_INSTANCE) {
1155 new_instance_count++;
1156 } else if (opcode == Instruction::MONITOR_ENTER) {
1157 monitor_enter_count++;
1158 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 size_t inst_size = inst->SizeInCodeUnits();
1160 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
1161 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -07001162 inst = inst->Next();
1163 }
1164
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1167 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001168 return false;
1169 }
1170
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 new_instance_count_ = new_instance_count;
1172 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001173 return true;
1174}
1175
Ian Rogersd81871c2011-10-03 13:57:23 -07001176bool DexVerifier::ScanTryCatchBlocks() {
1177 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001178 if (tries_size == 0) {
1179 return true;
1180 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001182 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001183
1184 for (uint32_t idx = 0; idx < tries_size; idx++) {
1185 const DexFile::TryItem* try_item = &tries[idx];
1186 uint32_t start = try_item->start_addr_;
1187 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001188 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1190 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001191 return false;
1192 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001195 return false;
1196 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 for (uint32_t dex_pc = start; dex_pc < end;
1198 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
1199 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001200 }
1201 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001202 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -07001203 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001204 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001205 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001206 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001207 CatchHandlerIterator iterator(handlers_ptr);
1208 for (; iterator.HasNext(); iterator.Next()) {
1209 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001212 return false;
1213 }
jeffhao60f83e32012-02-13 17:16:30 -08001214 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1215 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
jeffhao60f83e32012-02-13 17:16:30 -08001217 << dex_pc << ")";
1218 return false;
1219 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001221 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1222 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001223 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
jeffhaof56197c2012-03-05 18:01:54 -08001224 Class* exception_type = linker->ResolveType(*dex_file_, iterator.GetHandlerTypeIndex(),
1225 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001226 if (exception_type == NULL) {
1227 DCHECK(Thread::Current()->IsExceptionPending());
1228 Thread::Current()->ClearException();
1229 }
1230 }
jeffhaobdb76512011-09-07 11:43:16 -07001231 }
Ian Rogers0571d352011-11-03 19:51:38 -07001232 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001233 }
jeffhaobdb76512011-09-07 11:43:16 -07001234 return true;
1235}
1236
Ian Rogersd81871c2011-10-03 13:57:23 -07001237bool DexVerifier::VerifyInstructions() {
1238 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001239
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 /* Flag the start of the method as a branch target. */
1241 insn_flags_[0].SetBranchTarget();
1242
1243 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001244 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001246 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
jeffhaod1224c72012-02-29 13:43:08 -08001247 fail_messages_ << "Rejecting opcode " << inst->Name() << " at " << dex_pc;
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 return false;
1249 }
1250 /* Flag instructions that are garbage collection points */
1251 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
1252 insn_flags_[dex_pc].SetGcPoint();
1253 }
1254 dex_pc += inst->SizeInCodeUnits();
1255 inst = inst->Next();
1256 }
1257 return true;
1258}
1259
1260bool DexVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001261 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 bool result = true;
1263 switch (inst->GetVerifyTypeArgumentA()) {
1264 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001265 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 break;
1267 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001268 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 break;
1270 }
1271 switch (inst->GetVerifyTypeArgumentB()) {
1272 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001273 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 break;
1275 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001276 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 break;
1278 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 break;
1281 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 break;
1284 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001285 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 break;
1287 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001288 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 break;
1290 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001291 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 break;
1293 }
1294 switch (inst->GetVerifyTypeArgumentC()) {
1295 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001296 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 break;
1298 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001299 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 break;
1301 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001302 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 break;
1304 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001305 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 break;
1307 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001308 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 break;
1310 }
1311 switch (inst->GetVerifyExtraFlags()) {
1312 case Instruction::kVerifyArrayData:
1313 result = result && CheckArrayData(code_offset);
1314 break;
1315 case Instruction::kVerifyBranchTarget:
1316 result = result && CheckBranchTarget(code_offset);
1317 break;
1318 case Instruction::kVerifySwitchTargets:
1319 result = result && CheckSwitchTargets(code_offset);
1320 break;
1321 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 break;
1324 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 break;
1327 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 result = false;
1330 break;
1331 }
1332 return result;
1333}
1334
1335bool DexVerifier::CheckRegisterIndex(uint32_t idx) {
1336 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1338 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 return false;
1340 }
1341 return true;
1342}
1343
1344bool DexVerifier::CheckWideRegisterIndex(uint32_t idx) {
1345 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1347 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 return false;
1349 }
1350 return true;
1351}
1352
1353bool DexVerifier::CheckFieldIndex(uint32_t idx) {
1354 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1356 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 return false;
1358 }
1359 return true;
1360}
1361
1362bool DexVerifier::CheckMethodIndex(uint32_t idx) {
1363 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1365 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 return false;
1367 }
1368 return true;
1369}
1370
1371bool DexVerifier::CheckNewInstance(uint32_t idx) {
1372 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1374 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 return false;
1376 }
1377 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001378 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 return false;
1382 }
1383 return true;
1384}
1385
1386bool DexVerifier::CheckStringIndex(uint32_t idx) {
1387 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1389 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 return false;
1391 }
1392 return true;
1393}
1394
1395bool DexVerifier::CheckTypeIndex(uint32_t idx) {
1396 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1398 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 return false;
1400 }
1401 return true;
1402}
1403
1404bool DexVerifier::CheckNewArray(uint32_t idx) {
1405 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1407 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 return false;
1409 }
1410 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001411 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 const char* cp = descriptor;
1413 while (*cp++ == '[') {
1414 bracket_count++;
1415 }
1416 if (bracket_count == 0) {
1417 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -07001418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 } else if (bracket_count > 255) {
1421 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -07001422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 return false;
1424 }
1425 return true;
1426}
1427
1428bool DexVerifier::CheckArrayData(uint32_t cur_offset) {
1429 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1430 const uint16_t* insns = code_item_->insns_ + cur_offset;
1431 const uint16_t* array_data;
1432 int32_t array_data_offset;
1433
1434 DCHECK_LT(cur_offset, insn_count);
1435 /* make sure the start of the array data table is in range */
1436 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1437 if ((int32_t) cur_offset + array_data_offset < 0 ||
1438 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
1440 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 return false;
1442 }
1443 /* offset to array data table is a relative branch-style offset */
1444 array_data = insns + array_data_offset;
1445 /* make sure the table is 32-bit aligned */
1446 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1448 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 return false;
1450 }
1451 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001452 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1454 /* make sure the end of the switch is in range */
1455 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1457 << ", data offset " << array_data_offset << ", end "
1458 << cur_offset + array_data_offset + table_size
1459 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 return false;
1461 }
1462 return true;
1463}
1464
1465bool DexVerifier::CheckBranchTarget(uint32_t cur_offset) {
1466 int32_t offset;
1467 bool isConditional, selfOkay;
1468 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1469 return false;
1470 }
1471 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 return false;
1474 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001475 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1476 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 return false;
1480 }
1481 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1482 int32_t abs_offset = cur_offset + offset;
1483 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001485 << reinterpret_cast<void*>(abs_offset) << ") at "
1486 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 return false;
1488 }
1489 insn_flags_[abs_offset].SetBranchTarget();
1490 return true;
1491}
1492
1493bool DexVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
1494 bool* selfOkay) {
1495 const uint16_t* insns = code_item_->insns_ + cur_offset;
1496 *pConditional = false;
1497 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 switch (*insns & 0xff) {
1499 case Instruction::GOTO:
1500 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 break;
1502 case Instruction::GOTO_32:
1503 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 *selfOkay = true;
1505 break;
1506 case Instruction::GOTO_16:
1507 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 break;
1509 case Instruction::IF_EQ:
1510 case Instruction::IF_NE:
1511 case Instruction::IF_LT:
1512 case Instruction::IF_GE:
1513 case Instruction::IF_GT:
1514 case Instruction::IF_LE:
1515 case Instruction::IF_EQZ:
1516 case Instruction::IF_NEZ:
1517 case Instruction::IF_LTZ:
1518 case Instruction::IF_GEZ:
1519 case Instruction::IF_GTZ:
1520 case Instruction::IF_LEZ:
1521 *pOffset = (int16_t) insns[1];
1522 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 break;
1524 default:
1525 return false;
1526 break;
1527 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 return true;
1529}
1530
Ian Rogersd81871c2011-10-03 13:57:23 -07001531bool DexVerifier::CheckSwitchTargets(uint32_t cur_offset) {
1532 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001533 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001535 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1537 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
1539 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001540 return false;
1541 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001544 /* make sure the table is 32-bit aligned */
1545 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1547 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 return false;
1549 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 uint32_t switch_count = switch_insns[1];
1551 int32_t keys_offset, targets_offset;
1552 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1554 /* 0=sig, 1=count, 2/3=firstKey */
1555 targets_offset = 4;
1556 keys_offset = -1;
1557 expected_signature = Instruction::kPackedSwitchSignature;
1558 } else {
1559 /* 0=sig, 1=count, 2..count*2 = keys */
1560 keys_offset = 2;
1561 targets_offset = 2 + 2 * switch_count;
1562 expected_signature = Instruction::kSparseSwitchSignature;
1563 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001565 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1567 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001568 return false;
1569 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001570 /* make sure the end of the switch is in range */
1571 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
1573 << switch_offset << ", end "
1574 << (cur_offset + switch_offset + table_size)
1575 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001576 return false;
1577 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001578 /* for a sparse switch, verify the keys are in ascending order */
1579 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1581 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1583 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1584 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1586 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001587 return false;
1588 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001589 last_key = key;
1590 }
1591 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001592 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 for (uint32_t targ = 0; targ < switch_count; targ++) {
1594 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1595 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1596 int32_t abs_offset = cur_offset + offset;
1597 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001599 << reinterpret_cast<void*>(abs_offset) << ") at "
1600 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001601 return false;
1602 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 insn_flags_[abs_offset].SetBranchTarget();
1604 }
1605 return true;
1606}
1607
1608bool DexVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
1609 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 return false;
1612 }
1613 uint16_t registers_size = code_item_->registers_size_;
1614 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001615 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1617 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 return false;
1619 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001620 }
1621
1622 return true;
1623}
1624
Ian Rogersd81871c2011-10-03 13:57:23 -07001625bool DexVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
1626 uint16_t registers_size = code_item_->registers_size_;
1627 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1628 // integer overflow when adding them here.
1629 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
1631 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001632 return false;
1633 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001634 return true;
1635}
1636
Brian Carlstrom75412882012-01-18 01:26:54 -08001637const std::vector<uint8_t>* CreateLengthPrefixedGcMap(const std::vector<uint8_t>& gc_map) {
1638 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
1639 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1640 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1641 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1642 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1643 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1644 gc_map.begin(),
1645 gc_map.end());
1646 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1647 DCHECK_EQ(gc_map.size(),
1648 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1649 (length_prefixed_gc_map->at(1) << 16) |
1650 (length_prefixed_gc_map->at(2) << 8) |
1651 (length_prefixed_gc_map->at(3) << 0)));
1652 return length_prefixed_gc_map;
1653}
1654
Ian Rogersd81871c2011-10-03 13:57:23 -07001655bool DexVerifier::VerifyCodeFlow() {
1656 uint16_t registers_size = code_item_->registers_size_;
1657 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001658
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001660 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1661 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 }
1663 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -07001664 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -07001665
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 work_line_.reset(new RegisterLine(registers_size, this));
1667 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001668
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 /* Initialize register types of method arguments. */
1670 if (!SetTypesFromSignature()) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001671 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
1672 fail_messages_ << "Bad signature in " << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 return false;
1674 }
1675 /* Perform code flow verification. */
1676 if (!CodeFlowVerifyMethod()) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001677 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001679 }
1680
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001682 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1683 if (map.get() == NULL) {
1684 DCHECK_NE(failure_, VERIFY_ERROR_NONE);
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 return false; // Not a real failure, but a failure to encode
1686 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001687#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001688 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689#endif
Brian Carlstrom75412882012-01-18 01:26:54 -08001690 const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
1691 Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
1692 verifier::DexVerifier::SetGcMap(ref, *gc_map);
Logan Chienfca7e872011-12-20 20:08:22 +08001693
Brian Carlstrom75412882012-01-18 01:26:54 -08001694 method_->SetGcMap(&gc_map->at(0));
Logan Chiendd361c92012-04-10 23:40:37 +08001695
1696#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +08001697 /* Generate Inferred Register Category for LLVM-based Code Generator */
1698 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Logan Chiendd361c92012-04-10 23:40:37 +08001699 verifier::DexVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +08001700#endif
1701
jeffhaobdb76512011-09-07 11:43:16 -07001702 return true;
1703}
1704
Ian Rogersd81871c2011-10-03 13:57:23 -07001705void DexVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001706 if (code_item_ == NULL) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 os << "Native method" << std::endl;
1708 return;
jeffhaobdb76512011-09-07 11:43:16 -07001709 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 DCHECK(code_item_ != NULL);
1711 const Instruction* inst = Instruction::At(code_item_->insns_);
1712 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1713 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Elliott Hughesaa6e1cd2012-01-18 19:26:06 -08001714 os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].Dump()
Ian Rogers2c8a8572011-10-24 17:11:36 -07001715 << " " << inst->DumpHex(5) << " " << inst->DumpString(dex_file_) << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1717 if (reg_line != NULL) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07001718 os << reg_line->Dump() << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07001719 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001720 inst = inst->Next();
1721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722}
1723
Ian Rogersd81871c2011-10-03 13:57:23 -07001724static bool IsPrimitiveDescriptor(char descriptor) {
1725 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001726 case 'I':
1727 case 'C':
1728 case 'S':
1729 case 'B':
1730 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001731 case 'F':
1732 case 'D':
1733 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001735 default:
1736 return false;
1737 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001738}
1739
Ian Rogersd81871c2011-10-03 13:57:23 -07001740bool DexVerifier::SetTypesFromSignature() {
1741 RegisterLine* reg_line = reg_table_.GetLine(0);
1742 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1743 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001744
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1746 //Include the "this" pointer.
1747 size_t cur_arg = 0;
1748 if (!method_->IsStatic()) {
1749 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1750 // argument as uninitialized. This restricts field access until the superclass constructor is
1751 // called.
1752 Class* declaring_class = method_->GetDeclaringClass();
1753 if (method_->IsConstructor() && !declaring_class->IsObjectClass()) {
1754 reg_line->SetRegisterType(arg_start + cur_arg,
1755 reg_types_.UninitializedThisArgument(declaring_class));
1756 } else {
1757 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.FromClass(declaring_class));
jeffhaobdb76512011-09-07 11:43:16 -07001758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001760 }
1761
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001762 const DexFile::ProtoId& proto_id =
1763 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(method_->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07001764 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001765
1766 for (; iterator.HasNext(); iterator.Next()) {
1767 const char* descriptor = iterator.GetDescriptor();
1768 if (descriptor == NULL) {
1769 LOG(FATAL) << "Null descriptor";
1770 }
1771 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1773 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 return false;
1775 }
1776 switch (descriptor[0]) {
1777 case 'L':
1778 case '[':
1779 // We assume that reference arguments are initialized. The only way it could be otherwise
1780 // (assuming the caller was verified) is if the current method is <init>, but in that case
1781 // it's effectively considered initialized the instant we reach here (in the sense that we
1782 // can return without doing anything or call virtual methods).
1783 {
1784 const RegType& reg_type =
1785 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogers84fa0742011-10-25 18:13:30 -07001786 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
1788 break;
1789 case 'Z':
1790 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1791 break;
1792 case 'C':
1793 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1794 break;
1795 case 'B':
1796 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1797 break;
1798 case 'I':
1799 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1800 break;
1801 case 'S':
1802 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1803 break;
1804 case 'F':
1805 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1806 break;
1807 case 'J':
1808 case 'D': {
1809 const RegType& low_half = descriptor[0] == 'J' ? reg_types_.Long() : reg_types_.Double();
1810 reg_line->SetRegisterType(arg_start + cur_arg, low_half); // implicitly sets high-register
1811 cur_arg++;
1812 break;
1813 }
1814 default:
jeffhaod5347e02012-03-22 17:25:05 -07001815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 return false;
1817 }
1818 cur_arg++;
1819 }
1820 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 return false;
1823 }
1824 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1825 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1826 // format. Only major difference from the method argument format is that 'V' is supported.
1827 bool result;
1828 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1829 result = descriptor[1] == '\0';
1830 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1831 size_t i = 0;
1832 do {
1833 i++;
1834 } while (descriptor[i] == '['); // process leading [
1835 if (descriptor[i] == 'L') { // object array
1836 do {
1837 i++; // find closing ;
1838 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1839 result = descriptor[i] == ';';
1840 } else { // primitive array
1841 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1842 }
1843 } else if (descriptor[0] == 'L') {
1844 // could be more thorough here, but shouldn't be required
1845 size_t i = 0;
1846 do {
1847 i++;
1848 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1849 result = descriptor[i] == ';';
1850 } else {
1851 result = false;
1852 }
1853 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1855 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
1857 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001858}
1859
Ian Rogersd81871c2011-10-03 13:57:23 -07001860bool DexVerifier::CodeFlowVerifyMethod() {
1861 const uint16_t* insns = code_item_->insns_;
1862 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001863
jeffhaobdb76512011-09-07 11:43:16 -07001864 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 insn_flags_[0].SetChanged();
1866 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001867
jeffhaobdb76512011-09-07 11:43:16 -07001868 /* Continue until no instructions are marked "changed". */
1869 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1871 uint32_t insn_idx = start_guess;
1872 for (; insn_idx < insns_size; insn_idx++) {
1873 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 }
jeffhaobdb76512011-09-07 11:43:16 -07001876 if (insn_idx == insns_size) {
1877 if (start_guess != 0) {
1878 /* try again, starting from the top */
1879 start_guess = 0;
1880 continue;
1881 } else {
1882 /* all flags are clear */
1883 break;
1884 }
1885 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 // We carry the working set of registers from instruction to instruction. If this address can
1887 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1888 // "changed" flags, we need to load the set of registers from the table.
1889 // Because we always prefer to continue on to the next instruction, we should never have a
1890 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1891 // target.
1892 work_insn_idx_ = insn_idx;
1893 if (insn_flags_[insn_idx].IsBranchTarget()) {
1894 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001895 } else {
1896#ifndef NDEBUG
1897 /*
1898 * Sanity check: retrieve the stored register line (assuming
1899 * a full table) and make sure it actually matches.
1900 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1902 if (register_line != NULL) {
1903 if (work_line_->CompareLine(register_line) != 0) {
1904 Dump(std::cout);
1905 std::cout << info_messages_.str();
1906 LOG(FATAL) << "work_line diverged in " << PrettyMethod(method_)
Elliott Hughes398f64b2012-03-26 18:05:48 -07001907 << "@" << reinterpret_cast<void*>(work_insn_idx_) << std::endl
1908 << " work_line=" << *work_line_ << std::endl
1909 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 }
jeffhaobdb76512011-09-07 11:43:16 -07001911 }
1912#endif
1913 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 if (!CodeFlowVerifyInstruction(&start_guess)) {
1915 fail_messages_ << std::endl << PrettyMethod(method_) << " failed to verify";
jeffhaoba5ebb92011-08-25 17:24:37 -07001916 return false;
1917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 insn_flags_[insn_idx].SetVisited();
1920 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001921 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001922
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 if (DEAD_CODE_SCAN && ((method_->GetAccessFlags() & kAccWritable) == 0)) {
jeffhaobdb76512011-09-07 11:43:16 -07001924 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001925 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001926 * (besides the wasted space), but it indicates a flaw somewhere
1927 * down the line, possibly in the verifier.
1928 *
1929 * If we've substituted "always throw" instructions into the stream,
1930 * we are almost certainly going to have some dead code.
1931 */
1932 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 uint32_t insn_idx = 0;
1934 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001935 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001936 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001937 * may or may not be preceded by a padding NOP (for alignment).
1938 */
1939 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1940 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1941 insns[insn_idx] == Instruction::kArrayDataSignature ||
1942 (insns[insn_idx] == Instruction::NOP &&
1943 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1944 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1945 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001947 }
1948
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001950 if (dead_start < 0)
1951 dead_start = insn_idx;
1952 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001953 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001954 dead_start = -1;
1955 }
1956 }
1957 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001958 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001959 }
1960 }
jeffhaobdb76512011-09-07 11:43:16 -07001961 return true;
1962}
1963
Ian Rogersd81871c2011-10-03 13:57:23 -07001964bool DexVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001965#ifdef VERIFIER_STATS
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 if (CurrentInsnFlags().IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 gDvm.verifierStats.instrsReexamined++;
1968 } else {
1969 gDvm.verifierStats.instrsExamined++;
1970 }
1971#endif
1972
1973 /*
1974 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001975 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001976 * control to another statement:
1977 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001978 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001979 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001980 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001981 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001982 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001983 * throw an exception that is handled by an encompassing "try"
1984 * block.
1985 *
1986 * We can also return, in which case there is no successor instruction
1987 * from this point.
1988 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001990 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1992 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 DecodedInstruction dec_insn(inst);
1994 int opcode_flags = Instruction::Flags(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001995
jeffhaobdb76512011-09-07 11:43:16 -07001996 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001997 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001998 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 // Generate processing back trace to debug verifier
Ian Rogers5ed29bf2011-10-26 12:22:21 -07002000 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2001 << *work_line_.get() << std::endl;
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 }
jeffhaobdb76512011-09-07 11:43:16 -07002003
2004 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002005 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002006 * can throw an exception, we will copy/merge this into the "catch"
2007 * address rather than work_line, because we don't want the result
2008 * from the "successful" code path (e.g. a check-cast that "improves"
2009 * a type) to be visible to the exception handler.
2010 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002011 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags().IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07002013 } else {
2014#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002016#endif
2017 }
2018
Elliott Hughesadb8c672012-03-06 16:49:32 -08002019 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07002020 case Instruction::NOP:
2021 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002022 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002023 * a signature that looks like a NOP; if we see one of these in
2024 * the course of executing code then we have a problem.
2025 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002028 }
2029 break;
2030
2031 case Instruction::MOVE:
2032 case Instruction::MOVE_FROM16:
2033 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002034 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::MOVE_WIDE:
2037 case Instruction::MOVE_WIDE_FROM16:
2038 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002039 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::MOVE_OBJECT:
2042 case Instruction::MOVE_OBJECT_FROM16:
2043 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046
2047 /*
2048 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002049 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002050 * might want to hold the result in an actual CPU register, so the
2051 * Dalvik spec requires that these only appear immediately after an
2052 * invoke or filled-new-array.
2053 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002054 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002055 * redundant with the reset done below, but it can make the debug info
2056 * easier to read in some cases.)
2057 */
2058 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002062 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002065 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
2067
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07002069 /*
jeffhao60f83e32012-02-13 17:16:30 -08002070 * This statement can only appear as the first instruction in an exception handler. We verify
2071 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002072 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002073 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002074 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 }
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::RETURN_VOID:
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2079 if (!GetMethodReturnType().IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07002080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 }
jeffhaobdb76512011-09-07 11:43:16 -07002082 }
2083 break;
2084 case Instruction::RETURN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002086 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 const RegType& return_type = GetMethodReturnType();
2088 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 } else {
2091 // Compilers may generate synthetic functions that write byte values into boolean fields.
2092 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08002093 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2095 ((return_type.IsBoolean() || return_type.IsByte() ||
2096 return_type.IsShort() || return_type.IsChar()) &&
2097 src_type.IsInteger()));
2098 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002099 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002101 fail_messages_ << " return-1nr on invalid register v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 }
jeffhaobdb76512011-09-07 11:43:16 -07002103 }
2104 }
2105 break;
2106 case Instruction::RETURN_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07002108 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 const RegType& return_type = GetMethodReturnType();
2110 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 } else {
2113 /* check the register contents */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002114 work_line_->VerifyRegisterType(dec_insn.vA, return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002116 fail_messages_ << " return-wide on invalid register pair v" << dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 }
jeffhaobdb76512011-09-07 11:43:16 -07002118 }
2119 }
2120 break;
2121 case Instruction::RETURN_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 if (!method_->IsConstructor() || work_line_->CheckConstructorReturn()) {
2123 const RegType& return_type = GetMethodReturnType();
2124 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 } else {
2127 /* return_type is the *expected* return type, not register value */
2128 DCHECK(!return_type.IsZero());
2129 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08002130 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07002131 // Disallow returning uninitialized values and verify that the reference in vAA is an
2132 // instance of the "return_type"
2133 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002134 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002135 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
Ian Rogers9074b992011-10-26 17:41:55 -07002137 << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07002138 }
2139 }
2140 }
2141 break;
2142
2143 case Instruction::CONST_4:
2144 case Instruction::CONST_16:
2145 case Instruction::CONST:
2146 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const((int32_t) dec_insn.vB));
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
2149 case Instruction::CONST_HIGH16:
2150 /* could be boolean, int, float, or a null reference */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 work_line_->SetRegisterType(dec_insn.vA,
2152 reg_types_.FromCat1Const((int32_t) dec_insn.vB << 16));
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::CONST_WIDE_16:
2155 case Instruction::CONST_WIDE_32:
2156 case Instruction::CONST_WIDE:
2157 case Instruction::CONST_WIDE_HIGH16:
2158 /* could be long or double; resolved upon use */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002159 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::CONST_STRING:
2162 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002163 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002166 // Get type from instruction if unresolved then we need an access check
2167 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08002168 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002169 // Register holds class, ie its type is class, but on error we keep it Unknown
Elliott Hughesadb8c672012-03-06 16:49:32 -08002170 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers28ad40d2011-10-27 15:19:26 -07002171 res_type.IsUnknown() ? res_type : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002175 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177 case Instruction::MONITOR_EXIT:
2178 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002179 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002180 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002181 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002182 * to the need to handle asynchronous exceptions, a now-deprecated
2183 * feature that Dalvik doesn't support.)
2184 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002185 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002186 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002187 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002188 * structured locking checks are working, the former would have
2189 * failed on the -enter instruction, and the latter is impossible.
2190 *
2191 * This is fortunate, because issue 3221411 prevents us from
2192 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002193 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002194 * some catch blocks (which will show up as "dead" code when
2195 * we skip them here); if we can't, then the code path could be
2196 * "live" so we still need to check it.
2197 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002198 opcode_flags &= ~Instruction::kThrow;
2199 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201
Ian Rogers28ad40d2011-10-27 15:19:26 -07002202 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002204 /*
2205 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2206 * could be a "upcast" -- not expected, so we don't try to address it.)
2207 *
2208 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002209 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002210 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002211 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002212 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002213 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogers9f1ab122011-12-12 08:52:43 -08002214 if (res_type.IsUnknown()) {
2215 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2216 break; // couldn't resolve class
2217 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2219 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08002220 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002221 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002223 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07002225 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002226 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002227 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002229 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002232 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 }
2234 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002235 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002237 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002240 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 }
2242 }
2243 break;
2244 }
2245 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002246 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
jeffhao8cd6dda2012-02-22 10:15:34 -08002247 if (res_type.IsUnknown()) {
2248 CHECK_NE(failure_, VERIFY_ERROR_NONE);
2249 break; // couldn't resolve class
2250 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002251 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2252 // can't create an instance of an interface or abstract class */
2253 if (!res_type.IsInstantiableTypes()) {
2254 Fail(VERIFY_ERROR_INSTANTIATION)
2255 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002257 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
2258 // Any registers holding previous allocations from this address that have not yet been
2259 // initialized must be marked invalid.
2260 work_line_->MarkUninitRefsAsInvalid(uninit_type);
2261 // add the new uninitialized reference to the register state
Elliott Hughesadb8c672012-03-06 16:49:32 -08002262 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 }
2264 break;
2265 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002266 case Instruction::NEW_ARRAY:
2267 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08002270 VerifyNewArray(dec_insn, true, false);
2271 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002273 case Instruction::FILLED_NEW_ARRAY_RANGE:
2274 VerifyNewArray(dec_insn, true, true);
2275 just_set_result = true; // Filled new array range sets result register
2276 break;
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::CMPL_FLOAT:
2278 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002279 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002280 break;
2281 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002282 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002283 break;
2284 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002285 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002286 break;
2287 case Instruction::CMPL_DOUBLE:
2288 case Instruction::CMPG_DOUBLE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002290 break;
2291 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002292 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Double())) {
jeffhao457cc512012-02-02 16:55:13 -08002293 break;
2294 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002295 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
2297 case Instruction::CMP_LONG:
Elliott Hughesadb8c672012-03-06 16:49:32 -08002298 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002299 break;
2300 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002301 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Long())) {
jeffhao457cc512012-02-02 16:55:13 -08002302 break;
2303 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002304 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002307 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002308 if (!reg_types_.JavaLangThrowable().IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002309 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002310 }
2311 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
jeffhaobdb76512011-09-07 11:43:16 -07002313 case Instruction::GOTO:
2314 case Instruction::GOTO_16:
2315 case Instruction::GOTO_32:
2316 /* no effect on or use of registers */
2317 break;
2318
2319 case Instruction::PACKED_SWITCH:
2320 case Instruction::SPARSE_SWITCH:
2321 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002322 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 case Instruction::FILL_ARRAY_DATA: {
2326 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08002328 /* array_type can be null if the reg type is Zero */
2329 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002330 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002332 } else {
jeffhao457cc512012-02-02 16:55:13 -08002333 const RegType& component_type = reg_types_.GetComponentType(array_type,
2334 method_->GetDeclaringClass()->GetClassLoader());
2335 DCHECK(!component_type.IsUnknown());
2336 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2338 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 } else {
jeffhao457cc512012-02-02 16:55:13 -08002340 // Now verify if the element width in the table matches the element width declared in
2341 // the array
2342 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2343 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002345 } else {
2346 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2347 // Since we don't compress the data in Dex, expect to see equal width of data stored
2348 // in the table and expected from the array class.
2349 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2351 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002352 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 }
2354 }
jeffhaobdb76512011-09-07 11:43:16 -07002355 }
2356 }
2357 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 }
jeffhaobdb76512011-09-07 11:43:16 -07002359 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002361 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2362 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 bool mismatch = false;
2364 if (reg_type1.IsZero()) { // zero then integral or reference expected
2365 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2366 } else if (reg_type1.IsReferenceTypes()) { // both references?
2367 mismatch = !reg_type2.IsReferenceTypes();
2368 } else { // both integral?
2369 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2370 }
2371 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07002372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
2373 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002374 }
2375 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002376 }
jeffhaobdb76512011-09-07 11:43:16 -07002377 case Instruction::IF_LT:
2378 case Instruction::IF_GE:
2379 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002381 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
2382 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2385 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002386 }
2387 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 case Instruction::IF_LTZ:
2398 case Instruction::IF_GEZ:
2399 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002401 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2404 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 }
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
2410 break;
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 VerifyAGet(dec_insn, reg_types_.Byte(), true);
2413 break;
jeffhaobdb76512011-09-07 11:43:16 -07002414 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 VerifyAGet(dec_insn, reg_types_.Char(), true);
2416 break;
jeffhaobdb76512011-09-07 11:43:16 -07002417 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::AGET:
2421 VerifyAGet(dec_insn, reg_types_.Integer(), true);
2422 break;
jeffhaobdb76512011-09-07 11:43:16 -07002423 case Instruction::AGET_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 VerifyAGet(dec_insn, reg_types_.Long(), true);
2425 break;
2426 case Instruction::AGET_OBJECT:
2427 VerifyAGet(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 case Instruction::APUT_BOOLEAN:
2431 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
2432 break;
2433 case Instruction::APUT_BYTE:
2434 VerifyAPut(dec_insn, reg_types_.Byte(), true);
2435 break;
2436 case Instruction::APUT_CHAR:
2437 VerifyAPut(dec_insn, reg_types_.Char(), true);
2438 break;
2439 case Instruction::APUT_SHORT:
2440 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::APUT_WIDE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 VerifyAPut(dec_insn, reg_types_.Long(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::APUT_OBJECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 VerifyAPut(dec_insn, reg_types_.JavaLangObject(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451
jeffhaobdb76512011-09-07 11:43:16 -07002452 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002453 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 break;
jeffhaobdb76512011-09-07 11:43:16 -07002455 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002456 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 break;
jeffhaobdb76512011-09-07 11:43:16 -07002458 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002459 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 break;
jeffhaobdb76512011-09-07 11:43:16 -07002461 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002462 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 break;
2464 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002465 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::IGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002468 VerifyISGet(dec_insn, reg_types_.Long(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::IGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002471 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 break;
jeffhaobdb76512011-09-07 11:43:16 -07002473
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002475 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 break;
2477 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002478 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 break;
2480 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002481 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 break;
2483 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002484 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002487 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::IPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002490 VerifyISPut(dec_insn, reg_types_.Long(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 break;
jeffhaobdb76512011-09-07 11:43:16 -07002492 case Instruction::IPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002493 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495
jeffhaobdb76512011-09-07 11:43:16 -07002496 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002497 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 break;
jeffhaobdb76512011-09-07 11:43:16 -07002499 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002500 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 break;
jeffhaobdb76512011-09-07 11:43:16 -07002502 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002503 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 break;
jeffhaobdb76512011-09-07 11:43:16 -07002505 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002506 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 break;
2508 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002509 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::SGET_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002512 VerifyISGet(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::SGET_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002515 VerifyISGet(dec_insn, reg_types_.JavaLangObject(), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 break;
2517
2518 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002519 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 break;
2521 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002522 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 break;
2524 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002525 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 break;
2527 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002528 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002531 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::SPUT_WIDE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002534 VerifyISPut(dec_insn, reg_types_.Long(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
2536 case Instruction::SPUT_OBJECT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07002537 VerifyISPut(dec_insn, reg_types_.JavaLangObject(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
2539
2540 case Instruction::INVOKE_VIRTUAL:
2541 case Instruction::INVOKE_VIRTUAL_RANGE:
2542 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002544 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
2545 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2546 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
2547 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL, is_range, is_super);
2549 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002550 const char* descriptor;
2551 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002552 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002553 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2554 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002555 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002556 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002557 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 }
Ian Rogers9074b992011-10-26 17:41:55 -07002559 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002560 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002562 just_set_result = true;
2563 }
2564 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 }
jeffhaobdb76512011-09-07 11:43:16 -07002566 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002568 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT, is_range, false);
2570 if (failure_ == VERIFY_ERROR_NONE) {
jeffhaobdb76512011-09-07 11:43:16 -07002571 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 * Some additional checks when calling a constructor. We know from the invocation arg check
2573 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2574 * that to require that called_method->klass is the same as this->klass or this->super,
2575 * allowing the latter only if the "this" argument is the same as the "this" argument to
2576 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002577 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002578 bool is_constructor;
2579 if (called_method != NULL) {
2580 is_constructor = called_method->IsConstructor();
2581 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002582 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002583 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2584 const char* name = dex_file_->GetMethodName(method_id);
2585 is_constructor = strcmp(name, "<init>") == 0;
2586 }
2587 if (is_constructor) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2589 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591
2592 /* no null refs allowed (?) */
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 if (this_type.IsZero()) {
jeffhaod5347e02012-03-22 17:25:05 -07002594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002597 if (called_method != NULL) {
2598 Class* this_class = this_type.GetClass();
2599 DCHECK(this_class != NULL);
2600 /* must be in same class or in superclass */
2601 if (called_method->GetDeclaringClass() == this_class->GetSuperClass()) {
2602 if (this_class != method_->GetDeclaringClass()) {
jeffhaod5347e02012-03-22 17:25:05 -07002603 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers28ad40d2011-10-27 15:19:26 -07002604 << "invoke-direct <init> on super only allowed for 'this' in <init>";
2605 break;
2606 }
2607 } else if (called_method->GetDeclaringClass() != this_class) {
jeffhaod5347e02012-03-22 17:25:05 -07002608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-direct <init> must be on current class or super";
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 }
jeffhaobdb76512011-09-07 11:43:16 -07002611 }
2612
2613 /* arg must be an uninitialized reference */
Ian Rogers84fa0742011-10-25 18:13:30 -07002614 if (!this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 << this_type;
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 }
2619
2620 /*
Ian Rogers84fa0742011-10-25 18:13:30 -07002621 * Replace the uninitialized reference with an initialized one. We need to do this for all
2622 * registers that have the same object instance in them, not just the "this" register.
jeffhaobdb76512011-09-07 11:43:16 -07002623 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 work_line_->MarkRefsAsInitialized(this_type);
2625 if (failure_ != VERIFY_ERROR_NONE)
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002627 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002628 const char* descriptor;
2629 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002630 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002631 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2632 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002633 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002634 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002635 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002636 }
Ian Rogers9074b992011-10-26 17:41:55 -07002637 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002638 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002640 just_set_result = true;
2641 }
2642 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 }
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::INVOKE_STATIC:
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 case Instruction::INVOKE_STATIC_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002646 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 Method* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
2648 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002649 const char* descriptor;
2650 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002651 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002652 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2653 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002654 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002655 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002656 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002657 }
Ian Rogers9074b992011-10-26 17:41:55 -07002658 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002659 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 work_line_->SetResultRegisterType(return_type);
2661 just_set_result = true;
2662 }
jeffhaobdb76512011-09-07 11:43:16 -07002663 }
2664 break;
2665 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002667 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 Method* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
2669 if (failure_ == VERIFY_ERROR_NONE) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002670 if (abs_method != NULL) {
2671 Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersf3c1f782011-11-02 14:12:15 -07002672 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002673 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2674 << PrettyMethod(abs_method) << "'";
2675 break;
2676 }
2677 }
2678 /* Get the type of the "this" arg, which should either be a sub-interface of called
2679 * interface or Object (see comments in RegType::JoinClass).
2680 */
2681 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2682 if (failure_ == VERIFY_ERROR_NONE) {
2683 if (this_type.IsZero()) {
2684 /* null pointer always passes (and always fails at runtime) */
2685 } else {
2686 if (this_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
Ian Rogers28ad40d2011-10-27 15:19:26 -07002688 << this_type;
2689 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002691 // In the past we have tried to assert that "called_interface" is assignable
2692 // from "this_type.GetClass()", however, as we do an imprecise Join
2693 // (RegType::JoinClass) we don't have full information on what interfaces are
2694 // implemented by "this_type". For example, two classes may implement the same
2695 // interfaces and have a common parent that doesn't implement the interface. The
2696 // join will set "this_type" to the parent class and a test that this implements
2697 // the interface will incorrectly fail.
jeffhaobdb76512011-09-07 11:43:16 -07002698 }
2699 }
jeffhaobdb76512011-09-07 11:43:16 -07002700 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 * We don't have an object instance, so we can't find the concrete method. However, all of
2702 * the type information is in the abstract method, so we're good.
jeffhaobdb76512011-09-07 11:43:16 -07002703 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07002704 const char* descriptor;
2705 if (abs_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002706 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002707 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2708 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002709 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002710 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002711 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002712 }
Ian Rogers9074b992011-10-26 17:41:55 -07002713 const RegType& return_type =
Ian Rogers28ad40d2011-10-27 15:19:26 -07002714 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
2715 work_line_->SetResultRegisterType(return_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 work_line_->SetResultRegisterType(return_type);
jeffhaobdb76512011-09-07 11:43:16 -07002717 just_set_result = true;
2718 }
2719 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 case Instruction::NEG_INT:
2722 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725 case Instruction::NEG_LONG:
2726 case Instruction::NOT_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::NEG_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::INT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::INT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744 case Instruction::LONG_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::LONG_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::LONG_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Long());
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756 case Instruction::FLOAT_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 work_line_->CheckUnaryOp(dec_insn, reg_types_.Double(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002761 break;
2762 case Instruction::DOUBLE_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002764 break;
2765 case Instruction::DOUBLE_TO_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 work_line_->CheckUnaryOp(dec_insn, reg_types_.Long(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Double());
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002773 break;
2774 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002779 break;
2780
2781 case Instruction::ADD_INT:
2782 case Instruction::SUB_INT:
2783 case Instruction::MUL_INT:
2784 case Instruction::REM_INT:
2785 case Instruction::DIV_INT:
2786 case Instruction::SHL_INT:
2787 case Instruction::SHR_INT:
2788 case Instruction::USHR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::AND_INT:
2792 case Instruction::OR_INT:
2793 case Instruction::XOR_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002795 break;
2796 case Instruction::ADD_LONG:
2797 case Instruction::SUB_LONG:
2798 case Instruction::MUL_LONG:
2799 case Instruction::DIV_LONG:
2800 case Instruction::REM_LONG:
2801 case Instruction::AND_LONG:
2802 case Instruction::OR_LONG:
2803 case Instruction::XOR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002805 break;
2806 case Instruction::SHL_LONG:
2807 case Instruction::SHR_LONG:
2808 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 /* shift distance is Int, making these different from other binary operations */
2810 work_line_->CheckBinaryOp(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812 case Instruction::ADD_FLOAT:
2813 case Instruction::SUB_FLOAT:
2814 case Instruction::MUL_FLOAT:
2815 case Instruction::DIV_FLOAT:
2816 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819 case Instruction::ADD_DOUBLE:
2820 case Instruction::SUB_DOUBLE:
2821 case Instruction::MUL_DOUBLE:
2822 case Instruction::DIV_DOUBLE:
2823 case Instruction::REM_DOUBLE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 work_line_->CheckBinaryOp(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002825 break;
2826 case Instruction::ADD_INT_2ADDR:
2827 case Instruction::SUB_INT_2ADDR:
2828 case Instruction::MUL_INT_2ADDR:
2829 case Instruction::REM_INT_2ADDR:
2830 case Instruction::SHL_INT_2ADDR:
2831 case Instruction::SHR_INT_2ADDR:
2832 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002834 break;
2835 case Instruction::AND_INT_2ADDR:
2836 case Instruction::OR_INT_2ADDR:
2837 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002839 break;
2840 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843 case Instruction::ADD_LONG_2ADDR:
2844 case Instruction::SUB_LONG_2ADDR:
2845 case Instruction::MUL_LONG_2ADDR:
2846 case Instruction::DIV_LONG_2ADDR:
2847 case Instruction::REM_LONG_2ADDR:
2848 case Instruction::AND_LONG_2ADDR:
2849 case Instruction::OR_LONG_2ADDR:
2850 case Instruction::XOR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Long(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002852 break;
2853 case Instruction::SHL_LONG_2ADDR:
2854 case Instruction::SHR_LONG_2ADDR:
2855 case Instruction::USHR_LONG_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Long(), reg_types_.Long(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002857 break;
2858 case Instruction::ADD_FLOAT_2ADDR:
2859 case Instruction::SUB_FLOAT_2ADDR:
2860 case Instruction::MUL_FLOAT_2ADDR:
2861 case Instruction::DIV_FLOAT_2ADDR:
2862 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002864 break;
2865 case Instruction::ADD_DOUBLE_2ADDR:
2866 case Instruction::SUB_DOUBLE_2ADDR:
2867 case Instruction::MUL_DOUBLE_2ADDR:
2868 case Instruction::DIV_DOUBLE_2ADDR:
2869 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Double(), reg_types_.Double(), reg_types_.Double(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::ADD_INT_LIT16:
2873 case Instruction::RSUB_INT:
2874 case Instruction::MUL_INT_LIT16:
2875 case Instruction::DIV_INT_LIT16:
2876 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::AND_INT_LIT16:
2880 case Instruction::OR_INT_LIT16:
2881 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884 case Instruction::ADD_INT_LIT8:
2885 case Instruction::RSUB_INT_LIT8:
2886 case Instruction::MUL_INT_LIT8:
2887 case Instruction::DIV_INT_LIT8:
2888 case Instruction::REM_INT_LIT8:
2889 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002890 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002891 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002893 break;
2894 case Instruction::AND_INT_LIT8:
2895 case Instruction::OR_INT_LIT8:
2896 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899
2900 /*
2901 * This falls into the general category of "optimized" instructions,
jeffhaod1f0fde2011-09-08 17:25:33 -07002902 * which don't generally appear during verification. Because it's
jeffhaobdb76512011-09-07 11:43:16 -07002903 * inserted in the course of verification, we can expect to see it here.
2904 */
jeffhaob4df5142011-09-19 20:25:32 -07002905 case Instruction::THROW_VERIFICATION_ERROR:
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 /* These should never appear during verification. */
jeffhaobdb76512011-09-07 11:43:16 -07002909 case Instruction::UNUSED_EE:
2910 case Instruction::UNUSED_EF:
2911 case Instruction::UNUSED_F2:
2912 case Instruction::UNUSED_F3:
2913 case Instruction::UNUSED_F4:
2914 case Instruction::UNUSED_F5:
2915 case Instruction::UNUSED_F6:
2916 case Instruction::UNUSED_F7:
2917 case Instruction::UNUSED_F8:
2918 case Instruction::UNUSED_F9:
2919 case Instruction::UNUSED_FA:
2920 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002921 case Instruction::UNUSED_F0:
2922 case Instruction::UNUSED_F1:
2923 case Instruction::UNUSED_E3:
2924 case Instruction::UNUSED_E8:
2925 case Instruction::UNUSED_E7:
2926 case Instruction::UNUSED_E4:
2927 case Instruction::UNUSED_E9:
2928 case Instruction::UNUSED_FC:
2929 case Instruction::UNUSED_E5:
2930 case Instruction::UNUSED_EA:
2931 case Instruction::UNUSED_FD:
2932 case Instruction::UNUSED_E6:
2933 case Instruction::UNUSED_EB:
2934 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002935 case Instruction::UNUSED_3E:
2936 case Instruction::UNUSED_3F:
2937 case Instruction::UNUSED_40:
2938 case Instruction::UNUSED_41:
2939 case Instruction::UNUSED_42:
2940 case Instruction::UNUSED_43:
2941 case Instruction::UNUSED_73:
2942 case Instruction::UNUSED_79:
2943 case Instruction::UNUSED_7A:
2944 case Instruction::UNUSED_EC:
2945 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002947 break;
2948
2949 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002950 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002951 * complain if an instruction is missing (which is desirable).
2952 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002953 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002954
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 if (failure_ != VERIFY_ERROR_NONE) {
jeffhaod5347e02012-03-22 17:25:05 -07002956 if (failure_ == VERIFY_ERROR_BAD_CLASS_HARD || failure_ == VERIFY_ERROR_BAD_CLASS_SOFT) {
jeffhaobdb76512011-09-07 11:43:16 -07002957 /* immediate failure, reject class */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002958 fail_messages_ << std::endl << "Rejecting opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002959 return false;
2960 } else {
2961 /* replace opcode and continue on */
Ian Rogers2c8a8572011-10-24 17:11:36 -07002962 fail_messages_ << std::endl << "Replacing opcode " << inst->DumpString(dex_file_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 ReplaceFailingInstruction();
jeffhaobdb76512011-09-07 11:43:16 -07002964 /* IMPORTANT: method->insns may have been changed */
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 insns = code_item_->insns_ + work_insn_idx_;
jeffhaobdb76512011-09-07 11:43:16 -07002966 /* continue on as if we just handled a throw-verification-error */
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 failure_ = VERIFY_ERROR_NONE;
Elliott Hughesadb8c672012-03-06 16:49:32 -08002968 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002969 }
2970 }
jeffhaobdb76512011-09-07 11:43:16 -07002971 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 * If we didn't just set the result register, clear it out. This ensures that you can only use
2973 * "move-result" immediately after the result is set. (We could check this statically, but it's
2974 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002975 */
2976 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002978 }
2979
jeffhaoa0a764a2011-09-16 10:43:38 -07002980 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002981 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags().GetLengthInCodeUnits();
2983 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002985 return false;
2986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2988 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002989 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002990 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 }
2992 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2993 if (next_line != NULL) {
2994 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2995 // needed.
2996 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002997 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 }
jeffhaobdb76512011-09-07 11:43:16 -07002999 } else {
3000 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 * We're not recording register data for the next instruction, so we don't know what the prior
3002 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07003003 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003005 }
3006 }
3007
3008 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003009 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003010 *
3011 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003012 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003013 * somebody could get a reference field, check it for zero, and if the
3014 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003015 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003016 * that, and will reject the code.
3017 *
3018 * TODO: avoid re-fetching the branch target
3019 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003020 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003021 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003023 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003025 return false;
3026 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003027 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07003028 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003029 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 }
jeffhaobdb76512011-09-07 11:43:16 -07003031 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003033 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 }
jeffhaobdb76512011-09-07 11:43:16 -07003035 }
3036
3037 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003038 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003039 *
3040 * We've already verified that the table is structurally sound, so we
3041 * just need to walk through and tag the targets.
3042 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003043 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003044 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3045 const uint16_t* switch_insns = insns + offset_to_switch;
3046 int switch_count = switch_insns[1];
3047 int offset_to_targets, targ;
3048
3049 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3050 /* 0 = sig, 1 = count, 2/3 = first key */
3051 offset_to_targets = 4;
3052 } else {
3053 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003054 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003055 offset_to_targets = 2 + 2 * switch_count;
3056 }
3057
3058 /* verify each switch target */
3059 for (targ = 0; targ < switch_count; targ++) {
3060 int offset;
3061 uint32_t abs_offset;
3062
3063 /* offsets are 32-bit, and only partly endian-swapped */
3064 offset = switch_insns[offset_to_targets + targ * 2] |
3065 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 abs_offset = work_insn_idx_ + offset;
3067 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07003068 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003069 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 }
3071 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07003072 return false;
3073 }
3074 }
3075
3076 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3078 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003079 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003080 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003082 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003083
Ian Rogers0571d352011-11-03 19:51:38 -07003084 for (; iterator.HasNext(); iterator.Next()) {
3085 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 within_catch_all = true;
3087 }
jeffhaobdb76512011-09-07 11:43:16 -07003088 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3090 * "work_regs", because at runtime the exception will be thrown before the instruction
3091 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003092 */
Ian Rogers0571d352011-11-03 19:51:38 -07003093 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07003094 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 }
jeffhaobdb76512011-09-07 11:43:16 -07003096 }
3097
3098 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3100 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003101 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07003103 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 * The state in work_line reflects the post-execution state. If the current instruction is a
3105 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003106 * it will do so before grabbing the lock).
3107 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003108 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003109 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003111 return false;
3112 }
3113 }
3114 }
3115
jeffhaod1f0fde2011-09-08 17:25:33 -07003116 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003117 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003118 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 return false;
3120 }
jeffhaobdb76512011-09-07 11:43:16 -07003121 }
3122
3123 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003124 * Update start_guess. Advance to the next instruction of that's
3125 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003126 * neither of those exists we're in a return or throw; leave start_guess
3127 * alone and let the caller sort it out.
3128 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003131 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003132 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003134 }
3135
Ian Rogersd81871c2011-10-03 13:57:23 -07003136 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3137 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003138
3139 return true;
3140}
3141
Ian Rogers28ad40d2011-10-27 15:19:26 -07003142const RegType& DexVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003143 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003144 Class* referrer = method_->GetDeclaringClass();
3145 Class* klass = method_->GetDexCacheResolvedTypes()->Get(class_idx);
3146 const RegType& result =
3147 klass != NULL ? reg_types_.FromClass(klass)
3148 : reg_types_.FromDescriptor(referrer->GetClassLoader(), descriptor);
3149 if (klass == NULL && !result.IsUnresolvedTypes()) {
3150 method_->GetDexCacheResolvedTypes()->Set(class_idx, result.GetClass());
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 }
jeffhaod1224c72012-02-29 13:43:08 -08003152 if (result.IsUnknown()) {
jeffhaod5347e02012-03-22 17:25:05 -07003153 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing unknown class in " << PrettyDescriptor(referrer);
jeffhaod1224c72012-02-29 13:43:08 -08003154 return result;
3155 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003156 // Check if access is allowed. Unresolved types use AllocObjectFromCodeWithAccessCheck to
3157 // check at runtime if access is allowed and so pass here.
3158 if (!result.IsUnresolvedTypes() && !referrer->CanAccess(result.GetClass())) {
3159 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003160 << PrettyDescriptor(referrer) << "' -> '"
Ian Rogers28ad40d2011-10-27 15:19:26 -07003161 << result << "'";
3162 return reg_types_.Unknown();
3163 } else {
3164 return result;
3165 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003166}
3167
Ian Rogers28ad40d2011-10-27 15:19:26 -07003168const RegType& DexVerifier::GetCaughtExceptionType() {
3169 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07003171 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3173 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003174 CatchHandlerIterator iterator(handlers_ptr);
3175 for (; iterator.HasNext(); iterator.Next()) {
3176 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3177 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003178 common_super = &reg_types_.JavaLangThrowable();
Ian Rogersd81871c2011-10-03 13:57:23 -07003179 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07003180 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08003181 if (common_super == NULL) {
3182 // Unconditionally assign for the first handler. We don't assert this is a Throwable
3183 // as that is caught at runtime
3184 common_super = &exception;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003185 } else if (!reg_types_.JavaLangThrowable().IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003186 // We don't know enough about the type and the common path merge will result in
3187 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07003188 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003189 return reg_types_.Unknown();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003190 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003191 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003193 common_super = &common_super->Merge(exception, &reg_types_);
3194 CHECK(reg_types_.JavaLangThrowable().IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 }
3196 }
3197 }
3198 }
Ian Rogers0571d352011-11-03 19:51:38 -07003199 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 }
3201 }
3202 if (common_super == NULL) {
3203 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003204 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersc4762272012-02-01 15:55:55 -08003205 return reg_types_.Unknown();
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003207 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003208}
3209
jeffhao8cd6dda2012-02-22 10:15:34 -08003210Method* DexVerifier::ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) {
Ian Rogers90040192011-12-16 08:54:29 -08003211 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3212 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
3213 if (failure_ != VERIFY_ERROR_NONE) {
3214 fail_messages_ << " in attempt to access method " << dex_file_->GetMethodName(method_id);
3215 return NULL;
3216 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003217 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003218 return NULL; // Can't resolve Class so no more to do here
3219 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003220 Class* klass = klass_type.GetClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 Class* referrer = method_->GetDeclaringClass();
3222 DexCache* dex_cache = referrer->GetDexCache();
3223 Method* res_method = dex_cache->GetResolvedMethod(method_idx);
3224 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003225 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07003226 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08003227
3228 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003230 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 res_method = klass->FindInterfaceMethod(name, signature);
3232 } else {
3233 res_method = klass->FindVirtualMethod(name, signature);
3234 }
3235 if (res_method != NULL) {
3236 dex_cache->SetResolvedMethod(method_idx, res_method);
3237 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003238 // If a virtual or interface method wasn't found with the expected type, look in
3239 // the direct methods. This can happen when the wrong invoke type is used or when
3240 // a class has changed, and will be flagged as an error in later checks.
3241 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3242 res_method = klass->FindDirectMethod(name, signature);
3243 }
3244 if (res_method == NULL) {
3245 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3246 << PrettyDescriptor(klass) << "." << name
3247 << " " << signature;
3248 return NULL;
3249 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 }
3251 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3253 // enforce them here.
3254 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3256 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 return NULL;
3258 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003259 // Disallow any calls to class initializers.
3260 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3262 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003263 return NULL;
3264 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003265 // Check if access is allowed.
3266 if (!referrer->CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
3267 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
3268 << " from " << PrettyDescriptor(referrer) << ")";
3269 return NULL;
3270 }
jeffhaode0d9c92012-02-27 13:58:13 -08003271 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3272 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3274 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003275 return NULL;
3276 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003277 // Check that interface methods match interface classes.
3278 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3279 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3280 << " is in an interface class " << PrettyClass(klass);
3281 return NULL;
3282 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3283 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3284 << " is in a non-interface class " << PrettyClass(klass);
3285 return NULL;
3286 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 // See if the method type implied by the invoke instruction matches the access flags for the
3288 // target method.
3289 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3290 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3291 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3292 ) {
Ian Rogers573db4a2011-12-13 15:30:50 -08003293 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type does not match method type of "
3294 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003295 return NULL;
3296 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003297 return res_method;
3298}
3299
Elliott Hughesadb8c672012-03-06 16:49:32 -08003300Method* DexVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
jeffhao8cd6dda2012-02-22 10:15:34 -08003301 MethodType method_type, bool is_range, bool is_super) {
3302 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3303 // we're making.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003304 Method* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003305 if (res_method == NULL) { // error or class is unresolved
3306 return NULL;
3307 }
3308
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3310 // has a vtable entry for the target method.
3311 if (is_super) {
3312 DCHECK(method_type == METHOD_VIRTUAL);
3313 Class* super = method_->GetDeclaringClass()->GetSuperClass();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08003314 if (super == NULL || res_method->GetMethodIndex() >= super->GetVTable()->GetLength()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 if (super == NULL) { // Only Object has no super class
3316 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
3317 << " to super " << PrettyMethod(res_method);
3318 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003319 MethodHelper mh(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " << PrettyMethod(method_)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003321 << " to super " << PrettyDescriptor(super)
3322 << "." << mh.GetName()
3323 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 }
3325 return NULL;
3326 }
3327 }
3328 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3329 // match the call to the signature. Also, we might might be calling through an abstract method
3330 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08003331 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 /* caught by static verifier */
3333 DCHECK(is_range || expected_args <= 5);
3334 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3337 return NULL;
3338 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003339
jeffhaobdb76512011-09-07 11:43:16 -07003340 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 * Check the "this" argument, which must be an instance of the class
3342 * that declared the method. For an interface class, we don't do the
3343 * full interface merge, so we can't do a rigorous check here (which
3344 * is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003345 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003346 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003347 if (!res_method->IsStatic()) {
3348 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
3349 if (failure_ != VERIFY_ERROR_NONE) {
3350 return NULL;
3351 }
3352 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 return NULL;
3355 }
3356 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers9074b992011-10-26 17:41:55 -07003357 const RegType& res_method_class = reg_types_.FromClass(res_method->GetDeclaringClass());
3358 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003359 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003360 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 return NULL;
3362 }
3363 }
3364 actual_args++;
3365 }
3366 /*
3367 * Process the target method's signature. This signature may or may not
3368 * have been verified, so we can't assume it's properly formed.
3369 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003370 MethodHelper mh(res_method);
3371 const DexFile::TypeList* params = mh.GetParameterTypeList();
3372 size_t params_size = params == NULL ? 0 : params->Size();
3373 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003376 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3377 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 return NULL;
3379 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003380 const char* descriptor =
3381 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3382 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003384 << " missing signature component";
3385 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 }
3387 const RegType& reg_type =
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003388 reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(), descriptor);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003389 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003390 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3391 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 }
3393 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003397 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 return NULL;
3399 } else {
3400 return res_method;
3401 }
3402}
3403
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003404const RegType& DexVerifier::GetMethodReturnType() {
3405 return reg_types_.FromDescriptor(method_->GetDeclaringClass()->GetClassLoader(),
3406 MethodHelper(method_).GetReturnTypeDescriptor());
3407}
3408
Elliott Hughesadb8c672012-03-06 16:49:32 -08003409void DexVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08003410 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003411 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003412 if (res_type.IsUnknown()) {
3413 CHECK_NE(failure_, VERIFY_ERROR_NONE);
3414 } else {
3415 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3416 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003418 } else if (!is_filled) {
3419 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003420 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003421 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003422 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003423 } else {
3424 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3425 // the list and fail. It's legal, if silly, for arg_count to be zero.
3426 const RegType& expected_type = reg_types_.GetComponentType(res_type,
3427 method_->GetDeclaringClass()->GetClassLoader());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003429 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003431 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
3432 work_line_->SetResultRegisterType(reg_types_.Unknown());
3433 return;
3434 }
3435 }
3436 // filled-array result goes into "result" register
3437 work_line_->SetResultRegisterType(res_type);
3438 }
3439 }
3440}
3441
Elliott Hughesadb8c672012-03-06 16:49:32 -08003442void DexVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003444 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003447 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003449 if (array_type.IsZero()) {
3450 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3451 // instruction type. TODO: have a proper notion of bottom here.
3452 if (!is_primitive || insn_type.IsCategory1Types()) {
3453 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08003454 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003456 // Category 2
Elliott Hughesadb8c672012-03-06 16:49:32 -08003457 work_line_->SetRegisterType(dec_insn.vA, reg_types_.ConstLo());
Ian Rogers89310de2012-02-01 13:47:30 -08003458 }
jeffhaofc3144e2012-02-01 17:21:15 -08003459 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003461 } else {
3462 /* verify the class */
3463 const RegType& component_type = reg_types_.GetComponentType(array_type,
3464 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003465 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003467 << " source for aget-object";
3468 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003470 << " source for category 1 aget";
3471 } else if (is_primitive && !insn_type.Equals(component_type) &&
3472 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3473 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003476 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003477 // Use knowledge of the field type which is stronger than the type inferred from the
3478 // instruction, which can't differentiate object types and ints from floats, longs from
3479 // doubles.
Elliott Hughesadb8c672012-03-06 16:49:32 -08003480 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 }
3482 }
3483 }
3484}
3485
Elliott Hughesadb8c672012-03-06 16:49:32 -08003486void DexVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003488 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003492 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08003493 if (array_type.IsZero()) {
3494 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3495 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003496 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003498 } else {
3499 /* verify the class */
3500 const RegType& component_type = reg_types_.GetComponentType(array_type,
3501 method_->GetDeclaringClass()->GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003502 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003504 << " source for aput-object";
3505 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003507 << " source for category 1 aput";
3508 } else if (is_primitive && !insn_type.Equals(component_type) &&
3509 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3510 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003512 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003514 // The instruction agrees with the type of array, confirm the value to be stored does too
3515 // Note: we use the instruction type (rather than the component type) for aput-object as
3516 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08003517 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003518 }
3519 }
3520 }
3521}
3522
3523Field* DexVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003524 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3525 // Check access to class
3526 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3527 if (failure_ != VERIFY_ERROR_NONE) {
3528 fail_messages_ << " in attempt to access static field " << field_idx << " ("
3529 << dex_file_->GetFieldName(field_id) << ") in "
3530 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3531 return NULL;
3532 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003533 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003534 return NULL; // Can't resolve Class so no more to do here
3535 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003536 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003538 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
3539 << dex_file_->GetFieldName(field_id) << ") in "
3540 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 DCHECK(Thread::Current()->IsExceptionPending());
3542 Thread::Current()->ClearException();
3543 return NULL;
3544 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3545 field->GetAccessFlags())) {
3546 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
3547 << " from " << PrettyClass(method_->GetDeclaringClass());
3548 return NULL;
3549 } else if (!field->IsStatic()) {
3550 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3551 return NULL;
3552 } else {
3553 return field;
3554 }
3555}
3556
Ian Rogersd81871c2011-10-03 13:57:23 -07003557Field* DexVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003558 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3559 // Check access to class
3560 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
3561 if (failure_ != VERIFY_ERROR_NONE) {
3562 fail_messages_ << " in attempt to access instance field " << field_idx << " ("
3563 << dex_file_->GetFieldName(field_id) << ") in "
3564 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
3565 return NULL;
3566 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003567 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003568 return NULL; // Can't resolve Class so no more to do here
3569 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003570 Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(field_idx, method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003572 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
3573 << dex_file_->GetFieldName(field_id) << ") in "
3574 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 DCHECK(Thread::Current()->IsExceptionPending());
3576 Thread::Current()->ClearException();
3577 return NULL;
3578 } else if (!method_->GetDeclaringClass()->CanAccessMember(field->GetDeclaringClass(),
3579 field->GetAccessFlags())) {
3580 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
3581 << " from " << PrettyClass(method_->GetDeclaringClass());
3582 return NULL;
3583 } else if (field->IsStatic()) {
3584 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3585 << " to not be static";
3586 return NULL;
3587 } else if (obj_type.IsZero()) {
3588 // Cannot infer and check type, however, access will cause null pointer exception
3589 return field;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003590 } else if (obj_type.IsUninitializedTypes() &&
3591 (!method_->IsConstructor() || method_->GetDeclaringClass() != obj_type.GetClass() ||
3592 field->GetDeclaringClass() != method_->GetDeclaringClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003593 // Field accesses through uninitialized references are only allowable for constructors where
3594 // the field is declared in this class
jeffhaod5347e02012-03-22 17:25:05 -07003595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3596 << " of a not fully initialized object within the context of "
3597 << PrettyMethod(method_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 return NULL;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003599 } else if (!field->GetDeclaringClass()->IsAssignableFrom(obj_type.GetClass())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3601 // of C1. For resolution to occur the declared class of the field must be compatible with
3602 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3603 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3604 << " from object of type " << PrettyClass(obj_type.GetClass());
3605 return NULL;
3606 } else {
3607 return field;
3608 }
3609}
3610
Elliott Hughesadb8c672012-03-06 16:49:32 -08003611void DexVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003612 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003613 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003614 Field* field;
3615 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003616 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003617 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003618 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003619 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003620 }
Ian Rogersf4028cc2011-11-02 14:56:39 -07003621 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003622 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003623 } else {
3624 const char* descriptor;
3625 const ClassLoader* loader;
3626 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003627 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003628 loader = field->GetDeclaringClass()->GetClassLoader();
3629 } else {
3630 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3631 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3632 loader = method_->GetDeclaringClass()->GetClassLoader();
3633 }
3634 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
Ian Rogersd81871c2011-10-03 13:57:23 -07003635 if (is_primitive) {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003636 if (field_type.Equals(insn_type) ||
3637 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3638 (field_type.IsDouble() && insn_type.IsLongTypes())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003639 // expected that read is of the correct primitive type or that int reads are reading
3640 // floats or long reads are reading doubles
3641 } else {
3642 // This is a global failure rather than a class change failure as the instructions and
3643 // the descriptors for the type should have been consistent within the same file at
3644 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3646 << " to be of type '" << insn_type
3647 << "' but found type '" << field_type << "' in get";
Ian Rogersd81871c2011-10-03 13:57:23 -07003648 return;
3649 }
3650 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003651 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003652 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3653 << " to be compatible with type '" << insn_type
3654 << "' but found type '" << field_type
3655 << "' in get-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 return;
3657 }
3658 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003659 work_line_->SetRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003660 }
3661}
3662
Elliott Hughesadb8c672012-03-06 16:49:32 -08003663void DexVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003664 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003665 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003666 Field* field;
3667 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003668 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003669 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003670 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003671 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003672 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003673 if (failure_ != VERIFY_ERROR_NONE) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003674 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Unknown());
Ian Rogers55d249f2011-11-02 16:48:09 -07003675 } else {
3676 const char* descriptor;
3677 const ClassLoader* loader;
3678 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003679 descriptor = FieldHelper(field).GetTypeDescriptor();
Ian Rogers55d249f2011-11-02 16:48:09 -07003680 loader = field->GetDeclaringClass()->GetClassLoader();
3681 } else {
3682 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3683 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3684 loader = method_->GetDeclaringClass()->GetClassLoader();
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 }
Ian Rogers55d249f2011-11-02 16:48:09 -07003686 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor);
3687 if (field != NULL) {
3688 if (field->IsFinal() && field->GetDeclaringClass() != method_->GetDeclaringClass()) {
3689 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3690 << " from other class " << PrettyClass(method_->GetDeclaringClass());
3691 return;
3692 }
3693 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 if (is_primitive) {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003695 // Primitive field assignability rules are weaker than regular assignability rules
3696 bool instruction_compatible;
3697 bool value_compatible;
Elliott Hughesadb8c672012-03-06 16:49:32 -08003698 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers2c8a8572011-10-24 17:11:36 -07003699 if (field_type.IsIntegralTypes()) {
3700 instruction_compatible = insn_type.IsIntegralTypes();
3701 value_compatible = value_type.IsIntegralTypes();
3702 } else if (field_type.IsFloat()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003703 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
Ian Rogers2c8a8572011-10-24 17:11:36 -07003704 value_compatible = value_type.IsFloatTypes();
3705 } else if (field_type.IsLong()) {
3706 instruction_compatible = insn_type.IsLong();
3707 value_compatible = value_type.IsLongTypes();
3708 } else if (field_type.IsDouble()) {
Ian Rogersb94a27b2011-10-26 00:33:41 -07003709 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
Ian Rogers2c8a8572011-10-24 17:11:36 -07003710 value_compatible = value_type.IsDoubleTypes();
Ian Rogersd81871c2011-10-03 13:57:23 -07003711 } else {
Ian Rogers2c8a8572011-10-24 17:11:36 -07003712 instruction_compatible = false; // reference field with primitive store
3713 value_compatible = false; // unused
3714 }
3715 if (!instruction_compatible) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 // This is a global failure rather than a class change failure as the instructions and
3717 // the descriptors for the type should have been consistent within the same file at
3718 // compile time
jeffhaod5347e02012-03-22 17:25:05 -07003719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3720 << " to be of type '" << insn_type
3721 << "' but found type '" << field_type
3722 << "' in put";
Ian Rogersd81871c2011-10-03 13:57:23 -07003723 return;
3724 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003725 if (!value_compatible) {
jeffhaod5347e02012-03-22 17:25:05 -07003726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3727 << " of type " << value_type
3728 << " but expected " << field_type
3729 << " for store to " << PrettyField(field) << " in put";
Ian Rogers2c8a8572011-10-24 17:11:36 -07003730 return;
3731 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 } else {
Ian Rogersb5e95b92011-10-25 23:28:55 -07003733 if (!insn_type.IsAssignableFrom(field_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003734 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3735 << " to be compatible with type '" << insn_type
3736 << "' but found type '" << field_type
3737 << "' in put-object";
Ian Rogersd81871c2011-10-03 13:57:23 -07003738 return;
3739 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003740 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003741 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 }
3743}
3744
jeffhaod5347e02012-03-22 17:25:05 -07003745bool DexVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 return false;
3749 }
3750 return true;
3751}
3752
Ian Rogersd81871c2011-10-03 13:57:23 -07003753void DexVerifier::ReplaceFailingInstruction() {
Ian Rogersf1864ef2011-12-09 12:39:48 -08003754 if (Runtime::Current()->IsStarted()) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003755 LOG(ERROR) << "Verification attempting to replace instructions in " << PrettyMethod(method_)
Ian Rogersf1864ef2011-12-09 12:39:48 -08003756 << " " << fail_messages_.str();
3757 return;
3758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 const Instruction* inst = Instruction::At(code_item_->insns_ + work_insn_idx_);
3760 DCHECK(inst->IsThrow()) << "Expected instruction that will throw " << inst->Name();
3761 VerifyErrorRefType ref_type;
3762 switch (inst->Opcode()) {
3763 case Instruction::CONST_CLASS: // insn[1] == class ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003764 case Instruction::CHECK_CAST:
3765 case Instruction::INSTANCE_OF:
3766 case Instruction::NEW_INSTANCE:
3767 case Instruction::NEW_ARRAY:
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 case Instruction::FILLED_NEW_ARRAY: // insn[1] == class ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003769 case Instruction::FILLED_NEW_ARRAY_RANGE:
3770 ref_type = VERIFY_ERROR_REF_CLASS;
3771 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 case Instruction::IGET: // insn[1] == field ref, 2 code units (4 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003773 case Instruction::IGET_BOOLEAN:
3774 case Instruction::IGET_BYTE:
3775 case Instruction::IGET_CHAR:
3776 case Instruction::IGET_SHORT:
3777 case Instruction::IGET_WIDE:
3778 case Instruction::IGET_OBJECT:
3779 case Instruction::IPUT:
3780 case Instruction::IPUT_BOOLEAN:
3781 case Instruction::IPUT_BYTE:
3782 case Instruction::IPUT_CHAR:
3783 case Instruction::IPUT_SHORT:
3784 case Instruction::IPUT_WIDE:
3785 case Instruction::IPUT_OBJECT:
3786 case Instruction::SGET:
3787 case Instruction::SGET_BOOLEAN:
3788 case Instruction::SGET_BYTE:
3789 case Instruction::SGET_CHAR:
3790 case Instruction::SGET_SHORT:
3791 case Instruction::SGET_WIDE:
3792 case Instruction::SGET_OBJECT:
3793 case Instruction::SPUT:
3794 case Instruction::SPUT_BOOLEAN:
3795 case Instruction::SPUT_BYTE:
3796 case Instruction::SPUT_CHAR:
3797 case Instruction::SPUT_SHORT:
3798 case Instruction::SPUT_WIDE:
3799 case Instruction::SPUT_OBJECT:
3800 ref_type = VERIFY_ERROR_REF_FIELD;
3801 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 case Instruction::INVOKE_VIRTUAL: // insn[1] == method ref, 3 code units (6 bytes)
jeffhaobdb76512011-09-07 11:43:16 -07003803 case Instruction::INVOKE_VIRTUAL_RANGE:
3804 case Instruction::INVOKE_SUPER:
3805 case Instruction::INVOKE_SUPER_RANGE:
3806 case Instruction::INVOKE_DIRECT:
3807 case Instruction::INVOKE_DIRECT_RANGE:
3808 case Instruction::INVOKE_STATIC:
3809 case Instruction::INVOKE_STATIC_RANGE:
3810 case Instruction::INVOKE_INTERFACE:
3811 case Instruction::INVOKE_INTERFACE_RANGE:
3812 ref_type = VERIFY_ERROR_REF_METHOD;
3813 break;
jeffhaobdb76512011-09-07 11:43:16 -07003814 default:
Ian Rogers2c8a8572011-10-24 17:11:36 -07003815 LOG(FATAL) << "Error: verifier asked to replace instruction " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003816 return;
jeffhaoba5ebb92011-08-25 17:24:37 -07003817 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003818 uint16_t* insns = const_cast<uint16_t*>(code_item_->insns_);
3819 // THROW_VERIFICATION_ERROR is a 2 code unit instruction. We shouldn't be rewriting a 1 code unit
3820 // instruction, so assert it.
3821 size_t width = inst->SizeInCodeUnits();
3822 CHECK_GT(width, 1u);
Ian Rogersf1864ef2011-12-09 12:39:48 -08003823 // If the instruction is larger than 2 code units, rewrite subsequent code unit sized chunks with
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 // NOPs
3825 for (size_t i = 2; i < width; i++) {
3826 insns[work_insn_idx_ + i] = Instruction::NOP;
3827 }
3828 // Encode the opcode, with the failure code in the high byte
3829 uint16_t new_instruction = Instruction::THROW_VERIFICATION_ERROR |
3830 (failure_ << 8) | // AA - component
3831 (ref_type << (8 + kVerifyErrorRefTypeShift));
3832 insns[work_insn_idx_] = new_instruction;
3833 // The 2nd code unit (higher in memory) with the reference in, comes from the instruction we
3834 // rewrote, so nothing to do here.
Ian Rogers9fdfc182011-10-26 23:12:52 -07003835 LOG(INFO) << "Verification error, replacing instructions in " << PrettyMethod(method_) << " "
3836 << fail_messages_.str();
3837 if (gDebugVerify) {
3838 std::cout << std::endl << info_messages_.str();
3839 Dump(std::cout);
3840 }
jeffhaobdb76512011-09-07 11:43:16 -07003841}
jeffhaoba5ebb92011-08-25 17:24:37 -07003842
Ian Rogersd81871c2011-10-03 13:57:23 -07003843bool DexVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 bool changed = true;
3845 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3846 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003847 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 * We haven't processed this instruction before, and we haven't touched the registers here, so
3849 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3850 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003851 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003853 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003854 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3855 if (gDebugVerify) {
3856 copy->CopyFromLine(target_line);
3857 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 changed = target_line->MergeRegisters(merge_line);
3859 if (failure_ != VERIFY_ERROR_NONE) {
3860 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003861 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003862 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003863 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
3864 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << std::endl
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 << *copy.get() << " MERGE" << std::endl
3866 << *merge_line << " ==" << std::endl
3867 << *target_line << std::endl;
jeffhaobdb76512011-09-07 11:43:16 -07003868 }
3869 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003870 if (changed) {
3871 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003872 }
3873 return true;
3874}
3875
Ian Rogersd81871c2011-10-03 13:57:23 -07003876void DexVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
3877 size_t* log2_max_gc_pc) {
3878 size_t local_gc_points = 0;
3879 size_t max_insn = 0;
3880 size_t max_ref_reg = -1;
3881 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3882 if (insn_flags_[i].IsGcPoint()) {
3883 local_gc_points++;
3884 max_insn = i;
3885 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003886 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003887 }
3888 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 *gc_points = local_gc_points;
3890 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3891 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003892 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003893 i++;
3894 }
3895 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003896}
3897
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003898const std::vector<uint8_t>* DexVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003899 size_t num_entries, ref_bitmap_bits, pc_bits;
3900 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3901 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003902 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003903 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003905 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003906 return NULL;
3907 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003908 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3909 // There are 2 bytes to encode the number of entries
3910 if (num_entries >= 65536) {
3911 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003913 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003914 return NULL;
3915 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003917 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003918 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003919 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003920 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003921 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003922 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003923 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003924 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003927 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3928 return NULL;
3929 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003930 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003931 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003932 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 return NULL;
3935 }
3936 // Write table header
jeffhao60f83e32012-02-13 17:16:30 -08003937 table->push_back(format | ((ref_bitmap_bytes >> kRegMapFormatShift) & ~kRegMapFormatMask));
3938 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003939 table->push_back(num_entries & 0xFF);
3940 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003941 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003942 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3943 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003944 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003946 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003947 }
3948 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003949 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003950 }
3951 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003952 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003953 return table;
3954}
jeffhaoa0a764a2011-09-16 10:43:38 -07003955
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003956void DexVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003957 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3958 // that the table data is well formed and all references are marked (or not) in the bitmap
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003959 PcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003960 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003961 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003962 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3963 if (insn_flags_[i].IsGcPoint()) {
3964 CHECK_LT(map_index, map.NumEntries());
3965 CHECK_EQ(map.GetPC(map_index), i);
3966 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3967 map_index++;
3968 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003969 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003970 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 CHECK_LT(j / 8, map.RegWidth());
3972 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3973 } else if ((j / 8) < map.RegWidth()) {
3974 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3975 } else {
3976 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3977 }
3978 }
3979 } else {
3980 CHECK(reg_bitmap == NULL);
3981 }
3982 }
3983}
jeffhaoa0a764a2011-09-16 10:43:38 -07003984
Ian Rogersd81871c2011-10-03 13:57:23 -07003985const uint8_t* PcToReferenceMap::FindBitMap(uint16_t dex_pc, bool error_if_not_present) const {
3986 size_t num_entries = NumEntries();
3987 // Do linear or binary search?
3988 static const size_t kSearchThreshold = 8;
3989 if (num_entries < kSearchThreshold) {
3990 for (size_t i = 0; i < num_entries; i++) {
3991 if (GetPC(i) == dex_pc) {
3992 return GetBitMap(i);
3993 }
3994 }
3995 } else {
3996 int lo = 0;
3997 int hi = num_entries -1;
jeffhaoa0a764a2011-09-16 10:43:38 -07003998 while (hi >= lo) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003999 int mid = (hi + lo) / 2;
4000 int mid_pc = GetPC(mid);
4001 if (dex_pc > mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004002 lo = mid + 1;
Ian Rogersd81871c2011-10-03 13:57:23 -07004003 } else if (dex_pc < mid_pc) {
jeffhaoa0a764a2011-09-16 10:43:38 -07004004 hi = mid - 1;
4005 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 return GetBitMap(mid);
jeffhaoa0a764a2011-09-16 10:43:38 -07004007 }
4008 }
4009 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 if (error_if_not_present) {
4011 LOG(ERROR) << "Didn't find reference bit map for dex_pc " << dex_pc;
4012 }
jeffhaoa0a764a2011-09-16 10:43:38 -07004013 return NULL;
4014}
4015
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004016Mutex* DexVerifier::gc_maps_lock_ = NULL;
4017DexVerifier::GcMapTable* DexVerifier::gc_maps_ = NULL;
4018
4019void DexVerifier::InitGcMaps() {
4020 gc_maps_lock_ = new Mutex("verifier GC maps lock");
4021 MutexLock mu(*gc_maps_lock_);
4022 gc_maps_ = new DexVerifier::GcMapTable;
4023}
4024
4025void DexVerifier::DeleteGcMaps() {
Elliott Hughesf34f1742012-03-16 18:56:00 -07004026 {
4027 MutexLock mu(*gc_maps_lock_);
4028 STLDeleteValues(gc_maps_);
4029 delete gc_maps_;
4030 gc_maps_ = NULL;
4031 }
4032 delete gc_maps_lock_;
4033 gc_maps_lock_ = NULL;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004034}
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004035
4036void DexVerifier::SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004037 MutexLock mu(*gc_maps_lock_);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004038 const std::vector<uint8_t>* existing_gc_map = GetGcMap(ref);
4039 if (existing_gc_map != NULL) {
4040 CHECK(*existing_gc_map == gc_map);
4041 delete existing_gc_map;
4042 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004043 (*gc_maps_)[ref] = &gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004044 CHECK(GetGcMap(ref) != NULL);
4045}
4046
4047const std::vector<uint8_t>* DexVerifier::GetGcMap(Compiler::MethodReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004048 MutexLock mu(*gc_maps_lock_);
4049 GcMapTable::const_iterator it = gc_maps_->find(ref);
4050 if (it == gc_maps_->end()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004051 return NULL;
4052 }
4053 CHECK(it->second != NULL);
4054 return it->second;
4055}
4056
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004057static Mutex& GetRejectedClassesLock() {
4058 static Mutex rejected_classes_lock("verifier rejected classes lock");
4059 return rejected_classes_lock;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004060}
4061
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004062static std::set<Compiler::ClassReference>& GetRejectedClasses() {
4063 static std::set<Compiler::ClassReference> rejected_classes;
4064 return rejected_classes;
4065}
jeffhaod1224c72012-02-29 13:43:08 -08004066
4067void DexVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004068 MutexLock mu(GetRejectedClassesLock());
4069 GetRejectedClasses().insert(ref);
jeffhaod1224c72012-02-29 13:43:08 -08004070 CHECK(IsClassRejected(ref));
4071}
4072
4073bool DexVerifier::IsClassRejected(Compiler::ClassReference ref) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004074 MutexLock mu(GetRejectedClassesLock());
4075 std::set<Compiler::ClassReference>& rejected_classes(GetRejectedClasses());
4076 return (rejected_classes.find(ref) != rejected_classes.end());
jeffhaod1224c72012-02-29 13:43:08 -08004077}
4078
Logan Chienfca7e872011-12-20 20:08:22 +08004079#if defined(ART_USE_LLVM_COMPILER)
Logan Chiendd361c92012-04-10 23:40:37 +08004080const InferredRegCategoryMap* DexVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08004081 uint32_t insns_size = code_item_->insns_size_in_code_units_;
4082 uint16_t regs_size = code_item_->registers_size_;
4083
4084 UniquePtr<InferredRegCategoryMap> table(
4085 new InferredRegCategoryMap(insns_size, regs_size));
4086
4087 for (size_t i = 0; i < insns_size; ++i) {
4088 if (RegisterLine* line = reg_table_.GetLine(i)) {
4089 for (size_t r = 0; r < regs_size; ++r) {
Logan Chiendd361c92012-04-10 23:40:37 +08004090 const RegType &rt = line->GetRegisterType(r);
Logan Chienfca7e872011-12-20 20:08:22 +08004091
4092 if (rt.IsZero()) {
4093 table->SetRegCategory(i, r, kRegZero);
4094 } else if (rt.IsCategory1Types()) {
4095 table->SetRegCategory(i, r, kRegCat1nr);
4096 } else if (rt.IsCategory2Types()) {
4097 table->SetRegCategory(i, r, kRegCat2);
4098 } else if (rt.IsReferenceTypes()) {
4099 table->SetRegCategory(i, r, kRegObject);
4100 } else {
4101 table->SetRegCategory(i, r, kRegUnknown);
4102 }
4103 }
4104 }
4105 }
4106
4107 return table.release();
4108}
Logan Chiendd361c92012-04-10 23:40:37 +08004109
4110Mutex* DexVerifier::inferred_reg_category_maps_lock_ = NULL;
4111DexVerifier::InferredRegCategoryMapTable* DexVerifier::inferred_reg_category_maps_ = NULL;
4112
4113void DexVerifier::InitInferredRegCategoryMaps() {
4114 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
4115 MutexLock mu(*inferred_reg_category_maps_lock_);
4116 inferred_reg_category_maps_ = new DexVerifier::InferredRegCategoryMapTable;
4117}
4118
4119void DexVerifier::DeleteInferredRegCategoryMaps() {
4120 {
4121 MutexLock mu(*inferred_reg_category_maps_lock_);
4122 STLDeleteValues(inferred_reg_category_maps_);
4123 delete inferred_reg_category_maps_;
4124 inferred_reg_category_maps_ = NULL;
4125 }
4126 delete inferred_reg_category_maps_lock_;
4127 inferred_reg_category_maps_lock_ = NULL;
4128}
4129
4130
4131void DexVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
4132 const InferredRegCategoryMap& inferred_reg_category_map) {
4133 MutexLock mu(*inferred_reg_category_maps_lock_);
4134 const InferredRegCategoryMap* existing_inferred_reg_category_map =
4135 GetInferredRegCategoryMap(ref);
4136
4137 if (existing_inferred_reg_category_map != NULL) {
4138 CHECK(*existing_inferred_reg_category_map == inferred_reg_category_map);
4139 delete existing_inferred_reg_category_map;
4140 }
4141
4142 (*inferred_reg_category_maps_)[ref] = &inferred_reg_category_map;
4143 CHECK(GetInferredRegCategoryMap(ref) != NULL);
4144}
4145
4146const InferredRegCategoryMap*
4147DexVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
4148 MutexLock mu(*inferred_reg_category_maps_lock_);
4149
4150 InferredRegCategoryMapTable::const_iterator it =
4151 inferred_reg_category_maps_->find(ref);
4152
4153 if (it == inferred_reg_category_maps_->end()) {
4154 return NULL;
4155 }
4156 CHECK(it->second != NULL);
4157 return it->second;
4158}
Logan Chienfca7e872011-12-20 20:08:22 +08004159#endif
4160
Ian Rogersd81871c2011-10-03 13:57:23 -07004161} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004162} // namespace art