blob: 87cc3286b3819335a039cca1cb51a6488efdd785 [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
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers1212a022013-03-04 10:48:41 -080025#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/abstract_method-inl.h"
34#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/field-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
49
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
62 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070077 std::string& error,
78 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080084 error = "Verifier rejected class ";
85 error += PrettyDescriptor(klass);
86 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
90 error = "Verifier rejected class ";
91 error += PrettyDescriptor(klass);
92 error += " that attempts to sub-class final class ";
93 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
98 uint32_t class_def_idx;
99 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
100 error = "Verifier rejected class ";
101 error += PrettyDescriptor(klass);
102 error += " that isn't present in dex file ";
103 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Jeff Haoee988952013-04-16 14:23:47 -0700106 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700107}
108
Ian Rogers365c1022012-06-22 15:05:28 -0700109MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::DexCache* dex_cache,
111 mirror::ClassLoader* class_loader,
112 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700113 std::string& error,
114 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800115 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
116 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700117 if (class_data == NULL) {
118 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700119 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700120 }
jeffhaof56197c2012-03-05 18:01:54 -0800121 ClassDataItemIterator it(*dex_file, class_data);
122 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
123 it.Next();
124 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700126 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700127 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700128 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800129 while (it.HasNextDirectMethod()) {
130 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700131 if (method_idx == previous_direct_method_idx) {
132 // smali can create dex files with two encoded_methods sharing the same method_idx
133 // http://code.google.com/p/smali/issues/detail?id=119
134 it.Next();
135 continue;
136 }
137 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::AbstractMethod* method =
140 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 if (method == NULL) {
142 DCHECK(Thread::Current()->IsExceptionPending());
143 // We couldn't resolve the method, but continue regardless.
144 Thread::Current()->ClearException();
145 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700147 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700148 if (result != kNoFailure) {
149 if (result == kHardFailure) {
150 hard_fail = true;
151 if (error_count > 0) {
152 error += "\n";
153 }
154 error = "Verifier rejected class ";
155 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
156 error += " due to bad method ";
157 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800160 }
161 it.Next();
162 }
jeffhao9b0b1882012-10-01 16:51:22 -0700163 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800164 while (it.HasNextVirtualMethod()) {
165 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700166 if (method_idx == previous_virtual_method_idx) {
167 // smali can create dex files with two encoded_methods sharing the same method_idx
168 // http://code.google.com/p/smali/issues/detail?id=119
169 it.Next();
170 continue;
171 }
172 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::AbstractMethod* method =
175 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 if (method == NULL) {
177 DCHECK(Thread::Current()->IsExceptionPending());
178 // We couldn't resolve the method, but continue regardless.
179 Thread::Current()->ClearException();
180 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700182 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700183 if (result != kNoFailure) {
184 if (result == kHardFailure) {
185 hard_fail = true;
186 if (error_count > 0) {
187 error += "\n";
188 }
189 error = "Verifier rejected class ";
190 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
191 error += " due to bad method ";
192 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800195 }
196 it.Next();
197 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 if (error_count == 0) {
199 return kNoFailure;
200 } else {
201 return hard_fail ? kHardFailure : kSoftFailure;
202 }
jeffhaof56197c2012-03-05 18:01:54 -0800203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
206 const DexFile* dex_file,
207 mirror::DexCache* dex_cache,
208 mirror::ClassLoader* class_loader,
209 uint32_t class_def_idx,
210 const DexFile::CodeItem* code_item,
211 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700212 uint32_t method_access_flags,
213 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700214 MethodVerifier::FailureKind result = kNoFailure;
215 uint64_t start_ns = NanoTime();
216
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700218 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Verification completed, however failures may be pending that didn't cause the verification
221 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700222 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 if (verifier.failures_.size() != 0) {
224 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700225 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700226 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // Bad method data.
230 CHECK_NE(verifier.failures_.size(), 0U);
231 CHECK(verifier.have_pending_hard_failure_);
232 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800234 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700235 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800236 verifier.Dump(std::cout);
237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800239 }
Ian Rogersc8982582012-09-07 16:53:25 -0700240 uint64_t duration_ns = NanoTime() - start_ns;
241 if (duration_ns > MsToNs(100)) {
242 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
243 << " took " << PrettyDuration(duration_ns);
244 }
245 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800246}
247
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile* dex_file, mirror::DexCache* dex_cache,
250 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
251 const DexFile::CodeItem* code_item,
252 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800253 uint32_t method_access_flags) {
254 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700255 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257 verifier.DumpFailures(os);
258 os << verifier.info_messages_.str();
259 verifier.Dump(os);
260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
263 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
264 const DexFile::CodeItem* code_item,
265 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700266 uint32_t method_access_flags, bool can_load_classes,
267 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800268 : reg_types_(can_load_classes),
269 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700271 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800273 dex_file_(dex_file),
274 dex_cache_(dex_cache),
275 class_loader_(class_loader),
276 class_def_idx_(class_def_idx),
277 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700278 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700279 interesting_dex_pc_(-1),
280 monitor_enter_dex_pcs_(NULL),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200281 accessed_field(NULL),
282 invoked_method(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700284 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800285 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800286 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700287 can_load_classes_(can_load_classes),
288 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800292 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700293 MethodHelper mh(m);
294 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
295 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700296 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700297 verifier.interesting_dex_pc_ = dex_pc;
298 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
299 verifier.FindLocksAtDexPc();
300}
301
302void MethodVerifier::FindLocksAtDexPc() {
303 CHECK(monitor_enter_dex_pcs_ != NULL);
304 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
305
306 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
307 // verification. In practice, the phase we want relies on data structures set up by all the
308 // earlier passes, so we just run the full method verification and bail out early when we've
309 // got what we wanted.
310 Verify();
311}
312
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200313mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
314 uint32_t dex_pc) {
315 MethodHelper mh(m);
316 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
317 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
318 m, m->GetAccessFlags(), false, true);
319 mirror::Field* field = NULL;
320 verifier.interesting_dex_pc_ = dex_pc;
321 verifier.accessed_field = &field;
322 verifier.FindAccessedFieldAtDexPc();
323 return field;
324}
325
326void MethodVerifier::FindAccessedFieldAtDexPc() {
327 CHECK(accessed_field != NULL);
328 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
329
330 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
331 // verification. In practice, the phase we want relies on data structures set up by all the
332 // earlier passes, so we just run the full method verification and bail out early when we've
333 // got what we wanted.
334 Verify();
335}
336
337mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
338 uint32_t dex_pc) {
339 MethodHelper mh(m);
340 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
341 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
342 m, m->GetAccessFlags(), false, true);
343 mirror::AbstractMethod* method = NULL;
344 verifier.interesting_dex_pc_ = dex_pc;
345 verifier.invoked_method = &method;
346 verifier.FindInvokedMethodAtDexPc();
347 return method;
348}
349
350void MethodVerifier::FindInvokedMethodAtDexPc() {
351 CHECK(invoked_method != NULL);
352 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
353
354 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
355 // verification. In practice, the phase we want relies on data structures set up by all the
356 // earlier passes, so we just run the full method verification and bail out early when we've
357 // got what we wanted.
358 Verify();
359}
360
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 // If there aren't any instructions, make sure that's expected, then exit successfully.
363 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700364 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700366 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700367 } else {
368 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700369 }
jeffhaobdb76512011-09-07 11:43:16 -0700370 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
372 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
374 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700376 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800378 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 // Run through the instructions and see if the width checks out.
380 bool result = ComputeWidthsAndCountOps();
381 // Flag instructions guarded by a "try" block and check exception handlers.
382 result = result && ScanTryCatchBlocks();
383 // Perform static instruction verification.
384 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 // Perform code-flow analysis and return.
386 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700387}
388
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390 switch (error) {
391 case VERIFY_ERROR_NO_CLASS:
392 case VERIFY_ERROR_NO_FIELD:
393 case VERIFY_ERROR_NO_METHOD:
394 case VERIFY_ERROR_ACCESS_CLASS:
395 case VERIFY_ERROR_ACCESS_FIELD:
396 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700397 case VERIFY_ERROR_INSTANTIATION:
398 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800399 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700400 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
401 // class change and instantiation errors into soft verification errors so that we re-verify
402 // at runtime. We may fail to find or to agree on access because of not yet available class
403 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
404 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
405 // paths" that dynamically perform the verification and cause the behavior to be that akin
406 // to an interpreter.
407 error = VERIFY_ERROR_BAD_CLASS_SOFT;
408 } else {
409 have_pending_runtime_throw_failure_ = true;
410 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700411 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700412 // Indication that verification should be retried at runtime.
413 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700414 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700415 have_pending_hard_failure_ = true;
416 }
417 break;
jeffhaod5347e02012-03-22 17:25:05 -0700418 // Hard verification failures at compile time will still fail at runtime, so the class is
419 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 case VERIFY_ERROR_BAD_CLASS_HARD: {
421 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800422 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800423 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425 have_pending_hard_failure_ = true;
426 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800427 }
428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800430 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700431 work_insn_idx_));
432 std::ostringstream* failure_message = new std::ostringstream(location);
433 failure_messages_.push_back(failure_message);
434 return *failure_message;
435}
436
437void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
438 size_t failure_num = failure_messages_.size();
439 DCHECK_NE(failure_num, 0U);
440 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
441 prepend += last_fail_message->str();
442 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
443 delete last_fail_message;
444}
445
446void MethodVerifier::AppendToLastFailMessage(std::string append) {
447 size_t failure_num = failure_messages_.size();
448 DCHECK_NE(failure_num, 0U);
449 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
450 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800451}
452
Ian Rogers776ac1f2012-04-13 23:36:36 -0700453bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700454 const uint16_t* insns = code_item_->insns_;
455 size_t insns_size = code_item_->insns_size_in_code_units_;
456 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700457 size_t new_instance_count = 0;
458 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700460
Ian Rogersd81871c2011-10-03 13:57:23 -0700461 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700462 Instruction::Code opcode = inst->Opcode();
463 if (opcode == Instruction::NEW_INSTANCE) {
464 new_instance_count++;
465 } else if (opcode == Instruction::MONITOR_ENTER) {
466 monitor_enter_count++;
467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 size_t inst_size = inst->SizeInCodeUnits();
469 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
470 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700471 inst = inst->Next();
472 }
473
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
476 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700477 return false;
478 }
479
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 new_instance_count_ = new_instance_count;
481 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700482 return true;
483}
484
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700486 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700487 if (tries_size == 0) {
488 return true;
489 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700491 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700492
493 for (uint32_t idx = 0; idx < tries_size; idx++) {
494 const DexFile::TryItem* try_item = &tries[idx];
495 uint32_t start = try_item->start_addr_;
496 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700497 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
499 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700500 return false;
501 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700502 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700504 return false;
505 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 for (uint32_t dex_pc = start; dex_pc < end;
507 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
508 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700509 }
510 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800511 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700512 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700513 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700514 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700515 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700516 CatchHandlerIterator iterator(handlers_ptr);
517 for (; iterator.HasNext(); iterator.Next()) {
518 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700521 return false;
522 }
jeffhao60f83e32012-02-13 17:16:30 -0800523 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
524 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800527 return false;
528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700530 // Ensure exception types are resolved so that they don't need resolution to be delivered,
531 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700532 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
534 iterator.GetHandlerTypeIndex(),
535 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700536 if (exception_type == NULL) {
537 DCHECK(Thread::Current()->IsExceptionPending());
538 Thread::Current()->ClearException();
539 }
540 }
jeffhaobdb76512011-09-07 11:43:16 -0700541 }
Ian Rogers0571d352011-11-03 19:51:38 -0700542 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700543 }
jeffhaobdb76512011-09-07 11:43:16 -0700544 return true;
545}
546
Ian Rogers776ac1f2012-04-13 23:36:36 -0700547bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700549
Ian Rogers0c7abda2012-09-19 13:33:42 -0700550 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700552 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700553
554 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700555 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 return false;
559 }
560 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700561 // All invoke points are marked as "Throw" points already.
562 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700564 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 }
566 dex_pc += inst->SizeInCodeUnits();
567 inst = inst->Next();
568 }
569 return true;
570}
571
Ian Rogers776ac1f2012-04-13 23:36:36 -0700572bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 bool result = true;
575 switch (inst->GetVerifyTypeArgumentA()) {
576 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800577 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 break;
579 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 break;
582 }
583 switch (inst->GetVerifyTypeArgumentB()) {
584 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800588 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 break;
590 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800591 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 break;
593 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800600 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 break;
602 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800603 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 break;
605 }
606 switch (inst->GetVerifyTypeArgumentC()) {
607 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 }
623 switch (inst->GetVerifyExtraFlags()) {
624 case Instruction::kVerifyArrayData:
625 result = result && CheckArrayData(code_offset);
626 break;
627 case Instruction::kVerifyBranchTarget:
628 result = result && CheckBranchTarget(code_offset);
629 break;
630 case Instruction::kVerifySwitchTargets:
631 result = result && CheckSwitchTargets(code_offset);
632 break;
633 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 result = false;
642 break;
643 }
644 return result;
645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
650 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 return false;
652 }
653 return true;
654}
655
Ian Rogers776ac1f2012-04-13 23:36:36 -0700656bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
659 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 return false;
661 }
662 return true;
663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
668 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 return false;
670 }
671 return true;
672}
673
Ian Rogers776ac1f2012-04-13 23:36:36 -0700674bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
677 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 return false;
679 }
680 return true;
681}
682
Ian Rogers776ac1f2012-04-13 23:36:36 -0700683bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
686 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700690 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
701 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
710 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
719 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700723 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 const char* cp = descriptor;
725 while (*cp++ == '[') {
726 bracket_count++;
727 }
728 if (bracket_count == 0) {
729 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 } else if (bracket_count > 255) {
733 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
742 const uint16_t* insns = code_item_->insns_ + cur_offset;
743 const uint16_t* array_data;
744 int32_t array_data_offset;
745
746 DCHECK_LT(cur_offset, insn_count);
747 /* make sure the start of the array data table is in range */
748 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
749 if ((int32_t) cur_offset + array_data_offset < 0 ||
750 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
752 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 /* offset to array data table is a relative branch-style offset */
756 array_data = insns + array_data_offset;
757 /* make sure the table is 32-bit aligned */
758 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
760 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700764 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
766 /* make sure the end of the switch is in range */
767 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
769 << ", data offset " << array_data_offset << ", end "
770 << cur_offset + array_data_offset + table_size
771 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 int32_t offset;
779 bool isConditional, selfOkay;
780 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
781 return false;
782 }
783 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700784 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 -0700785 return false;
786 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700787 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
788 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
794 int32_t abs_offset = cur_offset + offset;
795 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 << reinterpret_cast<void*>(abs_offset) << ") at "
798 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 insn_flags_[abs_offset].SetBranchTarget();
802 return true;
803}
804
Ian Rogers776ac1f2012-04-13 23:36:36 -0700805bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 bool* selfOkay) {
807 const uint16_t* insns = code_item_->insns_ + cur_offset;
808 *pConditional = false;
809 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 switch (*insns & 0xff) {
811 case Instruction::GOTO:
812 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 break;
814 case Instruction::GOTO_32:
815 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 *selfOkay = true;
817 break;
818 case Instruction::GOTO_16:
819 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 break;
821 case Instruction::IF_EQ:
822 case Instruction::IF_NE:
823 case Instruction::IF_LT:
824 case Instruction::IF_GE:
825 case Instruction::IF_GT:
826 case Instruction::IF_LE:
827 case Instruction::IF_EQZ:
828 case Instruction::IF_NEZ:
829 case Instruction::IF_LTZ:
830 case Instruction::IF_GEZ:
831 case Instruction::IF_GTZ:
832 case Instruction::IF_LEZ:
833 *pOffset = (int16_t) insns[1];
834 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700835 break;
836 default:
837 return false;
838 break;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 return true;
841}
842
Ian Rogers776ac1f2012-04-13 23:36:36 -0700843bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
849 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
851 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 return false;
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 /* make sure the table is 32-bit aligned */
857 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
859 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 uint32_t switch_count = switch_insns[1];
863 int32_t keys_offset, targets_offset;
864 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700865 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
866 /* 0=sig, 1=count, 2/3=firstKey */
867 targets_offset = 4;
868 keys_offset = -1;
869 expected_signature = Instruction::kPackedSwitchSignature;
870 } else {
871 /* 0=sig, 1=count, 2..count*2 = keys */
872 keys_offset = 2;
873 targets_offset = 2 + 2 * switch_count;
874 expected_signature = Instruction::kSparseSwitchSignature;
875 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
879 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 /* make sure the end of the switch is in range */
883 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
885 << switch_offset << ", end "
886 << (cur_offset + switch_offset + table_size)
887 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return false;
889 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 /* for a sparse switch, verify the keys are in ascending order */
891 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
893 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
895 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
896 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
898 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 return false;
900 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 last_key = key;
902 }
903 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 for (uint32_t targ = 0; targ < switch_count; targ++) {
906 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
907 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
908 int32_t abs_offset = cur_offset + offset;
909 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700911 << reinterpret_cast<void*>(abs_offset) << ") at "
912 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 return false;
914 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 insn_flags_[abs_offset].SetBranchTarget();
916 }
917 return true;
918}
919
Ian Rogers776ac1f2012-04-13 23:36:36 -0700920bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 return false;
924 }
925 uint16_t registers_size = code_item_->registers_size_;
926 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800927 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
929 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 }
933
934 return true;
935}
936
Ian Rogers776ac1f2012-04-13 23:36:36 -0700937bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 uint16_t registers_size = code_item_->registers_size_;
939 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
940 // integer overflow when adding them here.
941 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
943 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 return false;
945 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return true;
947}
948
Ian Rogers0c7abda2012-09-19 13:33:42 -0700949static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800950 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700951 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800952 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
953 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
954 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
955 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
956 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
957 gc_map.begin(),
958 gc_map.end());
959 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
960 DCHECK_EQ(gc_map.size(),
961 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
962 (length_prefixed_gc_map->at(1) << 16) |
963 (length_prefixed_gc_map->at(2) << 8) |
964 (length_prefixed_gc_map->at(3) << 0)));
965 return length_prefixed_gc_map;
966}
967
Ian Rogers776ac1f2012-04-13 23:36:36 -0700968bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 uint16_t registers_size = code_item_->registers_size_;
970 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700971
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800973 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
974 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 }
976 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700977 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
978
jeffhaobdb76512011-09-07 11:43:16 -0700979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 work_line_.reset(new RegisterLine(registers_size, this));
981 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700982
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 /* Initialize register types of method arguments. */
984 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700985 DCHECK_NE(failures_.size(), 0U);
986 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700988 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 /* Perform code flow verification. */
992 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700993 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700995 }
996
Ian Rogers1212a022013-03-04 10:48:41 -0800997 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700998
TDYa127b2eb5c12012-05-24 15:52:10 -0700999
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001001 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1002 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001003 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 return false; // Not a real failure, but a failure to encode
1005 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07001006 if (kIsDebugBuild) {
1007 VerifyGcMap(*map);
1008 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07001009 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1010 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001011
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001012 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
1013 if(method_to_safe_casts != NULL ) {
1014 SetSafeCastMap(ref, method_to_safe_casts);
1015 }
1016
Ian Rogersd0583802013-06-01 10:51:46 -07001017 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001018 if(pc_to_concrete_method != NULL ) {
1019 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001020 }
jeffhaobdb76512011-09-07 11:43:16 -07001021 return true;
1022}
1023
Ian Rogersad0b3a32012-04-16 14:50:24 -07001024std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1025 DCHECK_EQ(failures_.size(), failure_messages_.size());
1026 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001027 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001028 }
1029 return os;
1030}
1031
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 v->Dump(std::cerr);
1035}
1036
Ian Rogers776ac1f2012-04-13 23:36:36 -07001037void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001038 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001039 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 return;
jeffhaobdb76512011-09-07 11:43:16 -07001041 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001042 {
1043 os << "Register Types:\n";
1044 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1045 std::ostream indent_os(&indent_filter);
1046 reg_types_.Dump(indent_os);
1047 }
Ian Rogersb4903572012-10-11 11:52:56 -07001048 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001049 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1050 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 const Instruction* inst = Instruction::At(code_item_->insns_);
1052 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1053 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1055 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001056 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001057 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001058 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001059 const bool kDumpHexOfInstruction = false;
1060 if (kDumpHexOfInstruction) {
1061 indent_os << inst->DumpHex(5) << " ";
1062 }
1063 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001064 inst = inst->Next();
1065 }
jeffhaobdb76512011-09-07 11:43:16 -07001066}
1067
Ian Rogersd81871c2011-10-03 13:57:23 -07001068static bool IsPrimitiveDescriptor(char descriptor) {
1069 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001070 case 'I':
1071 case 'C':
1072 case 'S':
1073 case 'B':
1074 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001075 case 'F':
1076 case 'D':
1077 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001079 default:
1080 return false;
1081 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001082}
1083
Ian Rogers776ac1f2012-04-13 23:36:36 -07001084bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 RegisterLine* reg_line = reg_table_.GetLine(0);
1086 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1087 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001088
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1090 //Include the "this" pointer.
1091 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001092 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1094 // argument as uninitialized. This restricts field access until the superclass constructor is
1095 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 const RegType& declaring_class = GetDeclaringClass();
1097 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 reg_line->SetRegisterType(arg_start + cur_arg,
1099 reg_types_.UninitializedThisArgument(declaring_class));
1100 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001104 }
1105
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001106 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001107 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001108 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001109
1110 for (; iterator.HasNext(); iterator.Next()) {
1111 const char* descriptor = iterator.GetDescriptor();
1112 if (descriptor == NULL) {
1113 LOG(FATAL) << "Null descriptor";
1114 }
1115 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1117 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return false;
1119 }
1120 switch (descriptor[0]) {
1121 case 'L':
1122 case '[':
1123 // We assume that reference arguments are initialized. The only way it could be otherwise
1124 // (assuming the caller was verified) is if the current method is <init>, but in that case
1125 // it's effectively considered initialized the instant we reach here (in the sense that we
1126 // can return without doing anything or call virtual methods).
1127 {
Ian Rogersb4903572012-10-11 11:52:56 -07001128 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001129 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 }
1131 break;
1132 case 'Z':
1133 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1134 break;
1135 case 'C':
1136 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1137 break;
1138 case 'B':
1139 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1140 break;
1141 case 'I':
1142 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1143 break;
1144 case 'S':
1145 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1146 break;
1147 case 'F':
1148 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1149 break;
1150 case 'J':
1151 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001152 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1153 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1154 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 cur_arg++;
1156 break;
1157 }
1158 default:
jeffhaod5347e02012-03-22 17:25:05 -07001159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 return false;
1161 }
1162 cur_arg++;
1163 }
1164 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 return false;
1167 }
1168 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1169 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1170 // format. Only major difference from the method argument format is that 'V' is supported.
1171 bool result;
1172 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1173 result = descriptor[1] == '\0';
1174 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1175 size_t i = 0;
1176 do {
1177 i++;
1178 } while (descriptor[i] == '['); // process leading [
1179 if (descriptor[i] == 'L') { // object array
1180 do {
1181 i++; // find closing ;
1182 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1183 result = descriptor[i] == ';';
1184 } else { // primitive array
1185 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1186 }
1187 } else if (descriptor[0] == 'L') {
1188 // could be more thorough here, but shouldn't be required
1189 size_t i = 0;
1190 do {
1191 i++;
1192 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1193 result = descriptor[i] == ';';
1194 } else {
1195 result = false;
1196 }
1197 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1199 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 }
1201 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 const uint16_t* insns = code_item_->insns_;
1206 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001207
jeffhaobdb76512011-09-07 11:43:16 -07001208 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 insn_flags_[0].SetChanged();
1210 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211
jeffhaobdb76512011-09-07 11:43:16 -07001212 /* Continue until no instructions are marked "changed". */
1213 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1215 uint32_t insn_idx = start_guess;
1216 for (; insn_idx < insns_size; insn_idx++) {
1217 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001218 break;
1219 }
jeffhaobdb76512011-09-07 11:43:16 -07001220 if (insn_idx == insns_size) {
1221 if (start_guess != 0) {
1222 /* try again, starting from the top */
1223 start_guess = 0;
1224 continue;
1225 } else {
1226 /* all flags are clear */
1227 break;
1228 }
1229 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 // We carry the working set of registers from instruction to instruction. If this address can
1231 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1232 // "changed" flags, we need to load the set of registers from the table.
1233 // Because we always prefer to continue on to the next instruction, we should never have a
1234 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1235 // target.
1236 work_insn_idx_ = insn_idx;
1237 if (insn_flags_[insn_idx].IsBranchTarget()) {
1238 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001239 } else {
1240#ifndef NDEBUG
1241 /*
1242 * Sanity check: retrieve the stored register line (assuming
1243 * a full table) and make sure it actually matches.
1244 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1246 if (register_line != NULL) {
1247 if (work_line_->CompareLine(register_line) != 0) {
1248 Dump(std::cout);
1249 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001251 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1252 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001253 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 }
jeffhaobdb76512011-09-07 11:43:16 -07001255 }
1256#endif
1257 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001260 prepend += " failed to verify: ";
1261 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 return false;
1263 }
jeffhaobdb76512011-09-07 11:43:16 -07001264 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 insn_flags_[insn_idx].SetVisited();
1266 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001267 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001268
Ian Rogers1c849e52012-06-28 14:00:33 -07001269 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001270 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001271 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001272 * (besides the wasted space), but it indicates a flaw somewhere
1273 * down the line, possibly in the verifier.
1274 *
1275 * If we've substituted "always throw" instructions into the stream,
1276 * we are almost certainly going to have some dead code.
1277 */
1278 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 uint32_t insn_idx = 0;
1280 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001281 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001282 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001283 * may or may not be preceded by a padding NOP (for alignment).
1284 */
1285 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1286 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1287 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001288 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001289 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1290 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1291 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001293 }
1294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001296 if (dead_start < 0)
1297 dead_start = insn_idx;
1298 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001299 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001300 dead_start = -1;
1301 }
1302 }
1303 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001304 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001306 // To dump the state of the verify after a method, do something like:
1307 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1308 // "boolean java.lang.String.equals(java.lang.Object)") {
1309 // LOG(INFO) << info_messages_.str();
1310 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001311 }
jeffhaobdb76512011-09-07 11:43:16 -07001312 return true;
1313}
1314
Ian Rogers776ac1f2012-04-13 23:36:36 -07001315bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001316 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1317 // We want the state _before_ the instruction, for the case where the dex pc we're
1318 // interested in is itself a monitor-enter instruction (which is a likely place
1319 // for a thread to be suspended).
1320 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001321 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001322 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1323 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1324 }
1325 }
1326
jeffhaobdb76512011-09-07 11:43:16 -07001327 /*
1328 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001329 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001330 * control to another statement:
1331 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001332 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001333 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001334 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001335 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001337 * throw an exception that is handled by an encompassing "try"
1338 * block.
1339 *
1340 * We can also return, in which case there is no successor instruction
1341 * from this point.
1342 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001343 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001344 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1346 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001347 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001348
jeffhaobdb76512011-09-07 11:43:16 -07001349 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001350 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001351 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001353 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1354 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 }
jeffhaobdb76512011-09-07 11:43:16 -07001356
1357 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001358 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001359 * can throw an exception, we will copy/merge this into the "catch"
1360 * address rather than work_line, because we don't want the result
1361 * from the "successful" code path (e.g. a check-cast that "improves"
1362 * a type) to be visible to the exception handler.
1363 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001366 } else {
1367#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001369#endif
1370 }
1371
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001372
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001373 // We need to ensure the work line is consistent while performing validation. When we spot a
1374 // peephole pattern we compute a new line for either the fallthrough instruction or the
1375 // branch target.
1376 UniquePtr<RegisterLine> branch_line;
1377 UniquePtr<RegisterLine> fallthrough_line;
1378
Sebastien Hertz5243e912013-05-21 10:55:07 +02001379 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001380 case Instruction::NOP:
1381 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001382 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001383 * a signature that looks like a NOP; if we see one of these in
1384 * the course of executing code then we have a problem.
1385 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001386 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001388 }
1389 break;
1390
1391 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001392 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1393 break;
jeffhaobdb76512011-09-07 11:43:16 -07001394 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001395 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1396 break;
jeffhaobdb76512011-09-07 11:43:16 -07001397 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001398 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001399 break;
1400 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001401 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1402 break;
jeffhaobdb76512011-09-07 11:43:16 -07001403 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001404 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1405 break;
jeffhaobdb76512011-09-07 11:43:16 -07001406 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001407 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001408 break;
1409 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001410 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1411 break;
jeffhaobdb76512011-09-07 11:43:16 -07001412 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001413 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1414 break;
jeffhaobdb76512011-09-07 11:43:16 -07001415 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001416 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001417 break;
1418
1419 /*
1420 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001421 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001422 * might want to hold the result in an actual CPU register, so the
1423 * Dalvik spec requires that these only appear immediately after an
1424 * invoke or filled-new-array.
1425 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001426 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001427 * redundant with the reset done below, but it can make the debug info
1428 * easier to read in some cases.)
1429 */
1430 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001431 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001432 break;
1433 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001434 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001435 break;
1436 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001437 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001438 break;
1439
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001441 /*
jeffhao60f83e32012-02-13 17:16:30 -08001442 * This statement can only appear as the first instruction in an exception handler. We verify
1443 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001444 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001445 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001446 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001447 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 }
jeffhaobdb76512011-09-07 11:43:16 -07001449 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001450 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1451 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 }
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
1455 break;
1456 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001457 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001458 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 const RegType& return_type = GetMethodReturnType();
1460 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 } else {
1463 // Compilers may generate synthetic functions that write byte values into boolean fields.
1464 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001465 const uint32_t vregA = inst->VRegA_11x();
1466 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1468 ((return_type.IsBoolean() || return_type.IsByte() ||
1469 return_type.IsShort() || return_type.IsChar()) &&
1470 src_type.IsInteger()));
1471 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001472 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001474 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477 }
1478 }
1479 break;
1480 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001481 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001482 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 const RegType& return_type = GetMethodReturnType();
1484 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 } else {
1487 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 const uint32_t vregA = inst->VRegA_11x();
1489 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001490 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001491 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 }
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
1494 }
1495 break;
1496 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 const RegType& return_type = GetMethodReturnType();
1499 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 } else {
1502 /* return_type is the *expected* return type, not register value */
1503 DCHECK(!return_type.IsZero());
1504 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 const uint32_t vregA = inst->VRegA_11x();
1506 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001507 // Disallow returning uninitialized values and verify that the reference in vAA is an
1508 // instance of the "return_type"
1509 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001510 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001511 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001512 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1513 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
1515 }
1516 }
1517 break;
1518
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001520 case Instruction::CONST_4: {
1521 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1522 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001523 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 }
1525 case Instruction::CONST_16: {
1526 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1527 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001528 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 }
jeffhaobdb76512011-09-07 11:43:16 -07001530 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 work_line_->SetRegisterType(inst->VRegA_31i(),
1532 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
1534 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 work_line_->SetRegisterType(inst->VRegA_21h(),
1536 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
jeffhaobdb76512011-09-07 11:43:16 -07001538 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1542 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 }
1546 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1549 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 break;
1552 }
1553 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1556 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 break;
1559 }
1560 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1563 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 break;
1566 }
jeffhaobdb76512011-09-07 11:43:16 -07001567 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1569 break;
jeffhaobdb76512011-09-07 11:43:16 -07001570 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001572 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001574 // Get type from instruction if unresolved then we need an access check
1575 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001576 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001579 res_type.IsConflict() ? res_type
1580 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 }
jeffhaobdb76512011-09-07 11:43:16 -07001583 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001585 break;
1586 case Instruction::MONITOR_EXIT:
1587 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001588 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001589 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001590 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001591 * to the need to handle asynchronous exceptions, a now-deprecated
1592 * feature that Dalvik doesn't support.)
1593 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001594 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001595 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001597 * structured locking checks are working, the former would have
1598 * failed on the -enter instruction, and the latter is impossible.
1599 *
1600 * This is fortunate, because issue 3221411 prevents us from
1601 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001603 * some catch blocks (which will show up as "dead" code when
1604 * we skip them here); if we can't, then the code path could be
1605 * "live" so we still need to check it.
1606 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001608 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610
Ian Rogers28ad40d2011-10-27 15:19:26 -07001611 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001613 /*
1614 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1615 * could be a "upcast" -- not expected, so we don't try to address it.)
1616 *
1617 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001618 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001619 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1621 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1622 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001623 if (res_type.IsConflict()) {
1624 DCHECK_NE(failures_.size(), 0U);
1625 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001627 }
1628 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001629 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001630 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1632 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001633 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 if (is_checkcast) {
1635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1636 } else {
1637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1638 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001639 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 if (is_checkcast) {
1641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1642 } else {
1643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1644 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001645 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001646 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
1654 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001656 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001657 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
1662 }
1663 break;
1664 }
1665 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001666 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001667 if (res_type.IsConflict()) {
1668 DCHECK_NE(failures_.size(), 0U);
1669 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001670 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1672 // can't create an instance of an interface or abstract class */
1673 if (!res_type.IsInstantiableTypes()) {
1674 Fail(VERIFY_ERROR_INSTANTIATION)
1675 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001676 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001678 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1679 // Any registers holding previous allocations from this address that have not yet been
1680 // initialized must be marked invalid.
1681 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1682 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 break;
1685 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001686 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001691 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001693 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001695 just_set_result = true; // Filled new array range sets result register
1696 break;
jeffhaobdb76512011-09-07 11:43:16 -07001697 case Instruction::CMPL_FLOAT:
1698 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001700 break;
1701 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001703 break;
1704 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707 case Instruction::CMPL_DOUBLE:
1708 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001711 break;
1712 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001715 break;
1716 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001718 break;
1719 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001722 break;
1723 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001725 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001726 break;
1727 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001729 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001732 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001734 }
1735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::GOTO:
1738 case Instruction::GOTO_16:
1739 case Instruction::GOTO_32:
1740 /* no effect on or use of registers */
1741 break;
1742
1743 case Instruction::PACKED_SWITCH:
1744 case Instruction::SPARSE_SWITCH:
1745 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001747 break;
1748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 case Instruction::FILL_ARRAY_DATA: {
1750 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001751 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001752 /* array_type can be null if the reg type is Zero */
1753 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001754 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001756 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001757 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1758 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001759 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1761 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 } else {
jeffhao457cc512012-02-02 16:55:13 -08001763 // Now verify if the element width in the table matches the element width declared in
1764 // the array
1765 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1766 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001768 } else {
1769 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1770 // Since we don't compress the data in Dex, expect to see equal width of data stored
1771 // in the table and expected from the array class.
1772 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1774 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001775 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
1777 }
jeffhaobdb76512011-09-07 11:43:16 -07001778 }
1779 }
1780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 }
jeffhaobdb76512011-09-07 11:43:16 -07001782 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1785 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 bool mismatch = false;
1787 if (reg_type1.IsZero()) { // zero then integral or reference expected
1788 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1789 } else if (reg_type1.IsReferenceTypes()) { // both references?
1790 mismatch = !reg_type2.IsReferenceTypes();
1791 } else { // both integral?
1792 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1793 }
1794 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1796 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 case Instruction::IF_LT:
1801 case Instruction::IF_GE:
1802 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1805 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1808 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001809 }
1810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001818
1819 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001820 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001821 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001822 instance_of_idx = work_insn_idx_ - 1;
1823 while(0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
1824 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001825 }
Ian Rogers9b360392013-06-06 14:45:07 -07001826 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001827 } else {
1828 break;
1829 }
1830
Ian Rogers9b360392013-06-06 14:45:07 -07001831 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001832
1833 /* Check for peep-hole pattern of:
1834 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001835 * instance-of vX, vY, T;
1836 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001837 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001838 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001839 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001840 * and sharpen the type of vY to be type T.
1841 * Note, this pattern can't be if:
1842 * - if there are other branches to this branch,
1843 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001844 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001845 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001846 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1847 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1848 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001849 // Check that the we are not attempting conversion to interface types,
1850 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001851 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001852
Ian Rogersfae370a2013-06-05 08:33:27 -07001853 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001854 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001855 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001856 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001857 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001858 branch_line.reset(update_line);
1859 }
1860 update_line->CopyFromLine(work_line_.get());
1861 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1862 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1863 // See if instance-of was preceded by a move-object operation, common due to the small
1864 // register encoding space of instance-of, and propagate type information to the source
1865 // of the move-object.
1866 uint32_t move_idx = instance_of_idx - 1;
1867 while(0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
1868 move_idx--;
1869 }
1870 CHECK(insn_flags_[move_idx].IsOpcode());
1871 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1872 switch (move_inst->Opcode()) {
1873 case Instruction::MOVE_OBJECT:
1874 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1875 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1876 }
1877 break;
1878 case Instruction::MOVE_OBJECT_FROM16:
1879 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1880 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1881 }
1882 break;
1883 case Instruction::MOVE_OBJECT_16:
1884 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1885 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1886 }
1887 break;
1888 default:
1889 break;
1890 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001891 }
1892 }
1893 }
1894
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 }
jeffhaobdb76512011-09-07 11:43:16 -07001897 case Instruction::IF_LTZ:
1898 case Instruction::IF_GEZ:
1899 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001901 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1904 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 break;
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 break;
jeffhaobdb76512011-09-07 11:43:16 -07001914 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 break;
jeffhaobdb76512011-09-07 11:43:16 -07001917 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 break;
jeffhaobdb76512011-09-07 11:43:16 -07001923 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001924 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 break;
1926 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001928 break;
1929
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 break;
1933 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 break;
1936 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 break;
1939 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001941 break;
1942 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001946 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001949 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 break;
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 break;
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001959 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 break;
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001962 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 break;
1964 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
1967 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
1970 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 break;
jeffhaobdb76512011-09-07 11:43:16 -07001973
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 break;
1977 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 break;
1980 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 break;
1983 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
1986 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 break;
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
2008 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
2017
2018 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039
2040 case Instruction::INVOKE_VIRTUAL:
2041 case Instruction::INVOKE_VIRTUAL_RANGE:
2042 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2045 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2046 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2047 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2048 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002049 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002050 const char* descriptor;
2051 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002053 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2054 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2055 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2056 } else {
2057 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002058 }
Ian Rogersb4903572012-10-11 11:52:56 -07002059 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 if (!return_type.IsLowHalf()) {
2061 work_line_->SetResultRegisterType(return_type);
2062 } else {
2063 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2064 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002065 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 }
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2071 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002072 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002073 const char* return_type_descriptor;
2074 bool is_constructor;
2075 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002077 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2078 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2079 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2080 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2081 } else {
2082 is_constructor = called_method->IsConstructor();
2083 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2084 }
2085 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002086 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 * Some additional checks when calling a constructor. We know from the invocation arg check
2088 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2089 * that to require that called_method->klass is the same as this->klass or this->super,
2090 * allowing the latter only if the "this" argument is the same as the "this" argument to
2091 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002092 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002094 if (this_type.IsConflict()) // failure.
2095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096
jeffhaob57e9522012-04-26 18:08:21 -07002097 /* no null refs allowed (?) */
2098 if (this_type.IsZero()) {
2099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2100 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002101 }
jeffhaob57e9522012-04-26 18:08:21 -07002102
2103 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002104 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2105 // TODO: re-enable constructor type verification
2106 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002107 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002108 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2109 // break;
2110 // }
jeffhaob57e9522012-04-26 18:08:21 -07002111
2112 /* arg must be an uninitialized reference */
2113 if (!this_type.IsUninitializedTypes()) {
2114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2115 << this_type;
2116 break;
2117 }
2118
2119 /*
2120 * Replace the uninitialized reference with an initialized one. We need to do this for all
2121 * registers that have the same object instance in them, not just the "this" register.
2122 */
2123 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002124 }
Ian Rogersb4903572012-10-11 11:52:56 -07002125 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2126 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002127 if (!return_type.IsLowHalf()) {
2128 work_line_->SetResultRegisterType(return_type);
2129 } else {
2130 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2131 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002132 just_set_result = true;
2133 break;
2134 }
2135 case Instruction::INVOKE_STATIC:
2136 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2138 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002139 const char* descriptor;
2140 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002142 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2143 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002144 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002145 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002146 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002147 }
Ian Rogersb4903572012-10-11 11:52:56 -07002148 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 if (!return_type.IsLowHalf()) {
2150 work_line_->SetResultRegisterType(return_type);
2151 } else {
2152 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2153 }
jeffhaobdb76512011-09-07 11:43:16 -07002154 just_set_result = true;
2155 }
2156 break;
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2160 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002161 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002162 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002163 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2164 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2165 << PrettyMethod(abs_method) << "'";
2166 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002167 }
Ian Rogers0d604842012-04-16 14:50:24 -07002168 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002169 /* Get the type of the "this" arg, which should either be a sub-interface of called
2170 * interface or Object (see comments in RegType::JoinClass).
2171 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002173 if (this_type.IsZero()) {
2174 /* null pointer always passes (and always fails at runtime) */
2175 } else {
2176 if (this_type.IsUninitializedTypes()) {
2177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2178 << this_type;
2179 break;
2180 }
2181 // In the past we have tried to assert that "called_interface" is assignable
2182 // from "this_type.GetClass()", however, as we do an imprecise Join
2183 // (RegType::JoinClass) we don't have full information on what interfaces are
2184 // implemented by "this_type". For example, two classes may implement the same
2185 // interfaces and have a common parent that doesn't implement the interface. The
2186 // join will set "this_type" to the parent class and a test that this implements
2187 // the interface will incorrectly fail.
2188 }
2189 /*
2190 * We don't have an object instance, so we can't find the concrete method. However, all of
2191 * the type information is in the abstract method, so we're good.
2192 */
2193 const char* descriptor;
2194 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002196 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2197 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2198 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2199 } else {
2200 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2201 }
Ian Rogersb4903572012-10-11 11:52:56 -07002202 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002203 if (!return_type.IsLowHalf()) {
2204 work_line_->SetResultRegisterType(return_type);
2205 } else {
2206 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2207 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002208 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002209 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 }
jeffhaobdb76512011-09-07 11:43:16 -07002211 case Instruction::NEG_INT:
2212 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::NEG_LONG:
2216 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002245 break;
2246 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002267 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002271 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
2279 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002280 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282
2283 case Instruction::ADD_INT:
2284 case Instruction::SUB_INT:
2285 case Instruction::MUL_INT:
2286 case Instruction::REM_INT:
2287 case Instruction::DIV_INT:
2288 case Instruction::SHL_INT:
2289 case Instruction::SHR_INT:
2290 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::AND_INT:
2295 case Instruction::OR_INT:
2296 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300 case Instruction::ADD_LONG:
2301 case Instruction::SUB_LONG:
2302 case Instruction::MUL_LONG:
2303 case Instruction::DIV_LONG:
2304 case Instruction::REM_LONG:
2305 case Instruction::AND_LONG:
2306 case Instruction::OR_LONG:
2307 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 reg_types_.LongLo(), reg_types_.LongHi(),
2310 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::SHL_LONG:
2313 case Instruction::SHR_LONG:
2314 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::ADD_FLOAT:
2320 case Instruction::SUB_FLOAT:
2321 case Instruction::MUL_FLOAT:
2322 case Instruction::DIV_FLOAT:
2323 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::ADD_DOUBLE:
2327 case Instruction::SUB_DOUBLE:
2328 case Instruction::MUL_DOUBLE:
2329 case Instruction::DIV_DOUBLE:
2330 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2333 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::ADD_INT_2ADDR:
2336 case Instruction::SUB_INT_2ADDR:
2337 case Instruction::MUL_INT_2ADDR:
2338 case Instruction::REM_INT_2ADDR:
2339 case Instruction::SHL_INT_2ADDR:
2340 case Instruction::SHR_INT_2ADDR:
2341 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::AND_INT_2ADDR:
2345 case Instruction::OR_INT_2ADDR:
2346 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::ADD_LONG_2ADDR:
2353 case Instruction::SUB_LONG_2ADDR:
2354 case Instruction::MUL_LONG_2ADDR:
2355 case Instruction::DIV_LONG_2ADDR:
2356 case Instruction::REM_LONG_2ADDR:
2357 case Instruction::AND_LONG_2ADDR:
2358 case Instruction::OR_LONG_2ADDR:
2359 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.LongLo(), reg_types_.LongHi(),
2362 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::SHL_LONG_2ADDR:
2365 case Instruction::SHR_LONG_2ADDR:
2366 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::ADD_FLOAT_2ADDR:
2371 case Instruction::SUB_FLOAT_2ADDR:
2372 case Instruction::MUL_FLOAT_2ADDR:
2373 case Instruction::DIV_FLOAT_2ADDR:
2374 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::ADD_DOUBLE_2ADDR:
2378 case Instruction::SUB_DOUBLE_2ADDR:
2379 case Instruction::MUL_DOUBLE_2ADDR:
2380 case Instruction::DIV_DOUBLE_2ADDR:
2381 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2384 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::ADD_INT_LIT16:
2387 case Instruction::RSUB_INT:
2388 case Instruction::MUL_INT_LIT16:
2389 case Instruction::DIV_INT_LIT16:
2390 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::AND_INT_LIT16:
2394 case Instruction::OR_INT_LIT16:
2395 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::ADD_INT_LIT8:
2399 case Instruction::RSUB_INT_LIT8:
2400 case Instruction::MUL_INT_LIT8:
2401 case Instruction::DIV_INT_LIT8:
2402 case Instruction::REM_INT_LIT8:
2403 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002405 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::AND_INT_LIT8:
2409 case Instruction::OR_INT_LIT8:
2410 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002414 // Special instructions.
2415 //
2416 // Note: the following instructions encode offsets derived from class linking.
2417 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2418 // meaning if the class linking and resolution were successful.
2419 case Instruction::IGET_QUICK:
2420 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2421 break;
2422 case Instruction::IGET_WIDE_QUICK:
2423 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2424 break;
2425 case Instruction::IGET_OBJECT_QUICK:
2426 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2427 break;
2428 case Instruction::IPUT_QUICK:
2429 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2430 break;
2431 case Instruction::IPUT_WIDE_QUICK:
2432 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2433 break;
2434 case Instruction::IPUT_OBJECT_QUICK:
2435 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2436 break;
2437 case Instruction::INVOKE_VIRTUAL_QUICK:
2438 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2439 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2440 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2441 if (called_method != NULL) {
2442 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2443 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2444 if (!return_type.IsLowHalf()) {
2445 work_line_->SetResultRegisterType(return_type);
2446 } else {
2447 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2448 }
2449 just_set_result = true;
2450 }
2451 break;
2452 }
2453
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002455 case Instruction::UNUSED_3E:
2456 case Instruction::UNUSED_3F:
2457 case Instruction::UNUSED_40:
2458 case Instruction::UNUSED_41:
2459 case Instruction::UNUSED_42:
2460 case Instruction::UNUSED_43:
2461 case Instruction::UNUSED_73:
2462 case Instruction::UNUSED_79:
2463 case Instruction::UNUSED_7A:
2464 case Instruction::UNUSED_EB:
2465 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002466 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002467 case Instruction::UNUSED_EE:
2468 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002469 case Instruction::UNUSED_F0:
2470 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002471 case Instruction::UNUSED_F2:
2472 case Instruction::UNUSED_F3:
2473 case Instruction::UNUSED_F4:
2474 case Instruction::UNUSED_F5:
2475 case Instruction::UNUSED_F6:
2476 case Instruction::UNUSED_F7:
2477 case Instruction::UNUSED_F8:
2478 case Instruction::UNUSED_F9:
2479 case Instruction::UNUSED_FA:
2480 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002481 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002482 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002483 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002484 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487
2488 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002489 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002490 * complain if an instruction is missing (which is desirable).
2491 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002492 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002493
Ian Rogersad0b3a32012-04-16 14:50:24 -07002494 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002495 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002496 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002497 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002498 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002499 /* immediate failure, reject class */
2500 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2501 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002502 } else if (have_pending_runtime_throw_failure_) {
2503 /* slow path will throw, mark following code as unreachable */
2504 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002505 }
jeffhaobdb76512011-09-07 11:43:16 -07002506 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 * If we didn't just set the result register, clear it out. This ensures that you can only use
2508 * "move-result" immediately after the result is set. (We could check this statically, but it's
2509 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002510 */
2511 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002513 }
2514
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002515
jeffhaobdb76512011-09-07 11:43:16 -07002516
2517 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002518 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002519 *
2520 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002521 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002522 * somebody could get a reference field, check it for zero, and if the
2523 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002524 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002525 * that, and will reject the code.
2526 *
2527 * TODO: avoid re-fetching the branch target
2528 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002529 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002530 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002532 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002534 return false;
2535 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002536 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002537 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002538 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 }
jeffhaobdb76512011-09-07 11:43:16 -07002540 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002541 if (NULL != branch_line.get()) {
2542 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2543 return false;
2544 }
2545 } else {
2546 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2547 return false;
2548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 }
jeffhaobdb76512011-09-07 11:43:16 -07002550 }
2551
2552 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002553 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002554 *
2555 * We've already verified that the table is structurally sound, so we
2556 * just need to walk through and tag the targets.
2557 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002558 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002559 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2560 const uint16_t* switch_insns = insns + offset_to_switch;
2561 int switch_count = switch_insns[1];
2562 int offset_to_targets, targ;
2563
2564 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2565 /* 0 = sig, 1 = count, 2/3 = first key */
2566 offset_to_targets = 4;
2567 } else {
2568 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002569 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002570 offset_to_targets = 2 + 2 * switch_count;
2571 }
2572
2573 /* verify each switch target */
2574 for (targ = 0; targ < switch_count; targ++) {
2575 int offset;
2576 uint32_t abs_offset;
2577
2578 /* offsets are 32-bit, and only partly endian-swapped */
2579 offset = switch_insns[offset_to_targets + targ * 2] |
2580 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 abs_offset = work_insn_idx_ + offset;
2582 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002583 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002584 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 }
2586 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002587 return false;
2588 }
2589 }
2590
2591 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2593 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002594 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002595 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002597 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002598
Ian Rogers0571d352011-11-03 19:51:38 -07002599 for (; iterator.HasNext(); iterator.Next()) {
2600 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 within_catch_all = true;
2602 }
jeffhaobdb76512011-09-07 11:43:16 -07002603 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2605 * "work_regs", because at runtime the exception will be thrown before the instruction
2606 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002607 */
Ian Rogers0571d352011-11-03 19:51:38 -07002608 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002609 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 }
jeffhaobdb76512011-09-07 11:43:16 -07002611 }
2612
2613 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2615 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002616 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002618 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 * The state in work_line reflects the post-execution state. If the current instruction is a
2620 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002621 * it will do so before grabbing the lock).
2622 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002623 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002624 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002626 return false;
2627 }
2628 }
2629 }
2630
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002631 /* Handle "continue". Tag the next consecutive instruction.
2632 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2633 * because it changes work_line_ when performing peephole optimization
2634 * and this change should not be used in those cases.
2635 */
2636 if ((opcode_flags & Instruction::kContinue) != 0) {
2637 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2638 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2640 return false;
2641 }
2642 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2643 // next instruction isn't one.
2644 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2645 return false;
2646 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07002647 if (NULL != fallthrough_line.get()) {
2648 // Make workline consistent with fallthrough computed from peephole optimization.
2649 work_line_->CopyFromLine(fallthrough_line.get());
2650 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002651 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2652 if (next_line != NULL) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002653 // Merge registers into what we have for the next instruction,
2654 // and set the "changed" flag if needed.
2655 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2656 return false;
2657 }
2658 } else {
2659 /*
2660 * We're not recording register data for the next instruction, so we don't know what the
2661 * prior state was. We have to assume that something has changed and re-evaluate it.
2662 */
2663 insn_flags_[next_insn_idx].SetChanged();
2664 }
2665 }
2666
jeffhaod1f0fde2011-09-08 17:25:33 -07002667 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002669 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 return false;
2671 }
jeffhaobdb76512011-09-07 11:43:16 -07002672 }
2673
2674 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002675 * Update start_guess. Advance to the next instruction of that's
2676 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002677 * neither of those exists we're in a return or throw; leave start_guess
2678 * alone and let the caller sort it out.
2679 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002680 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002682 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002683 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002685 }
2686
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2688 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002689
2690 return true;
2691}
2692
Ian Rogers776ac1f2012-04-13 23:36:36 -07002693const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002694 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002695 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002696 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002697 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002698 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2699 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002700 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002701 if (result.IsConflict()) {
2702 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2703 << "' in " << referrer;
2704 return result;
2705 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002706 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002707 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002708 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002709 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002710 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002711 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002712 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002713 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002714 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002715 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002716}
2717
Ian Rogers776ac1f2012-04-13 23:36:36 -07002718const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002719 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002721 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2723 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002724 CatchHandlerIterator iterator(handlers_ptr);
2725 for (; iterator.HasNext(); iterator.Next()) {
2726 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2727 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002728 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002730 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002731 if (common_super == NULL) {
2732 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2733 // as that is caught at runtime
2734 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002735 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002736 // We don't know enough about the type and the common path merge will result in
2737 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002739 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002740 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002741 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002743 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002744 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
2746 }
2747 }
2748 }
Ian Rogers0571d352011-11-03 19:51:38 -07002749 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 }
2751 }
2752 if (common_super == NULL) {
2753 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002754 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002755 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002757 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002758}
2759
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002760mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2761 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002762 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002763 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002764 if (klass_type.IsConflict()) {
2765 std::string append(" in attempt to access method ");
2766 append += dex_file_->GetMethodName(method_id);
2767 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002768 return NULL;
2769 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002770 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002771 return NULL; // Can't resolve Class so no more to do here
2772 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002773 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002774 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002775 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002777 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002778 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002779
2780 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002782 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 res_method = klass->FindInterfaceMethod(name, signature);
2784 } else {
2785 res_method = klass->FindVirtualMethod(name, signature);
2786 }
2787 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002788 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002790 // If a virtual or interface method wasn't found with the expected type, look in
2791 // the direct methods. This can happen when the wrong invoke type is used or when
2792 // a class has changed, and will be flagged as an error in later checks.
2793 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2794 res_method = klass->FindDirectMethod(name, signature);
2795 }
2796 if (res_method == NULL) {
2797 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2798 << PrettyDescriptor(klass) << "." << name
2799 << " " << signature;
2800 return NULL;
2801 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 }
2803 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2805 // enforce them here.
2806 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2808 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 return NULL;
2810 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002811 // Disallow any calls to class initializers.
2812 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2814 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002815 return NULL;
2816 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002817 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002818 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002819 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002820 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002821 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002822 }
jeffhaode0d9c92012-02-27 13:58:13 -08002823 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2824 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2826 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002827 return NULL;
2828 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002829 // Check that interface methods match interface classes.
2830 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2831 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2832 << " is in an interface class " << PrettyClass(klass);
2833 return NULL;
2834 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2835 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2836 << " is in a non-interface class " << PrettyClass(klass);
2837 return NULL;
2838 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 // See if the method type implied by the invoke instruction matches the access flags for the
2840 // target method.
2841 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2842 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2843 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2844 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002845 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2846 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 return NULL;
2848 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002849 return res_method;
2850}
2851
Sebastien Hertz5243e912013-05-21 10:55:07 +02002852mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2853 MethodType method_type,
2854 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002855 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002856 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2857 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002858 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2859 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002860 if (res_method == NULL) { // error or class is unresolved
2861 return NULL;
2862 }
2863
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2865 // has a vtable entry for the target method.
2866 if (is_super) {
2867 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002869 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002870 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002871 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002872 << " to super " << PrettyMethod(res_method);
2873 return NULL;
2874 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002875 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002876 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002877 MethodHelper mh(res_method);
2878 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002879 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002880 << " to super " << super
2881 << "." << mh.GetName()
2882 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 return NULL;
2884 }
2885 }
2886 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002887 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002889 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 /* caught by static verifier */
2891 DCHECK(is_range || expected_args <= 5);
2892 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2895 return NULL;
2896 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002897
jeffhaobdb76512011-09-07 11:43:16 -07002898 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 * Check the "this" argument, which must be an instance of the class that declared the method.
2900 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2901 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002902 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002903 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002905 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 return NULL;
2908 }
2909 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 return NULL;
2912 }
2913 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002914 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002915 const RegType& res_method_class =
2916 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2917 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002918 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002919 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002920 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 return NULL;
2922 }
2923 }
2924 actual_args++;
2925 }
2926 /*
2927 * Process the target method's signature. This signature may or may not
2928 * have been verified, so we can't assume it's properly formed.
2929 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002930 MethodHelper mh(res_method);
2931 const DexFile::TypeList* params = mh.GetParameterTypeList();
2932 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002933 uint32_t arg[5];
2934 if (!is_range) {
2935 inst->GetArgs(arg);
2936 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002937 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002940 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2941 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 return NULL;
2943 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002944 const char* descriptor =
2945 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2946 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002948 << " missing signature component";
2949 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 }
Ian Rogersb4903572012-10-11 11:52:56 -07002951 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002952 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002953 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002954 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 }
2956 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2957 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002960 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 return NULL;
2962 } else {
2963 return res_method;
2964 }
2965}
2966
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002967mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
2968 bool is_range) {
2969 DCHECK(Runtime::Current()->IsStarted());
2970 CHECK(invoked_method != NULL || accessed_field != NULL)
2971 << "We should not be verifying " << inst->Name();
2972 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
2973 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
2974 return NULL;
2975 }
2976 mirror::Class* this_class = NULL;
2977 if (!actual_arg_type.IsUnresolvedTypes()) {
2978 this_class = actual_arg_type.GetClass();
2979 } else {
2980 const std::string& descriptor(actual_arg_type.GetDescriptor());
2981 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2982 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
2983 if (this_class == NULL) {
2984 Thread::Current()->ClearException();
2985 // Look for a system class
2986 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
2987 }
2988 }
2989 CHECK(this_class != NULL) << "Cannot get Class* for type " << actual_arg_type;
2990 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
2991 CHECK(vtable != NULL);
2992 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
2993 CHECK(vtable_index < vtable->GetLength());
2994 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
2995 CHECK(!Thread::Current()->IsExceptionPending());
2996 // TODO: we should move the code below to FindInvokedMethodAtDexPc. Once the
2997 // method is verified, we could access the information we need from register
2998 // lines for the dex pc we are looking for.
2999 if (invoked_method != NULL && work_insn_idx_ == interesting_dex_pc_) {
3000 // We've been requested to tell which method is invoked at this dex pc.
3001 *invoked_method = res_method;
3002 }
3003 if (res_method == NULL) {
3004 return NULL;
3005 }
3006 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3007
3008 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3009 // match the call to the signature. Also, we might be calling through an abstract method
3010 // definition (which doesn't have register count values).
3011 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3012 /* caught by static verifier */
3013 DCHECK(is_range || expected_args <= 5);
3014 if (expected_args > code_item_->outs_size_) {
3015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3016 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3017 return NULL;
3018 }
3019
3020 /*
3021 * Check the "this" argument, which must be an instance of the class that declared the method.
3022 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3023 * rigorous check here (which is okay since we have to do it at runtime).
3024 */
3025 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3027 return NULL;
3028 }
3029 if (!actual_arg_type.IsZero()) {
3030 mirror::Class* klass = res_method->GetDeclaringClass();
3031 const RegType& res_method_class =
3032 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3033 klass->CannotBeAssignedFromOtherTypes());
3034 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3035 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3036 << "' not instance of '" << res_method_class << "'";
3037 return NULL;
3038 }
3039 }
3040 /*
3041 * Process the target method's signature. This signature may or may not
3042 * have been verified, so we can't assume it's properly formed.
3043 */
3044 MethodHelper mh(res_method);
3045 const DexFile::TypeList* params = mh.GetParameterTypeList();
3046 size_t params_size = params == NULL ? 0 : params->Size();
3047 uint32_t arg[5];
3048 if (!is_range) {
3049 inst->GetArgs(arg);
3050 }
3051 size_t actual_args = 1;
3052 for (size_t param_index = 0; param_index < params_size; param_index++) {
3053 if (actual_args >= expected_args) {
3054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3055 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3056 << " (where longs/doubles count twice).";
3057 return NULL;
3058 }
3059 const char* descriptor =
3060 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3061 if (descriptor == NULL) {
3062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3063 << " missing signature component";
3064 return NULL;
3065 }
3066 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3067 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3068 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3069 return res_method;
3070 }
3071 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3072 }
3073 if (actual_args != expected_args) {
3074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3075 << " expected " << expected_args << " arguments, found " << actual_args;
3076 return NULL;
3077 } else {
3078 return res_method;
3079 }
3080}
3081
Ian Rogers62342ec2013-06-11 10:26:37 -07003082void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003083 uint32_t type_idx;
3084 if (!is_filled) {
3085 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3086 type_idx = inst->VRegC_22c();
3087 } else if (!is_range) {
3088 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3089 type_idx = inst->VRegB_35c();
3090 } else {
3091 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3092 type_idx = inst->VRegB_3rc();
3093 }
3094 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003095 if (res_type.IsConflict()) { // bad class
3096 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003097 } else {
3098 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3099 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003101 } else if (!is_filled) {
3102 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003103 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003104 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003105 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3106 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003107 } else {
3108 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3109 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003110 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003111 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3112 uint32_t arg[5];
3113 if (!is_range) {
3114 inst->GetArgs(arg);
3115 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003116 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003117 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003118 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003120 return;
3121 }
3122 }
3123 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003124 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3125 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003126 }
3127 }
3128}
3129
Sebastien Hertz5243e912013-05-21 10:55:07 +02003130void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003131 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003132 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003136 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003137 if (array_type.IsZero()) {
3138 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3139 // instruction type. TODO: have a proper notion of bottom here.
3140 if (!is_primitive || insn_type.IsCategory1Types()) {
3141 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003142 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003144 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003145 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003146 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003147 }
jeffhaofc3144e2012-02-01 17:21:15 -08003148 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003150 } else {
3151 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003152 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003153 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003155 << " source for aget-object";
3156 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003158 << " source for category 1 aget";
3159 } else if (is_primitive && !insn_type.Equals(component_type) &&
3160 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003161 (insn_type.IsLong() && component_type.IsDouble()))) {
3162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3163 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003164 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003165 // Use knowledge of the field type which is stronger than the type inferred from the
3166 // instruction, which can't differentiate object types and ints from floats, longs from
3167 // doubles.
3168 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003169 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003170 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003171 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003172 component_type.HighHalf(&reg_types_));
3173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 }
3175 }
3176 }
3177}
3178
Sebastien Hertz5243e912013-05-21 10:55:07 +02003179void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003181 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003185 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003186 if (array_type.IsZero()) {
3187 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3188 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003189 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003191 } else {
3192 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003193 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003194 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003196 << " source for aput-object";
3197 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003199 << " source for category 1 aput";
3200 } else if (is_primitive && !insn_type.Equals(component_type) &&
3201 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3202 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003204 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003206 // The instruction agrees with the type of array, confirm the value to be stored does too
3207 // Note: we use the instruction type (rather than the component type) for aput-object as
3208 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003209 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003210 }
3211 }
3212 }
3213}
3214
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003215mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003216 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3217 // Check access to class
3218 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003219 if (klass_type.IsConflict()) { // bad class
3220 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3221 field_idx, dex_file_->GetFieldName(field_id),
3222 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003223 return NULL;
3224 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003225 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003226 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003227 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003228 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003229 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003231 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003232 << dex_file_->GetFieldName(field_id) << ") in "
3233 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003234 DCHECK(Thread::Current()->IsExceptionPending());
3235 Thread::Current()->ClearException();
3236 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003237 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3238 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003240 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 return NULL;
3242 } else if (!field->IsStatic()) {
3243 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3244 return NULL;
3245 } else {
3246 return field;
3247 }
3248}
3249
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003250mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003251 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3252 // Check access to class
3253 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003254 if (klass_type.IsConflict()) {
3255 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3256 field_idx, dex_file_->GetFieldName(field_id),
3257 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003258 return NULL;
3259 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003260 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003261 return NULL; // Can't resolve Class so no more to do here
3262 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003263 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003264 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003266 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003267 << dex_file_->GetFieldName(field_id) << ") in "
3268 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 DCHECK(Thread::Current()->IsExceptionPending());
3270 Thread::Current()->ClearException();
3271 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003272 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3273 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 return NULL;
3277 } else if (field->IsStatic()) {
3278 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3279 << " to not be static";
3280 return NULL;
3281 } else if (obj_type.IsZero()) {
3282 // Cannot infer and check type, however, access will cause null pointer exception
3283 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003284 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003285 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003286 const RegType& field_klass =
3287 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003288 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003289 if (obj_type.IsUninitializedTypes() &&
3290 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3291 !field_klass.Equals(GetDeclaringClass()))) {
3292 // Field accesses through uninitialized references are only allowable for constructors where
3293 // the field is declared in this class
3294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3295 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003296 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003297 return NULL;
3298 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3299 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3300 // of C1. For resolution to occur the declared class of the field must be compatible with
3301 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3302 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3303 << " from object of type " << obj_type;
3304 return NULL;
3305 } else {
3306 return field;
3307 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 }
3309}
3310
Sebastien Hertz5243e912013-05-21 10:55:07 +02003311void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3312 bool is_primitive, bool is_static) {
3313 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003314 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003315 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003316 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003317 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003318 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003319 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003320 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003321 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003322 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003323 if (field != NULL) {
3324 descriptor = FieldHelper(field).GetTypeDescriptor();
3325 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003326 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003327 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3328 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3329 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003330 }
Ian Rogersb4903572012-10-11 11:52:56 -07003331 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003332 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003333 if (is_primitive) {
3334 if (field_type.Equals(insn_type) ||
3335 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3336 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3337 // expected that read is of the correct primitive type or that int reads are reading
3338 // floats or long reads are reading doubles
3339 } else {
3340 // This is a global failure rather than a class change failure as the instructions and
3341 // the descriptors for the type should have been consistent within the same file at
3342 // compile time
3343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3344 << " to be of type '" << insn_type
3345 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003346 return;
3347 }
3348 } else {
3349 if (!insn_type.IsAssignableFrom(field_type)) {
3350 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3351 << " to be compatible with type '" << insn_type
3352 << "' but found type '" << field_type
3353 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003354 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003355 return;
3356 }
3357 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003358 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003359 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003360 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003361 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003363}
3364
Sebastien Hertz5243e912013-05-21 10:55:07 +02003365void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3366 bool is_primitive, bool is_static) {
3367 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003368 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003369 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003370 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003371 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003372 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003373 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003375 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003376 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003377 if (field != NULL) {
3378 descriptor = FieldHelper(field).GetTypeDescriptor();
3379 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003380 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003381 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3382 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3383 loader = class_loader_;
3384 }
Ian Rogersb4903572012-10-11 11:52:56 -07003385 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003386 if (field != NULL) {
3387 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3388 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3389 << " from other class " << GetDeclaringClass();
3390 return;
3391 }
3392 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003393 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003394 if (is_primitive) {
3395 // Primitive field assignability rules are weaker than regular assignability rules
3396 bool instruction_compatible;
3397 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003398 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003399 if (field_type.IsIntegralTypes()) {
3400 instruction_compatible = insn_type.IsIntegralTypes();
3401 value_compatible = value_type.IsIntegralTypes();
3402 } else if (field_type.IsFloat()) {
3403 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3404 value_compatible = value_type.IsFloatTypes();
3405 } else if (field_type.IsLong()) {
3406 instruction_compatible = insn_type.IsLong();
3407 value_compatible = value_type.IsLongTypes();
3408 } else if (field_type.IsDouble()) {
3409 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3410 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003411 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003412 instruction_compatible = false; // reference field with primitive store
3413 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 if (!instruction_compatible) {
3416 // This is a global failure rather than a class change failure as the instructions and
3417 // the descriptors for the type should have been consistent within the same file at
3418 // compile time
3419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3420 << " to be of type '" << insn_type
3421 << "' but found type '" << field_type
3422 << "' in put";
3423 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003425 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003427 << " of type " << value_type
3428 << " but expected " << field_type
3429 << " for store to " << PrettyField(field) << " in put";
3430 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003432 } else {
3433 if (!insn_type.IsAssignableFrom(field_type)) {
3434 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3435 << " to be compatible with type '" << insn_type
3436 << "' but found type '" << field_type
3437 << "' in put-object";
3438 return;
3439 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003440 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 }
3442}
3443
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003444// Look for an instance field with this offset.
3445// TODO: we may speed up the search if offsets are sorted by doing a quick search.
3446static mirror::Field* FindInstanceFieldWithOffset(mirror::Class* klass,
3447 uint32_t field_offset)
3448 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3449 mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
3450 if (instance_fields != NULL) {
3451 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3452 mirror::Field* field = instance_fields->Get(i);
3453 if (field->GetOffset().Uint32Value() == field_offset) {
3454 return field;
3455 }
3456 }
3457 }
3458 if (klass->GetSuperClass() != NULL) {
3459 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3460 } else {
3461 return NULL;
3462 }
3463}
3464
3465void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3466 bool is_primitive) {
3467 DCHECK(Runtime::Current()->IsStarted());
3468 CHECK(accessed_field != NULL || invoked_method != NULL)
3469 << "We should not be verifying " << inst->Name();
3470 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
3471 mirror::Class* object_class = NULL;
3472 if (!object_type.IsUnresolvedTypes()) {
3473 object_class = object_type.GetClass();
3474 } else {
3475 // We need to resolve the class from its descriptor.
3476 const std::string& descriptor(object_type.GetDescriptor());
3477 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3478 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3479 if (object_class == NULL) {
3480 Thread::Current()->ClearException();
3481 // Look for a system class
3482 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3483 }
3484 }
3485 CHECK(object_class != NULL) << "Cannot get Class* for type " << object_type;
3486 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
3487 mirror::Field* field = FindInstanceFieldWithOffset(object_class, field_offset);
3488 CHECK(field != NULL);
3489 // TODO: we should move the code below to FindAccessedFieldAtDexPc. Once the
3490 // method is verified, we could access the information we need from register
3491 // lines for the dex pc we are looking for.
3492 if (accessed_field != NULL && work_insn_idx_ == interesting_dex_pc_) {
3493 // We've been requested to tell which field is accessed at this dex pc.
3494 *accessed_field = field;
3495 }
3496 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3497 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3498 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3499 const uint32_t vregA = inst->VRegA_22c();
3500 if (is_primitive) {
3501 if (field_type.Equals(insn_type) ||
3502 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3503 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3504 // expected that read is of the correct primitive type or that int reads are reading
3505 // floats or long reads are reading doubles
3506 } else {
3507 // This is a global failure rather than a class change failure as the instructions and
3508 // the descriptors for the type should have been consistent within the same file at
3509 // compile time
3510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3511 << " to be of type '" << insn_type
3512 << "' but found type '" << field_type << "' in get";
3513 return;
3514 }
3515 } else {
3516 if (!insn_type.IsAssignableFrom(field_type)) {
3517 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3518 << " to be compatible with type '" << insn_type
3519 << "' but found type '" << field_type
3520 << "' in get-object";
3521 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3522 return;
3523 }
3524 }
3525 if (!field_type.IsLowHalf()) {
3526 work_line_->SetRegisterType(vregA, field_type);
3527 } else {
3528 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3529 }
3530}
3531
3532void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3533 bool is_primitive) {
3534 DCHECK(Runtime::Current()->IsStarted());
3535 CHECK(accessed_field != NULL || invoked_method != NULL)
3536 << "We should not be verifying " << inst->Name();
3537 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
3538 mirror::Class* object_class = NULL;
3539 if (!object_type.IsUnresolvedTypes()) {
3540 object_class = object_type.GetClass();
3541 } else {
3542 // We need to resolve the class from its descriptor.
3543 const std::string& descriptor(object_type.GetDescriptor());
3544 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3545 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3546 if (object_class == NULL) {
3547 Thread::Current()->ClearException();
3548 // Look for a system class
3549 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3550 }
3551 }
3552 CHECK(object_class != NULL) << "Cannot get Class* for type " << object_type;
3553 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
3554 mirror::Field* field = FindInstanceFieldWithOffset(object_class, field_offset);
3555 CHECK(field != NULL);
3556 // TODO: like VerifyIGetQuick, we should move the code below to
3557 // FindAccessedFieldAtDexPc.
3558 if (accessed_field != NULL && work_insn_idx_ == interesting_dex_pc_) {
3559 // We've been requested to tell which field is accessed at this dex pc.
3560 *accessed_field = field;
3561 }
3562 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3563 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3564 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3565 if (field != NULL) {
3566 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3567 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3568 << " from other class " << GetDeclaringClass();
3569 return;
3570 }
3571 }
3572 const uint32_t vregA = inst->VRegA_22c();
3573 if (is_primitive) {
3574 // Primitive field assignability rules are weaker than regular assignability rules
3575 bool instruction_compatible;
3576 bool value_compatible;
3577 const RegType& value_type = work_line_->GetRegisterType(vregA);
3578 if (field_type.IsIntegralTypes()) {
3579 instruction_compatible = insn_type.IsIntegralTypes();
3580 value_compatible = value_type.IsIntegralTypes();
3581 } else if (field_type.IsFloat()) {
3582 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3583 value_compatible = value_type.IsFloatTypes();
3584 } else if (field_type.IsLong()) {
3585 instruction_compatible = insn_type.IsLong();
3586 value_compatible = value_type.IsLongTypes();
3587 } else if (field_type.IsDouble()) {
3588 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3589 value_compatible = value_type.IsDoubleTypes();
3590 } else {
3591 instruction_compatible = false; // reference field with primitive store
3592 value_compatible = false; // unused
3593 }
3594 if (!instruction_compatible) {
3595 // This is a global failure rather than a class change failure as the instructions and
3596 // the descriptors for the type should have been consistent within the same file at
3597 // compile time
3598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3599 << " to be of type '" << insn_type
3600 << "' but found type '" << field_type
3601 << "' in put";
3602 return;
3603 }
3604 if (!value_compatible) {
3605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3606 << " of type " << value_type
3607 << " but expected " << field_type
3608 << " for store to " << PrettyField(field) << " in put";
3609 return;
3610 }
3611 } else {
3612 if (!insn_type.IsAssignableFrom(field_type)) {
3613 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3614 << " to be compatible with type '" << insn_type
3615 << "' but found type '" << field_type
3616 << "' in put-object";
3617 return;
3618 }
3619 work_line_->VerifyRegisterType(vregA, field_type);
3620 }
3621}
3622
Ian Rogers776ac1f2012-04-13 23:36:36 -07003623bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003624 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 return false;
3627 }
3628 return true;
3629}
3630
Ian Rogers776ac1f2012-04-13 23:36:36 -07003631bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003632 bool changed = true;
3633 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3634 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003635 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 * We haven't processed this instruction before, and we haven't touched the registers here, so
3637 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3638 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003639 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003641 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003642 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3643 if (gDebugVerify) {
3644 copy->CopyFromLine(target_line);
3645 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003647 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003648 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003649 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003650 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003651 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003652 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3653 << *copy.get() << " MERGE\n"
3654 << *merge_line << " ==\n"
3655 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003656 }
3657 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 if (changed) {
3659 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003660 }
3661 return true;
3662}
3663
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003664InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003665 return &insn_flags_[work_insn_idx_];
3666}
3667
Ian Rogersad0b3a32012-04-16 14:50:24 -07003668const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003669 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003670 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3671 uint16_t return_type_idx = proto_id.return_type_idx_;
3672 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003673 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003674}
3675
3676const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003677 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003678 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003679 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003680 if (mirror_method_ != NULL) {
3681 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003682 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3683 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003684 } else {
3685 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3686 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003687 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003688 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003689}
3690
Ian Rogers776ac1f2012-04-13 23:36:36 -07003691void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 size_t* log2_max_gc_pc) {
3693 size_t local_gc_points = 0;
3694 size_t max_insn = 0;
3695 size_t max_ref_reg = -1;
3696 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003697 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003698 local_gc_points++;
3699 max_insn = i;
3700 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003701 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003702 }
3703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 *gc_points = local_gc_points;
3705 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3706 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003707 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003708 i++;
3709 }
3710 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003711}
3712
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003713MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3714 /*
3715 * Walks over the method code and adds any cast instructions in which
3716 * the type cast is implicit to a set, which is used in the code generation
3717 * to elide these casts.
3718 */
3719 if (!failure_messages_.empty()) {
3720 return NULL;
3721 }
3722 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003723 const Instruction* inst = Instruction::At(code_item_->insns_);
3724 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003725 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003726
3727 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003728 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003729 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003730 }
3731 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003732 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003733 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3734 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003735 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003736 if (mscs.get() == NULL) {
3737 mscs.reset(new MethodSafeCastSet());
3738 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003739 mscs->insert(dex_pc);
3740 }
3741 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003742 return mscs.release();
3743}
3744
Ian Rogersd0583802013-06-01 10:51:46 -07003745MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003746
3747 // It is risky to rely on reg_types for sharpening in cases of soft
3748 // verification, we might end up sharpening to a wrong implementation. Just abort.
3749 if (!failure_messages_.empty()) {
3750 return NULL;
3751 }
3752
Ian Rogersd0583802013-06-01 10:51:46 -07003753 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003754 const uint16_t* insns = code_item_->insns_ ;
3755 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003756 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003757
Ian Rogersd0583802013-06-01 10:51:46 -07003758 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003759 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3760 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3761 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3762 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3763
Ian Rogersd0583802013-06-01 10:51:46 -07003764 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003765 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003766 }
Ian Rogersd0583802013-06-01 10:51:46 -07003767 // Get reg type for register holding the reference to the object that will be dispatched upon.
3768 uint32_t dex_pc = inst->GetDexPc(insns);
3769 RegisterLine* line = reg_table_.GetLine(dex_pc);
3770 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3771 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3772 const RegType&
3773 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003774
Ian Rogersd0583802013-06-01 10:51:46 -07003775 if (!reg_type.HasClass()) {
3776 // We will compute devirtualization information only when we know the Class of the reg type.
3777 continue;
3778 }
3779 mirror::Class* reg_class = reg_type.GetClass();
3780 if (reg_class->IsInterface()) {
3781 // We can't devirtualize when the known type of the register is an interface.
3782 continue;
3783 }
3784 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3785 // We can't devirtualize abstract classes except on arrays of abstract classes.
3786 continue;
3787 }
3788 mirror::AbstractMethod* abstract_method =
3789 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003790 if(abstract_method == NULL) {
3791 // If the method is not found in the cache this means that it was never found
3792 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3793 continue;
3794 }
3795 // Find the concrete method.
3796 mirror::AbstractMethod* concrete_method = NULL;
3797 if (is_interface) {
3798 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3799 }
3800 if (is_virtual) {
3801 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3802 }
Ian Rogersd0583802013-06-01 10:51:46 -07003803 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3804 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003805 continue;
3806 }
Ian Rogersd0583802013-06-01 10:51:46 -07003807 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3808 concrete_method->GetDeclaringClass()->IsFinal()) {
3809 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3810 // overridden record the target to be used in the compiler driver.
3811 if (pc_to_concrete_method_map.get() == NULL) {
3812 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3813 }
3814 CompilerDriver::MethodReference concrete_ref(
3815 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3816 concrete_method->GetDexMethodIndex());
3817 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3818 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003819 }
Ian Rogersd0583802013-06-01 10:51:46 -07003820 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003821}
3822
Ian Rogers776ac1f2012-04-13 23:36:36 -07003823const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 size_t num_entries, ref_bitmap_bits, pc_bits;
3825 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3826 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003827 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003831 return NULL;
3832 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3834 // There are 2 bytes to encode the number of entries
3835 if (num_entries >= 65536) {
3836 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003839 return NULL;
3840 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003842 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003843 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003844 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003846 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003847 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003849 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3853 return NULL;
3854 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003855 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003856 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003859 return NULL;
3860 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003861 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003863 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3864 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003865 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003866 table->push_back(num_entries & 0xFF);
3867 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003870 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003871 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003873 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 }
3875 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003876 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 }
3878 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003879 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 return table;
3881}
jeffhaoa0a764a2011-09-16 10:43:38 -07003882
Ian Rogers776ac1f2012-04-13 23:36:36 -07003883void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003884 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3885 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003886 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003888 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003890 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003892 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003893 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3894 map_index++;
3895 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003896 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003897 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 CHECK_LT(j / 8, map.RegWidth());
3899 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3900 } else if ((j / 8) < map.RegWidth()) {
3901 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3902 } else {
3903 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3904 }
3905 }
3906 } else {
3907 CHECK(reg_bitmap == NULL);
3908 }
3909 }
3910}
jeffhaoa0a764a2011-09-16 10:43:38 -07003911
Ian Rogers637c65b2013-05-31 11:46:00 -07003912void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3913 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003914 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003915 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003916 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3917 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003918 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003919 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003920 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003921 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003922 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003923 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003924}
3925
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003926
Ian Rogersfae370a2013-06-05 08:33:27 -07003927void MethodVerifier::SetSafeCastMap(CompilerDriver::MethodReference ref,
3928 const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003929 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3930 SafeCastMap::iterator it = safecast_map_->find(ref);
3931 if (it != safecast_map_->end()) {
3932 delete it->second;
3933 safecast_map_->erase(it);
3934 }
3935
3936 safecast_map_->Put(ref, cast_set);
3937 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3938}
3939
3940bool MethodVerifier::IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) {
3941 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3942 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3943 if (it == safecast_map_->end()) {
3944 return false;
3945 }
3946
3947 // Look up the cast address in the set of safe casts
3948 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3949 return cast_it != it->second->end();
3950}
3951
Ian Rogers637c65b2013-05-31 11:46:00 -07003952const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3953 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3954 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3955 if (it == dex_gc_maps_->end()) {
3956 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3957 return NULL;
3958 }
3959 CHECK(it->second != NULL);
3960 return it->second;
3961}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003962
Ian Rogers637c65b2013-05-31 11:46:00 -07003963void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003964 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003965 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003966 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3967 if (it != devirt_maps_->end()) {
3968 delete it->second;
3969 devirt_maps_->erase(it);
3970 }
3971
3972 devirt_maps_->Put(ref, devirt_map);
3973 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3974}
3975
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003976const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3977 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003978 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003979 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3980 if (it == devirt_maps_->end()) {
3981 return NULL;
3982 }
3983
3984 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003985 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003986 if(pc_to_concrete_method != it->second->end()) {
3987 return &(pc_to_concrete_method->second);
3988 } else {
3989 return NULL;
3990 }
3991}
3992
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003993std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3994 RegisterLine* line = reg_table_.GetLine(dex_pc);
3995 std::vector<int32_t> result;
3996 for (size_t i = 0; i < line->NumRegs(); ++i) {
3997 const RegType& type = line->GetRegisterType(i);
3998 if (type.IsConstant()) {
3999 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4000 result.push_back(type.ConstantValue());
4001 } else if (type.IsConstantLo()) {
4002 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4003 result.push_back(type.ConstantValueLo());
4004 } else if (type.IsConstantHi()) {
4005 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4006 result.push_back(type.ConstantValueHi());
4007 } else if (type.IsIntegralTypes()) {
4008 result.push_back(kIntVReg);
4009 result.push_back(0);
4010 } else if (type.IsFloat()) {
4011 result.push_back(kFloatVReg);
4012 result.push_back(0);
4013 } else if (type.IsLong()) {
4014 result.push_back(kLongLoVReg);
4015 result.push_back(0);
4016 result.push_back(kLongHiVReg);
4017 result.push_back(0);
4018 ++i;
4019 } else if (type.IsDouble()) {
4020 result.push_back(kDoubleLoVReg);
4021 result.push_back(0);
4022 result.push_back(kDoubleHiVReg);
4023 result.push_back(0);
4024 ++i;
4025 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4026 result.push_back(kUndefined);
4027 result.push_back(0);
4028 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004029 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004030 result.push_back(kReferenceVReg);
4031 result.push_back(0);
4032 }
4033 }
4034 return result;
4035}
4036
Ian Rogers637c65b2013-05-31 11:46:00 -07004037ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004038MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004039
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004040Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4041MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4042
Ian Rogers637c65b2013-05-31 11:46:00 -07004043ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004044MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4045
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004046Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4047MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4048
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004049void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004050 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07004051 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004052 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004053 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004054 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004055 }
4056
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004057 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4058 {
4059 MutexLock mu(self, *safecast_map_lock_);
4060 safecast_map_ = new MethodVerifier::SafeCastMap();
4061 }
4062
Ian Rogers637c65b2013-05-31 11:46:00 -07004063 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004064
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004065 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004066 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004067 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4068 }
4069
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004070 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4071 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004072 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004073 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4074 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004075 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004076}
4077
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004078void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07004079 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004080 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004081 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004082 STLDeleteValues(dex_gc_maps_);
4083 delete dex_gc_maps_;
4084 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004085 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004086 delete dex_gc_maps_lock_;
4087 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004088
4089 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004090 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004091 STLDeleteValues(devirt_maps_);
4092 delete devirt_maps_;
4093 devirt_maps_ = NULL;
4094 }
4095 delete devirt_maps_lock_;
4096 devirt_maps_lock_ = NULL;
4097
4098 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004099 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004100 delete rejected_classes_;
4101 rejected_classes_ = NULL;
4102 }
4103 delete rejected_classes_lock_;
4104 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004105 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004106}
jeffhaod1224c72012-02-29 13:43:08 -08004107
Ian Rogers1212a022013-03-04 10:48:41 -08004108void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004109 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004110 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004111 rejected_classes_->insert(ref);
4112 }
jeffhaod1224c72012-02-29 13:43:08 -08004113 CHECK(IsClassRejected(ref));
4114}
4115
Ian Rogers1212a022013-03-04 10:48:41 -08004116bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07004117 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004118 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004119}
4120
Ian Rogersd81871c2011-10-03 13:57:23 -07004121} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004122} // namespace art