blob: 74a79e0ae2f4274fac4e1afd509d91958fd80de6 [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),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700281 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700282 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800283 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800284 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700285 can_load_classes_(can_load_classes),
286 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800290 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700291 MethodHelper mh(m);
292 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
293 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700294 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700295 verifier.interesting_dex_pc_ = dex_pc;
296 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
297 verifier.FindLocksAtDexPc();
298}
299
300void MethodVerifier::FindLocksAtDexPc() {
301 CHECK(monitor_enter_dex_pcs_ != NULL);
302 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
303
304 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
305 // verification. In practice, the phase we want relies on data structures set up by all the
306 // earlier passes, so we just run the full method verification and bail out early when we've
307 // got what we wanted.
308 Verify();
309}
310
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200311mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
312 uint32_t dex_pc) {
313 MethodHelper mh(m);
314 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
315 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
316 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200317 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200318}
319
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200320mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200321 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
322
323 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
324 // verification. In practice, the phase we want relies on data structures set up by all the
325 // earlier passes, so we just run the full method verification and bail out early when we've
326 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200327 bool success = Verify();
328 if (!success) {
329 return NULL;
330 }
331 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
332 if (register_line == NULL) {
333 return NULL;
334 }
335 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
336 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200337}
338
339mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
340 uint32_t dex_pc) {
341 MethodHelper mh(m);
342 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
343 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
344 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200345 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200346}
347
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200348mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200349 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
350
351 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
352 // verification. In practice, the phase we want relies on data structures set up by all the
353 // earlier passes, so we just run the full method verification and bail out early when we've
354 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200355 bool success = Verify();
356 if (!success) {
357 return NULL;
358 }
359 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
360 if (register_line == NULL) {
361 return NULL;
362 }
363 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
364 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
365 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366}
367
Ian Rogersad0b3a32012-04-16 14:50:24 -0700368bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 // If there aren't any instructions, make sure that's expected, then exit successfully.
370 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700371 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700373 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700374 } else {
375 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700376 }
jeffhaobdb76512011-09-07 11:43:16 -0700377 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700378 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
379 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
381 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700382 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700383 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700384 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800385 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700386 // Run through the instructions and see if the width checks out.
387 bool result = ComputeWidthsAndCountOps();
388 // Flag instructions guarded by a "try" block and check exception handlers.
389 result = result && ScanTryCatchBlocks();
390 // Perform static instruction verification.
391 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392 // Perform code-flow analysis and return.
393 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700394}
395
Ian Rogers776ac1f2012-04-13 23:36:36 -0700396std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 switch (error) {
398 case VERIFY_ERROR_NO_CLASS:
399 case VERIFY_ERROR_NO_FIELD:
400 case VERIFY_ERROR_NO_METHOD:
401 case VERIFY_ERROR_ACCESS_CLASS:
402 case VERIFY_ERROR_ACCESS_FIELD:
403 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700404 case VERIFY_ERROR_INSTANTIATION:
405 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800406 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700407 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
408 // class change and instantiation errors into soft verification errors so that we re-verify
409 // at runtime. We may fail to find or to agree on access because of not yet available class
410 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
411 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
412 // paths" that dynamically perform the verification and cause the behavior to be that akin
413 // to an interpreter.
414 error = VERIFY_ERROR_BAD_CLASS_SOFT;
415 } else {
416 have_pending_runtime_throw_failure_ = true;
417 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700418 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419 // Indication that verification should be retried at runtime.
420 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700421 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700422 have_pending_hard_failure_ = true;
423 }
424 break;
jeffhaod5347e02012-03-22 17:25:05 -0700425 // Hard verification failures at compile time will still fail at runtime, so the class is
426 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427 case VERIFY_ERROR_BAD_CLASS_HARD: {
428 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800429 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800430 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700432 have_pending_hard_failure_ = true;
433 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800434 }
435 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700436 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800437 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438 work_insn_idx_));
439 std::ostringstream* failure_message = new std::ostringstream(location);
440 failure_messages_.push_back(failure_message);
441 return *failure_message;
442}
443
444void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
445 size_t failure_num = failure_messages_.size();
446 DCHECK_NE(failure_num, 0U);
447 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
448 prepend += last_fail_message->str();
449 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
450 delete last_fail_message;
451}
452
453void MethodVerifier::AppendToLastFailMessage(std::string append) {
454 size_t failure_num = failure_messages_.size();
455 DCHECK_NE(failure_num, 0U);
456 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
457 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800458}
459
Ian Rogers776ac1f2012-04-13 23:36:36 -0700460bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700461 const uint16_t* insns = code_item_->insns_;
462 size_t insns_size = code_item_->insns_size_in_code_units_;
463 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700464 size_t new_instance_count = 0;
465 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700467
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700469 Instruction::Code opcode = inst->Opcode();
470 if (opcode == Instruction::NEW_INSTANCE) {
471 new_instance_count++;
472 } else if (opcode == Instruction::MONITOR_ENTER) {
473 monitor_enter_count++;
474 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700475 size_t inst_size = inst->SizeInCodeUnits();
476 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
477 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700478 inst = inst->Next();
479 }
480
Ian Rogersd81871c2011-10-03 13:57:23 -0700481 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
483 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700484 return false;
485 }
486
Ian Rogersd81871c2011-10-03 13:57:23 -0700487 new_instance_count_ = new_instance_count;
488 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700489 return true;
490}
491
Ian Rogers776ac1f2012-04-13 23:36:36 -0700492bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700494 if (tries_size == 0) {
495 return true;
496 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700498 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700499
500 for (uint32_t idx = 0; idx < tries_size; idx++) {
501 const DexFile::TryItem* try_item = &tries[idx];
502 uint32_t start = try_item->start_addr_;
503 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700504 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
506 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700507 return false;
508 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700509 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700511 return false;
512 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 for (uint32_t dex_pc = start; dex_pc < end;
514 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
515 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700516 }
517 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800518 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700519 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700520 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700521 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700522 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700523 CatchHandlerIterator iterator(handlers_ptr);
524 for (; iterator.HasNext(); iterator.Next()) {
525 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700528 return false;
529 }
jeffhao60f83e32012-02-13 17:16:30 -0800530 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
531 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700533 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800534 return false;
535 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700537 // Ensure exception types are resolved so that they don't need resolution to be delivered,
538 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700539 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
541 iterator.GetHandlerTypeIndex(),
542 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700543 if (exception_type == NULL) {
544 DCHECK(Thread::Current()->IsExceptionPending());
545 Thread::Current()->ClearException();
546 }
547 }
jeffhaobdb76512011-09-07 11:43:16 -0700548 }
Ian Rogers0571d352011-11-03 19:51:38 -0700549 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700550 }
jeffhaobdb76512011-09-07 11:43:16 -0700551 return true;
552}
553
Ian Rogers776ac1f2012-04-13 23:36:36 -0700554bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700556
Ian Rogers0c7abda2012-09-19 13:33:42 -0700557 /* 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 -0700558 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700559 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700560
561 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700562 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700564 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 return false;
566 }
567 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700568 // All invoke points are marked as "Throw" points already.
569 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700571 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 }
573 dex_pc += inst->SizeInCodeUnits();
574 inst = inst->Next();
575 }
576 return true;
577}
578
Ian Rogers776ac1f2012-04-13 23:36:36 -0700579bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 bool result = true;
582 switch (inst->GetVerifyTypeArgumentA()) {
583 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 }
590 switch (inst->GetVerifyTypeArgumentB()) {
591 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800592 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 break;
594 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800595 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 break;
597 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800598 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 break;
600 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800601 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 break;
603 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800604 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 break;
606 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 break;
609 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 break;
612 }
613 switch (inst->GetVerifyTypeArgumentC()) {
614 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800615 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 break;
617 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 break;
620 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800621 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 break;
623 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 break;
626 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800627 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 break;
629 }
630 switch (inst->GetVerifyExtraFlags()) {
631 case Instruction::kVerifyArrayData:
632 result = result && CheckArrayData(code_offset);
633 break;
634 case Instruction::kVerifyBranchTarget:
635 result = result && CheckBranchTarget(code_offset);
636 break;
637 case Instruction::kVerifySwitchTargets:
638 result = result && CheckSwitchTargets(code_offset);
639 break;
640 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 break;
643 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 result = false;
649 break;
650 }
651 return result;
652}
653
Ian Rogers776ac1f2012-04-13 23:36:36 -0700654bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
657 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 return false;
659 }
660 return true;
661}
662
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
666 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 return false;
668 }
669 return true;
670}
671
Ian Rogers776ac1f2012-04-13 23:36:36 -0700672bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
675 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 return false;
677 }
678 return true;
679}
680
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
684 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 return false;
686 }
687 return true;
688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
693 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 return false;
695 }
696 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700697 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 return false;
701 }
702 return true;
703}
704
Ian Rogers776ac1f2012-04-13 23:36:36 -0700705bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
708 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 return true;
712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
717 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 return false;
719 }
720 return true;
721}
722
Ian Rogers776ac1f2012-04-13 23:36:36 -0700723bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
726 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 return false;
728 }
729 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700730 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 const char* cp = descriptor;
732 while (*cp++ == '[') {
733 bracket_count++;
734 }
735 if (bracket_count == 0) {
736 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return false;
739 } else if (bracket_count > 255) {
740 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 return false;
743 }
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
749 const uint16_t* insns = code_item_->insns_ + cur_offset;
750 const uint16_t* array_data;
751 int32_t array_data_offset;
752
753 DCHECK_LT(cur_offset, insn_count);
754 /* make sure the start of the array data table is in range */
755 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
756 if ((int32_t) cur_offset + array_data_offset < 0 ||
757 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
759 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return false;
761 }
762 /* offset to array data table is a relative branch-style offset */
763 array_data = insns + array_data_offset;
764 /* make sure the table is 32-bit aligned */
765 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
767 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700771 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
773 /* make sure the end of the switch is in range */
774 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
776 << ", data offset " << array_data_offset << ", end "
777 << cur_offset + array_data_offset + table_size
778 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return false;
780 }
781 return true;
782}
783
Ian Rogers776ac1f2012-04-13 23:36:36 -0700784bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 int32_t offset;
786 bool isConditional, selfOkay;
787 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
788 return false;
789 }
790 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700791 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 -0700792 return false;
793 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700794 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
795 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
801 int32_t abs_offset = cur_offset + offset;
802 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700804 << reinterpret_cast<void*>(abs_offset) << ") at "
805 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 return false;
807 }
808 insn_flags_[abs_offset].SetBranchTarget();
809 return true;
810}
811
Ian Rogers776ac1f2012-04-13 23:36:36 -0700812bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 bool* selfOkay) {
814 const uint16_t* insns = code_item_->insns_ + cur_offset;
815 *pConditional = false;
816 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700817 switch (*insns & 0xff) {
818 case Instruction::GOTO:
819 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 break;
821 case Instruction::GOTO_32:
822 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700823 *selfOkay = true;
824 break;
825 case Instruction::GOTO_16:
826 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 break;
828 case Instruction::IF_EQ:
829 case Instruction::IF_NE:
830 case Instruction::IF_LT:
831 case Instruction::IF_GE:
832 case Instruction::IF_GT:
833 case Instruction::IF_LE:
834 case Instruction::IF_EQZ:
835 case Instruction::IF_NEZ:
836 case Instruction::IF_LTZ:
837 case Instruction::IF_GEZ:
838 case Instruction::IF_GTZ:
839 case Instruction::IF_LEZ:
840 *pOffset = (int16_t) insns[1];
841 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700842 break;
843 default:
844 return false;
845 break;
846 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 return true;
848}
849
Ian Rogers776ac1f2012-04-13 23:36:36 -0700850bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
856 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
858 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700859 return false;
860 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 /* make sure the table is 32-bit aligned */
864 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
866 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700867 return false;
868 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 uint32_t switch_count = switch_insns[1];
870 int32_t keys_offset, targets_offset;
871 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700872 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
873 /* 0=sig, 1=count, 2/3=firstKey */
874 targets_offset = 4;
875 keys_offset = -1;
876 expected_signature = Instruction::kPackedSwitchSignature;
877 } else {
878 /* 0=sig, 1=count, 2..count*2 = keys */
879 keys_offset = 2;
880 targets_offset = 2 + 2 * switch_count;
881 expected_signature = Instruction::kSparseSwitchSignature;
882 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700884 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
886 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 return false;
888 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700889 /* make sure the end of the switch is in range */
890 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
892 << switch_offset << ", end "
893 << (cur_offset + switch_offset + table_size)
894 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700895 return false;
896 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700897 /* for a sparse switch, verify the keys are in ascending order */
898 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
900 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
902 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
903 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
905 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 return false;
907 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700908 last_key = key;
909 }
910 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 for (uint32_t targ = 0; targ < switch_count; targ++) {
913 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
914 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
915 int32_t abs_offset = cur_offset + offset;
916 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700918 << reinterpret_cast<void*>(abs_offset) << ") at "
919 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700920 return false;
921 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 insn_flags_[abs_offset].SetBranchTarget();
923 }
924 return true;
925}
926
Ian Rogers776ac1f2012-04-13 23:36:36 -0700927bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
932 uint16_t registers_size = code_item_->registers_size_;
933 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800934 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
936 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 }
940
941 return true;
942}
943
Ian Rogers776ac1f2012-04-13 23:36:36 -0700944bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 uint16_t registers_size = code_item_->registers_size_;
946 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
947 // integer overflow when adding them here.
948 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
950 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700951 return false;
952 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700953 return true;
954}
955
Ian Rogers0c7abda2012-09-19 13:33:42 -0700956static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800957 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700958 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800959 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
960 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
961 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
962 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
963 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
964 gc_map.begin(),
965 gc_map.end());
966 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
967 DCHECK_EQ(gc_map.size(),
968 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
969 (length_prefixed_gc_map->at(1) << 16) |
970 (length_prefixed_gc_map->at(2) << 8) |
971 (length_prefixed_gc_map->at(3) << 0)));
972 return length_prefixed_gc_map;
973}
974
Ian Rogers776ac1f2012-04-13 23:36:36 -0700975bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 uint16_t registers_size = code_item_->registers_size_;
977 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700978
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800980 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
981 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 }
983 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700984 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
985
jeffhaobdb76512011-09-07 11:43:16 -0700986
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 work_line_.reset(new RegisterLine(registers_size, this));
988 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700989
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 /* Initialize register types of method arguments. */
991 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700992 DCHECK_NE(failures_.size(), 0U);
993 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800994 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700995 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 }
998 /* Perform code flow verification. */
999 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001000 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001002 }
1003
Ian Rogers1212a022013-03-04 10:48:41 -08001004 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -07001005
TDYa127b2eb5c12012-05-24 15:52:10 -07001006
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001008 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1009 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001010 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false; // Not a real failure, but a failure to encode
1012 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07001013 if (kIsDebugBuild) {
1014 VerifyGcMap(*map);
1015 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07001016 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1017 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001018
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001019 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
1020 if(method_to_safe_casts != NULL ) {
1021 SetSafeCastMap(ref, method_to_safe_casts);
1022 }
1023
Ian Rogersd0583802013-06-01 10:51:46 -07001024 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001025 if(pc_to_concrete_method != NULL ) {
1026 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001027 }
jeffhaobdb76512011-09-07 11:43:16 -07001028 return true;
1029}
1030
Ian Rogersad0b3a32012-04-16 14:50:24 -07001031std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1032 DCHECK_EQ(failures_.size(), failure_messages_.size());
1033 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001034 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001035 }
1036 return os;
1037}
1038
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001040 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001041 v->Dump(std::cerr);
1042}
1043
Ian Rogers776ac1f2012-04-13 23:36:36 -07001044void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001045 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001046 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 return;
jeffhaobdb76512011-09-07 11:43:16 -07001048 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001049 {
1050 os << "Register Types:\n";
1051 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1052 std::ostream indent_os(&indent_filter);
1053 reg_types_.Dump(indent_os);
1054 }
Ian Rogersb4903572012-10-11 11:52:56 -07001055 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001056 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1057 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 const Instruction* inst = Instruction::At(code_item_->insns_);
1059 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1060 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1062 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001064 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001065 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001066 const bool kDumpHexOfInstruction = false;
1067 if (kDumpHexOfInstruction) {
1068 indent_os << inst->DumpHex(5) << " ";
1069 }
1070 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001071 inst = inst->Next();
1072 }
jeffhaobdb76512011-09-07 11:43:16 -07001073}
1074
Ian Rogersd81871c2011-10-03 13:57:23 -07001075static bool IsPrimitiveDescriptor(char descriptor) {
1076 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001077 case 'I':
1078 case 'C':
1079 case 'S':
1080 case 'B':
1081 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001082 case 'F':
1083 case 'D':
1084 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001086 default:
1087 return false;
1088 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001089}
1090
Ian Rogers776ac1f2012-04-13 23:36:36 -07001091bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 RegisterLine* reg_line = reg_table_.GetLine(0);
1093 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1094 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001095
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1097 //Include the "this" pointer.
1098 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001099 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1101 // argument as uninitialized. This restricts field access until the superclass constructor is
1102 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001103 const RegType& declaring_class = GetDeclaringClass();
1104 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 reg_line->SetRegisterType(arg_start + cur_arg,
1106 reg_types_.UninitializedThisArgument(declaring_class));
1107 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001108 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001109 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001111 }
1112
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001113 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001114 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001115 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001116
1117 for (; iterator.HasNext(); iterator.Next()) {
1118 const char* descriptor = iterator.GetDescriptor();
1119 if (descriptor == NULL) {
1120 LOG(FATAL) << "Null descriptor";
1121 }
1122 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1124 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 switch (descriptor[0]) {
1128 case 'L':
1129 case '[':
1130 // We assume that reference arguments are initialized. The only way it could be otherwise
1131 // (assuming the caller was verified) is if the current method is <init>, but in that case
1132 // it's effectively considered initialized the instant we reach here (in the sense that we
1133 // can return without doing anything or call virtual methods).
1134 {
Ian Rogersb4903572012-10-11 11:52:56 -07001135 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001136 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 }
1138 break;
1139 case 'Z':
1140 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1141 break;
1142 case 'C':
1143 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1144 break;
1145 case 'B':
1146 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1147 break;
1148 case 'I':
1149 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1150 break;
1151 case 'S':
1152 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1153 break;
1154 case 'F':
1155 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1156 break;
1157 case 'J':
1158 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001159 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1160 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1161 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 cur_arg++;
1163 break;
1164 }
1165 default:
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 return false;
1168 }
1169 cur_arg++;
1170 }
1171 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 return false;
1174 }
1175 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1176 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1177 // format. Only major difference from the method argument format is that 'V' is supported.
1178 bool result;
1179 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1180 result = descriptor[1] == '\0';
1181 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1182 size_t i = 0;
1183 do {
1184 i++;
1185 } while (descriptor[i] == '['); // process leading [
1186 if (descriptor[i] == 'L') { // object array
1187 do {
1188 i++; // find closing ;
1189 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1190 result = descriptor[i] == ';';
1191 } else { // primitive array
1192 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1193 }
1194 } else if (descriptor[0] == 'L') {
1195 // could be more thorough here, but shouldn't be required
1196 size_t i = 0;
1197 do {
1198 i++;
1199 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1200 result = descriptor[i] == ';';
1201 } else {
1202 result = false;
1203 }
1204 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1206 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 }
1208 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001209}
1210
Ian Rogers776ac1f2012-04-13 23:36:36 -07001211bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 const uint16_t* insns = code_item_->insns_;
1213 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001214
jeffhaobdb76512011-09-07 11:43:16 -07001215 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 insn_flags_[0].SetChanged();
1217 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001218
jeffhaobdb76512011-09-07 11:43:16 -07001219 /* Continue until no instructions are marked "changed". */
1220 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1222 uint32_t insn_idx = start_guess;
1223 for (; insn_idx < insns_size; insn_idx++) {
1224 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001225 break;
1226 }
jeffhaobdb76512011-09-07 11:43:16 -07001227 if (insn_idx == insns_size) {
1228 if (start_guess != 0) {
1229 /* try again, starting from the top */
1230 start_guess = 0;
1231 continue;
1232 } else {
1233 /* all flags are clear */
1234 break;
1235 }
1236 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 // We carry the working set of registers from instruction to instruction. If this address can
1238 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1239 // "changed" flags, we need to load the set of registers from the table.
1240 // Because we always prefer to continue on to the next instruction, we should never have a
1241 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1242 // target.
1243 work_insn_idx_ = insn_idx;
1244 if (insn_flags_[insn_idx].IsBranchTarget()) {
1245 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001246 } else {
1247#ifndef NDEBUG
1248 /*
1249 * Sanity check: retrieve the stored register line (assuming
1250 * a full table) and make sure it actually matches.
1251 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1253 if (register_line != NULL) {
1254 if (work_line_->CompareLine(register_line) != 0) {
1255 Dump(std::cout);
1256 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001258 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1259 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001260 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 }
jeffhaobdb76512011-09-07 11:43:16 -07001262 }
1263#endif
1264 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001266 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001267 prepend += " failed to verify: ";
1268 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 return false;
1270 }
jeffhaobdb76512011-09-07 11:43:16 -07001271 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 insn_flags_[insn_idx].SetVisited();
1273 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001274 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001275
Ian Rogers1c849e52012-06-28 14:00:33 -07001276 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001277 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001278 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001279 * (besides the wasted space), but it indicates a flaw somewhere
1280 * down the line, possibly in the verifier.
1281 *
1282 * If we've substituted "always throw" instructions into the stream,
1283 * we are almost certainly going to have some dead code.
1284 */
1285 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 uint32_t insn_idx = 0;
1287 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001288 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001289 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001290 * may or may not be preceded by a padding NOP (for alignment).
1291 */
1292 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1293 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1294 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001295 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001296 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1297 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1298 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001300 }
1301
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001303 if (dead_start < 0)
1304 dead_start = insn_idx;
1305 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001306 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001307 dead_start = -1;
1308 }
1309 }
1310 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001311 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001313 // To dump the state of the verify after a method, do something like:
1314 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1315 // "boolean java.lang.String.equals(java.lang.Object)") {
1316 // LOG(INFO) << info_messages_.str();
1317 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 }
jeffhaobdb76512011-09-07 11:43:16 -07001319 return true;
1320}
1321
Ian Rogers776ac1f2012-04-13 23:36:36 -07001322bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001323 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1324 // We want the state _before_ the instruction, for the case where the dex pc we're
1325 // interested in is itself a monitor-enter instruction (which is a likely place
1326 // for a thread to be suspended).
1327 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001328 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001329 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1330 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1331 }
1332 }
1333
jeffhaobdb76512011-09-07 11:43:16 -07001334 /*
1335 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001337 * control to another statement:
1338 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001339 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001340 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001341 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001342 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001343 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001344 * throw an exception that is handled by an encompassing "try"
1345 * block.
1346 *
1347 * We can also return, in which case there is no successor instruction
1348 * from this point.
1349 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001350 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001351 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1353 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001354 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001355
jeffhaobdb76512011-09-07 11:43:16 -07001356 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001357 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001358 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001360 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1361 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 }
jeffhaobdb76512011-09-07 11:43:16 -07001363
1364 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001365 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001366 * can throw an exception, we will copy/merge this into the "catch"
1367 * address rather than work_line, because we don't want the result
1368 * from the "successful" code path (e.g. a check-cast that "improves"
1369 * a type) to be visible to the exception handler.
1370 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001371 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001373 } else {
1374#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001376#endif
1377 }
1378
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001379
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001380 // We need to ensure the work line is consistent while performing validation. When we spot a
1381 // peephole pattern we compute a new line for either the fallthrough instruction or the
1382 // branch target.
1383 UniquePtr<RegisterLine> branch_line;
1384 UniquePtr<RegisterLine> fallthrough_line;
1385
Sebastien Hertz5243e912013-05-21 10:55:07 +02001386 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001387 case Instruction::NOP:
1388 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001389 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001390 * a signature that looks like a NOP; if we see one of these in
1391 * the course of executing code then we have a problem.
1392 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001393 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001395 }
1396 break;
1397
1398 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001399 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1400 break;
jeffhaobdb76512011-09-07 11:43:16 -07001401 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001402 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1403 break;
jeffhaobdb76512011-09-07 11:43:16 -07001404 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001405 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001406 break;
1407 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001408 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1409 break;
jeffhaobdb76512011-09-07 11:43:16 -07001410 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001411 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1412 break;
jeffhaobdb76512011-09-07 11:43:16 -07001413 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001414 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001415 break;
1416 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001417 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1418 break;
jeffhaobdb76512011-09-07 11:43:16 -07001419 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001420 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1421 break;
jeffhaobdb76512011-09-07 11:43:16 -07001422 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001423 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001424 break;
1425
1426 /*
1427 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001428 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001429 * might want to hold the result in an actual CPU register, so the
1430 * Dalvik spec requires that these only appear immediately after an
1431 * invoke or filled-new-array.
1432 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001433 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001434 * redundant with the reset done below, but it can make the debug info
1435 * easier to read in some cases.)
1436 */
1437 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001438 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001439 break;
1440 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001441 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001442 break;
1443 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001444 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001445 break;
1446
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001448 /*
jeffhao60f83e32012-02-13 17:16:30 -08001449 * This statement can only appear as the first instruction in an exception handler. We verify
1450 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001451 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001452 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001453 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001454 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 }
jeffhaobdb76512011-09-07 11:43:16 -07001456 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001457 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1458 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 }
jeffhaobdb76512011-09-07 11:43:16 -07001461 }
1462 break;
1463 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001464 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001465 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 const RegType& return_type = GetMethodReturnType();
1467 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 } else {
1470 // Compilers may generate synthetic functions that write byte values into boolean fields.
1471 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 const uint32_t vregA = inst->VRegA_11x();
1473 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1475 ((return_type.IsBoolean() || return_type.IsByte() ||
1476 return_type.IsShort() || return_type.IsChar()) &&
1477 src_type.IsInteger()));
1478 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001479 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001481 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 }
jeffhaobdb76512011-09-07 11:43:16 -07001484 }
1485 }
1486 break;
1487 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001489 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 const RegType& return_type = GetMethodReturnType();
1491 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 } else {
1494 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 const uint32_t vregA = inst->VRegA_11x();
1496 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 }
jeffhaobdb76512011-09-07 11:43:16 -07001500 }
1501 }
1502 break;
1503 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001504 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 const RegType& return_type = GetMethodReturnType();
1506 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 } else {
1509 /* return_type is the *expected* return type, not register value */
1510 DCHECK(!return_type.IsZero());
1511 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001512 const uint32_t vregA = inst->VRegA_11x();
1513 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001514 // Disallow returning uninitialized values and verify that the reference in vAA is an
1515 // instance of the "return_type"
1516 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001517 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001518 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001519 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1520 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001521 }
1522 }
1523 }
1524 break;
1525
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001526 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001527 case Instruction::CONST_4: {
1528 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1529 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001530 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 }
1532 case Instruction::CONST_16: {
1533 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1534 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001535 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 work_line_->SetRegisterType(inst->VRegA_31i(),
1539 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001540 break;
1541 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001542 work_line_->SetRegisterType(inst->VRegA_21h(),
1543 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
jeffhaobdb76512011-09-07 11:43:16 -07001545 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
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_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001551 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 }
1553 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
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_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 break;
1559 }
1560 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 int64_t val = inst->VRegB_51l();
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_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 break;
1566 }
1567 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1570 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 break;
1573 }
jeffhaobdb76512011-09-07 11:43:16 -07001574 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1576 break;
jeffhaobdb76512011-09-07 11:43:16 -07001577 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001579 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001581 // Get type from instruction if unresolved then we need an access check
1582 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001583 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001584 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001586 res_type.IsConflict() ? res_type
1587 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001588 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 }
jeffhaobdb76512011-09-07 11:43:16 -07001590 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001591 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001592 break;
1593 case Instruction::MONITOR_EXIT:
1594 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001595 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001596 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001597 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001598 * to the need to handle asynchronous exceptions, a now-deprecated
1599 * feature that Dalvik doesn't support.)
1600 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001601 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001602 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001603 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001604 * structured locking checks are working, the former would have
1605 * failed on the -enter instruction, and the latter is impossible.
1606 *
1607 * This is fortunate, because issue 3221411 prevents us from
1608 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001609 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001610 * some catch blocks (which will show up as "dead" code when
1611 * we skip them here); if we can't, then the code path could be
1612 * "live" so we still need to check it.
1613 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001616 break;
1617
Ian Rogers28ad40d2011-10-27 15:19:26 -07001618 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001620 /*
1621 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1622 * could be a "upcast" -- not expected, so we don't try to address it.)
1623 *
1624 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001625 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001626 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1628 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1629 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001630 if (res_type.IsConflict()) {
1631 DCHECK_NE(failures_.size(), 0U);
1632 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001634 }
1635 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001636 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001637 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1639 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001640 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 if (is_checkcast) {
1642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1643 } else {
1644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1645 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001646 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 if (is_checkcast) {
1648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1649 } else {
1650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1651 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001652 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001653 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 }
1661 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001663 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001664 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 }
1669 }
1670 break;
1671 }
1672 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001674 if (res_type.IsConflict()) {
1675 DCHECK_NE(failures_.size(), 0U);
1676 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001677 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001678 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1679 // can't create an instance of an interface or abstract class */
1680 if (!res_type.IsInstantiableTypes()) {
1681 Fail(VERIFY_ERROR_INSTANTIATION)
1682 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001683 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001685 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1686 // Any registers holding previous allocations from this address that have not yet been
1687 // initialized must be marked invalid.
1688 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1689 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 break;
1692 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001693 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
1696 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001698 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001699 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001700 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001702 just_set_result = true; // Filled new array range sets result register
1703 break;
jeffhaobdb76512011-09-07 11:43:16 -07001704 case Instruction::CMPL_FLOAT:
1705 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001707 break;
1708 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001710 break;
1711 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
1714 case Instruction::CMPL_DOUBLE:
1715 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001716 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001718 break;
1719 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001722 break;
1723 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001725 break;
1726 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001728 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001729 break;
1730 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001733 break;
1734 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001735 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001736 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001739 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001740 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001741 }
1742 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::GOTO:
1745 case Instruction::GOTO_16:
1746 case Instruction::GOTO_32:
1747 /* no effect on or use of registers */
1748 break;
1749
1750 case Instruction::PACKED_SWITCH:
1751 case Instruction::SPARSE_SWITCH:
1752 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
1755
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 case Instruction::FILL_ARRAY_DATA: {
1757 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001759 /* array_type can be null if the reg type is Zero */
1760 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001761 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001763 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001764 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1765 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001766 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1768 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 } else {
jeffhao457cc512012-02-02 16:55:13 -08001770 // Now verify if the element width in the table matches the element width declared in
1771 // the array
1772 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1773 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001775 } else {
1776 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1777 // Since we don't compress the data in Dex, expect to see equal width of data stored
1778 // in the table and expected from the array class.
1779 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1781 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001782 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 }
1784 }
jeffhaobdb76512011-09-07 11:43:16 -07001785 }
1786 }
1787 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 }
jeffhaobdb76512011-09-07 11:43:16 -07001789 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1792 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 bool mismatch = false;
1794 if (reg_type1.IsZero()) { // zero then integral or reference expected
1795 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1796 } else if (reg_type1.IsReferenceTypes()) { // both references?
1797 mismatch = !reg_type2.IsReferenceTypes();
1798 } else { // both integral?
1799 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1800 }
1801 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1803 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001804 }
1805 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 }
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::IF_LT:
1808 case Instruction::IF_GE:
1809 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1812 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1815 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001816 }
1817 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 }
jeffhaobdb76512011-09-07 11:43:16 -07001819 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001821 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001825
1826 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001827 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001828 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001829 instance_of_idx = work_insn_idx_ - 1;
1830 while(0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
1831 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001832 }
Ian Rogers9b360392013-06-06 14:45:07 -07001833 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001834 } else {
1835 break;
1836 }
1837
Ian Rogers9b360392013-06-06 14:45:07 -07001838 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001839
1840 /* Check for peep-hole pattern of:
1841 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001842 * instance-of vX, vY, T;
1843 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001844 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001845 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001846 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001847 * and sharpen the type of vY to be type T.
1848 * Note, this pattern can't be if:
1849 * - if there are other branches to this branch,
1850 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001851 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001852 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001853 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1854 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1855 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001856 // Check that the we are not attempting conversion to interface types,
1857 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001858 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001859
Ian Rogersfae370a2013-06-05 08:33:27 -07001860 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001861 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001862 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001863 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001864 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001865 branch_line.reset(update_line);
1866 }
1867 update_line->CopyFromLine(work_line_.get());
1868 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1869 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1870 // See if instance-of was preceded by a move-object operation, common due to the small
1871 // register encoding space of instance-of, and propagate type information to the source
1872 // of the move-object.
1873 uint32_t move_idx = instance_of_idx - 1;
1874 while(0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
1875 move_idx--;
1876 }
1877 CHECK(insn_flags_[move_idx].IsOpcode());
1878 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1879 switch (move_inst->Opcode()) {
1880 case Instruction::MOVE_OBJECT:
1881 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1882 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1883 }
1884 break;
1885 case Instruction::MOVE_OBJECT_FROM16:
1886 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1887 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1888 }
1889 break;
1890 case Instruction::MOVE_OBJECT_16:
1891 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1892 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1893 }
1894 break;
1895 default:
1896 break;
1897 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001898 }
1899 }
1900 }
1901
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 }
jeffhaobdb76512011-09-07 11:43:16 -07001904 case Instruction::IF_LTZ:
1905 case Instruction::IF_GEZ:
1906 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001908 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1911 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
jeffhaobdb76512011-09-07 11:43:16 -07001913 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 }
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001916 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 break;
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 break;
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001922 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 break;
jeffhaobdb76512011-09-07 11:43:16 -07001924 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 break;
jeffhaobdb76512011-09-07 11:43:16 -07001930 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 break;
1933 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001935 break;
1936
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001938 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 break;
1940 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001941 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 break;
1943 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001944 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 break;
1946 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001947 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001951 break;
1952 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
1955 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958
jeffhaobdb76512011-09-07 11:43:16 -07001959 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 break;
jeffhaobdb76512011-09-07 11:43:16 -07001962 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 break;
jeffhaobdb76512011-09-07 11:43:16 -07001965 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 break;
jeffhaobdb76512011-09-07 11:43:16 -07001968 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 break;
1971 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001976 break;
1977 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 break;
jeffhaobdb76512011-09-07 11:43:16 -07001980
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 break;
1984 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 break;
1987 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 break;
1990 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001992 break;
1993 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001995 break;
1996 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
2002
jeffhaobdb76512011-09-07 11:43:16 -07002003 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
jeffhaobdb76512011-09-07 11:43:16 -07002009 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
jeffhaobdb76512011-09-07 11:43:16 -07002012 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 break;
2015 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024
2025 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
2028 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
2031 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
2034 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046
2047 case Instruction::INVOKE_VIRTUAL:
2048 case Instruction::INVOKE_VIRTUAL_RANGE:
2049 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2052 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2053 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2054 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2055 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002056 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002057 const char* descriptor;
2058 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002060 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2061 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2062 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2063 } else {
2064 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002065 }
Ian Rogersb4903572012-10-11 11:52:56 -07002066 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002067 if (!return_type.IsLowHalf()) {
2068 work_line_->SetResultRegisterType(return_type);
2069 } else {
2070 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2071 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002072 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2078 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002079 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002080 const char* return_type_descriptor;
2081 bool is_constructor;
2082 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002084 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2085 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2086 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2087 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2088 } else {
2089 is_constructor = called_method->IsConstructor();
2090 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2091 }
2092 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002093 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 * Some additional checks when calling a constructor. We know from the invocation arg check
2095 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2096 * that to require that called_method->klass is the same as this->klass or this->super,
2097 * allowing the latter only if the "this" argument is the same as the "this" argument to
2098 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002099 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002101 if (this_type.IsConflict()) // failure.
2102 break;
jeffhaobdb76512011-09-07 11:43:16 -07002103
jeffhaob57e9522012-04-26 18:08:21 -07002104 /* no null refs allowed (?) */
2105 if (this_type.IsZero()) {
2106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2107 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002108 }
jeffhaob57e9522012-04-26 18:08:21 -07002109
2110 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002111 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2112 // TODO: re-enable constructor type verification
2113 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002114 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002115 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2116 // break;
2117 // }
jeffhaob57e9522012-04-26 18:08:21 -07002118
2119 /* arg must be an uninitialized reference */
2120 if (!this_type.IsUninitializedTypes()) {
2121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2122 << this_type;
2123 break;
2124 }
2125
2126 /*
2127 * Replace the uninitialized reference with an initialized one. We need to do this for all
2128 * registers that have the same object instance in them, not just the "this" register.
2129 */
2130 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002131 }
Ian Rogersb4903572012-10-11 11:52:56 -07002132 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2133 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 if (!return_type.IsLowHalf()) {
2135 work_line_->SetResultRegisterType(return_type);
2136 } else {
2137 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002139 just_set_result = true;
2140 break;
2141 }
2142 case Instruction::INVOKE_STATIC:
2143 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2145 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002146 const char* descriptor;
2147 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002149 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2150 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002151 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002152 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002153 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002154 }
Ian Rogersb4903572012-10-11 11:52:56 -07002155 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 if (!return_type.IsLowHalf()) {
2157 work_line_->SetResultRegisterType(return_type);
2158 } else {
2159 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2160 }
jeffhaobdb76512011-09-07 11:43:16 -07002161 just_set_result = true;
2162 }
2163 break;
jeffhaobdb76512011-09-07 11:43:16 -07002164 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2167 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002168 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002169 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002170 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2171 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2172 << PrettyMethod(abs_method) << "'";
2173 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002174 }
Ian Rogers0d604842012-04-16 14:50:24 -07002175 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002176 /* Get the type of the "this" arg, which should either be a sub-interface of called
2177 * interface or Object (see comments in RegType::JoinClass).
2178 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002180 if (this_type.IsZero()) {
2181 /* null pointer always passes (and always fails at runtime) */
2182 } else {
2183 if (this_type.IsUninitializedTypes()) {
2184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2185 << this_type;
2186 break;
2187 }
2188 // In the past we have tried to assert that "called_interface" is assignable
2189 // from "this_type.GetClass()", however, as we do an imprecise Join
2190 // (RegType::JoinClass) we don't have full information on what interfaces are
2191 // implemented by "this_type". For example, two classes may implement the same
2192 // interfaces and have a common parent that doesn't implement the interface. The
2193 // join will set "this_type" to the parent class and a test that this implements
2194 // the interface will incorrectly fail.
2195 }
2196 /*
2197 * We don't have an object instance, so we can't find the concrete method. However, all of
2198 * the type information is in the abstract method, so we're good.
2199 */
2200 const char* descriptor;
2201 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002203 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2204 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2205 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2206 } else {
2207 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2208 }
Ian Rogersb4903572012-10-11 11:52:56 -07002209 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002210 if (!return_type.IsLowHalf()) {
2211 work_line_->SetResultRegisterType(return_type);
2212 } else {
2213 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2214 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002215 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 }
jeffhaobdb76512011-09-07 11:43:16 -07002218 case Instruction::NEG_INT:
2219 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
2222 case Instruction::NEG_LONG:
2223 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
2230 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002232 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002240 break;
2241 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
2245 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002251 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
2260 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002262 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002266 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002270 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289
2290 case Instruction::ADD_INT:
2291 case Instruction::SUB_INT:
2292 case Instruction::MUL_INT:
2293 case Instruction::REM_INT:
2294 case Instruction::DIV_INT:
2295 case Instruction::SHL_INT:
2296 case Instruction::SHR_INT:
2297 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::AND_INT:
2302 case Instruction::OR_INT:
2303 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
2307 case Instruction::ADD_LONG:
2308 case Instruction::SUB_LONG:
2309 case Instruction::MUL_LONG:
2310 case Instruction::DIV_LONG:
2311 case Instruction::REM_LONG:
2312 case Instruction::AND_LONG:
2313 case Instruction::OR_LONG:
2314 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002316 reg_types_.LongLo(), reg_types_.LongHi(),
2317 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::SHL_LONG:
2320 case Instruction::SHR_LONG:
2321 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::ADD_FLOAT:
2327 case Instruction::SUB_FLOAT:
2328 case Instruction::MUL_FLOAT:
2329 case Instruction::DIV_FLOAT:
2330 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::ADD_DOUBLE:
2334 case Instruction::SUB_DOUBLE:
2335 case Instruction::MUL_DOUBLE:
2336 case Instruction::DIV_DOUBLE:
2337 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2340 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::ADD_INT_2ADDR:
2343 case Instruction::SUB_INT_2ADDR:
2344 case Instruction::MUL_INT_2ADDR:
2345 case Instruction::REM_INT_2ADDR:
2346 case Instruction::SHL_INT_2ADDR:
2347 case Instruction::SHR_INT_2ADDR:
2348 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::AND_INT_2ADDR:
2352 case Instruction::OR_INT_2ADDR:
2353 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::ADD_LONG_2ADDR:
2360 case Instruction::SUB_LONG_2ADDR:
2361 case Instruction::MUL_LONG_2ADDR:
2362 case Instruction::DIV_LONG_2ADDR:
2363 case Instruction::REM_LONG_2ADDR:
2364 case Instruction::AND_LONG_2ADDR:
2365 case Instruction::OR_LONG_2ADDR:
2366 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.LongLo(), reg_types_.LongHi(),
2369 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::SHL_LONG_2ADDR:
2372 case Instruction::SHR_LONG_2ADDR:
2373 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::ADD_FLOAT_2ADDR:
2378 case Instruction::SUB_FLOAT_2ADDR:
2379 case Instruction::MUL_FLOAT_2ADDR:
2380 case Instruction::DIV_FLOAT_2ADDR:
2381 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::ADD_DOUBLE_2ADDR:
2385 case Instruction::SUB_DOUBLE_2ADDR:
2386 case Instruction::MUL_DOUBLE_2ADDR:
2387 case Instruction::DIV_DOUBLE_2ADDR:
2388 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002390 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2391 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::ADD_INT_LIT16:
2394 case Instruction::RSUB_INT:
2395 case Instruction::MUL_INT_LIT16:
2396 case Instruction::DIV_INT_LIT16:
2397 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::AND_INT_LIT16:
2401 case Instruction::OR_INT_LIT16:
2402 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002403 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::ADD_INT_LIT8:
2406 case Instruction::RSUB_INT_LIT8:
2407 case Instruction::MUL_INT_LIT8:
2408 case Instruction::DIV_INT_LIT8:
2409 case Instruction::REM_INT_LIT8:
2410 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002413 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::AND_INT_LIT8:
2416 case Instruction::OR_INT_LIT8:
2417 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002418 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002421 // Special instructions.
2422 //
2423 // Note: the following instructions encode offsets derived from class linking.
2424 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2425 // meaning if the class linking and resolution were successful.
2426 case Instruction::IGET_QUICK:
2427 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2428 break;
2429 case Instruction::IGET_WIDE_QUICK:
2430 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2431 break;
2432 case Instruction::IGET_OBJECT_QUICK:
2433 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2434 break;
2435 case Instruction::IPUT_QUICK:
2436 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2437 break;
2438 case Instruction::IPUT_WIDE_QUICK:
2439 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2440 break;
2441 case Instruction::IPUT_OBJECT_QUICK:
2442 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2443 break;
2444 case Instruction::INVOKE_VIRTUAL_QUICK:
2445 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2446 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2447 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2448 if (called_method != NULL) {
2449 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2450 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2451 if (!return_type.IsLowHalf()) {
2452 work_line_->SetResultRegisterType(return_type);
2453 } else {
2454 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2455 }
2456 just_set_result = true;
2457 }
2458 break;
2459 }
2460
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002462 case Instruction::UNUSED_3E:
2463 case Instruction::UNUSED_3F:
2464 case Instruction::UNUSED_40:
2465 case Instruction::UNUSED_41:
2466 case Instruction::UNUSED_42:
2467 case Instruction::UNUSED_43:
2468 case Instruction::UNUSED_73:
2469 case Instruction::UNUSED_79:
2470 case Instruction::UNUSED_7A:
2471 case Instruction::UNUSED_EB:
2472 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002473 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002474 case Instruction::UNUSED_EE:
2475 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002476 case Instruction::UNUSED_F0:
2477 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002478 case Instruction::UNUSED_F2:
2479 case Instruction::UNUSED_F3:
2480 case Instruction::UNUSED_F4:
2481 case Instruction::UNUSED_F5:
2482 case Instruction::UNUSED_F6:
2483 case Instruction::UNUSED_F7:
2484 case Instruction::UNUSED_F8:
2485 case Instruction::UNUSED_F9:
2486 case Instruction::UNUSED_FA:
2487 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002489 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002490 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002491 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494
2495 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002496 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002497 * complain if an instruction is missing (which is desirable).
2498 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002499 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002500
Ian Rogersad0b3a32012-04-16 14:50:24 -07002501 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002502 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002503 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002504 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002505 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002506 /* immediate failure, reject class */
2507 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2508 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002509 } else if (have_pending_runtime_throw_failure_) {
2510 /* slow path will throw, mark following code as unreachable */
2511 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002512 }
jeffhaobdb76512011-09-07 11:43:16 -07002513 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 * If we didn't just set the result register, clear it out. This ensures that you can only use
2515 * "move-result" immediately after the result is set. (We could check this statically, but it's
2516 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002517 */
2518 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002520 }
2521
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002522
jeffhaobdb76512011-09-07 11:43:16 -07002523
2524 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002525 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002526 *
2527 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002528 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002529 * somebody could get a reference field, check it for zero, and if the
2530 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002531 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002532 * that, and will reject the code.
2533 *
2534 * TODO: avoid re-fetching the branch target
2535 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002536 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002537 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002539 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002541 return false;
2542 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002543 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002544 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002545 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 }
jeffhaobdb76512011-09-07 11:43:16 -07002547 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002548 if (NULL != branch_line.get()) {
2549 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2550 return false;
2551 }
2552 } else {
2553 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2554 return false;
2555 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 }
jeffhaobdb76512011-09-07 11:43:16 -07002557 }
2558
2559 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002560 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002561 *
2562 * We've already verified that the table is structurally sound, so we
2563 * just need to walk through and tag the targets.
2564 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002565 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002566 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2567 const uint16_t* switch_insns = insns + offset_to_switch;
2568 int switch_count = switch_insns[1];
2569 int offset_to_targets, targ;
2570
2571 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2572 /* 0 = sig, 1 = count, 2/3 = first key */
2573 offset_to_targets = 4;
2574 } else {
2575 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002576 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002577 offset_to_targets = 2 + 2 * switch_count;
2578 }
2579
2580 /* verify each switch target */
2581 for (targ = 0; targ < switch_count; targ++) {
2582 int offset;
2583 uint32_t abs_offset;
2584
2585 /* offsets are 32-bit, and only partly endian-swapped */
2586 offset = switch_insns[offset_to_targets + targ * 2] |
2587 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 abs_offset = work_insn_idx_ + offset;
2589 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002590 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002591 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 }
2593 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002594 return false;
2595 }
2596 }
2597
2598 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2600 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002601 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002602 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002604 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002605
Ian Rogers0571d352011-11-03 19:51:38 -07002606 for (; iterator.HasNext(); iterator.Next()) {
2607 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 within_catch_all = true;
2609 }
jeffhaobdb76512011-09-07 11:43:16 -07002610 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2612 * "work_regs", because at runtime the exception will be thrown before the instruction
2613 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002614 */
Ian Rogers0571d352011-11-03 19:51:38 -07002615 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002616 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 }
jeffhaobdb76512011-09-07 11:43:16 -07002618 }
2619
2620 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2622 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002623 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002625 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 * The state in work_line reflects the post-execution state. If the current instruction is a
2627 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002628 * it will do so before grabbing the lock).
2629 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002630 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002631 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002633 return false;
2634 }
2635 }
2636 }
2637
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002638 /* Handle "continue". Tag the next consecutive instruction.
2639 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2640 * because it changes work_line_ when performing peephole optimization
2641 * and this change should not be used in those cases.
2642 */
2643 if ((opcode_flags & Instruction::kContinue) != 0) {
2644 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2645 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2647 return false;
2648 }
2649 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2650 // next instruction isn't one.
2651 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2652 return false;
2653 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07002654 if (NULL != fallthrough_line.get()) {
2655 // Make workline consistent with fallthrough computed from peephole optimization.
2656 work_line_->CopyFromLine(fallthrough_line.get());
2657 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002658 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2659 if (next_line != NULL) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002660 // Merge registers into what we have for the next instruction,
2661 // and set the "changed" flag if needed.
2662 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2663 return false;
2664 }
2665 } else {
2666 /*
2667 * We're not recording register data for the next instruction, so we don't know what the
2668 * prior state was. We have to assume that something has changed and re-evaluate it.
2669 */
2670 insn_flags_[next_insn_idx].SetChanged();
2671 }
2672 }
2673
jeffhaod1f0fde2011-09-08 17:25:33 -07002674 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002675 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002676 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 return false;
2678 }
jeffhaobdb76512011-09-07 11:43:16 -07002679 }
2680
2681 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002682 * Update start_guess. Advance to the next instruction of that's
2683 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002684 * neither of those exists we're in a return or throw; leave start_guess
2685 * alone and let the caller sort it out.
2686 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002687 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002690 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002692 }
2693
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2695 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002696
2697 return true;
2698}
2699
Ian Rogers776ac1f2012-04-13 23:36:36 -07002700const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002701 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002702 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002703 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002704 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002705 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2706 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002707 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002708 if (result.IsConflict()) {
2709 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2710 << "' in " << referrer;
2711 return result;
2712 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002713 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002714 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002715 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002716 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002717 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002718 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002719 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002720 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002721 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002722 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723}
2724
Ian Rogers776ac1f2012-04-13 23:36:36 -07002725const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002726 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002728 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2730 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002731 CatchHandlerIterator iterator(handlers_ptr);
2732 for (; iterator.HasNext(); iterator.Next()) {
2733 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2734 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002735 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002737 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002738 if (common_super == NULL) {
2739 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2740 // as that is caught at runtime
2741 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002742 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002743 // We don't know enough about the type and the common path merge will result in
2744 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002745 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002746 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002747 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002748 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002750 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002751 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 }
2753 }
2754 }
2755 }
Ian Rogers0571d352011-11-03 19:51:38 -07002756 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 }
2758 }
2759 if (common_super == NULL) {
2760 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002762 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002764 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002765}
2766
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002767mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2768 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002769 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002770 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002771 if (klass_type.IsConflict()) {
2772 std::string append(" in attempt to access method ");
2773 append += dex_file_->GetMethodName(method_id);
2774 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002775 return NULL;
2776 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002777 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002778 return NULL; // Can't resolve Class so no more to do here
2779 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002780 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002781 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002782 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002784 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002785 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002786
2787 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002789 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 res_method = klass->FindInterfaceMethod(name, signature);
2791 } else {
2792 res_method = klass->FindVirtualMethod(name, signature);
2793 }
2794 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002795 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002797 // If a virtual or interface method wasn't found with the expected type, look in
2798 // the direct methods. This can happen when the wrong invoke type is used or when
2799 // a class has changed, and will be flagged as an error in later checks.
2800 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2801 res_method = klass->FindDirectMethod(name, signature);
2802 }
2803 if (res_method == NULL) {
2804 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2805 << PrettyDescriptor(klass) << "." << name
2806 << " " << signature;
2807 return NULL;
2808 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 }
2810 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2812 // enforce them here.
2813 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2815 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 return NULL;
2817 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002818 // Disallow any calls to class initializers.
2819 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2821 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002822 return NULL;
2823 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002824 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002825 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002826 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002828 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002829 }
jeffhaode0d9c92012-02-27 13:58:13 -08002830 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2831 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2833 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002834 return NULL;
2835 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002836 // Check that interface methods match interface classes.
2837 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2838 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2839 << " is in an interface class " << PrettyClass(klass);
2840 return NULL;
2841 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2842 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2843 << " is in a non-interface class " << PrettyClass(klass);
2844 return NULL;
2845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 // See if the method type implied by the invoke instruction matches the access flags for the
2847 // target method.
2848 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2849 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2850 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2851 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002852 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2853 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 return NULL;
2855 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002856 return res_method;
2857}
2858
Sebastien Hertz5243e912013-05-21 10:55:07 +02002859mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2860 MethodType method_type,
2861 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002862 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002863 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2864 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002865 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2866 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002867 if (res_method == NULL) { // error or class is unresolved
2868 return NULL;
2869 }
2870
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2872 // has a vtable entry for the target method.
2873 if (is_super) {
2874 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002876 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002877 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002878 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002879 << " to super " << PrettyMethod(res_method);
2880 return NULL;
2881 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002882 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002884 MethodHelper mh(res_method);
2885 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002886 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002887 << " to super " << super
2888 << "." << mh.GetName()
2889 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 return NULL;
2891 }
2892 }
2893 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002894 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002896 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 /* caught by static verifier */
2898 DCHECK(is_range || expected_args <= 5);
2899 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2902 return NULL;
2903 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002904
jeffhaobdb76512011-09-07 11:43:16 -07002905 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 * Check the "this" argument, which must be an instance of the class that declared the method.
2907 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2908 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002909 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002910 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002912 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002913 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 return NULL;
2915 }
2916 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 return NULL;
2919 }
2920 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002921 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002922 const RegType& res_method_class =
2923 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2924 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002925 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002927 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 return NULL;
2929 }
2930 }
2931 actual_args++;
2932 }
2933 /*
2934 * Process the target method's signature. This signature may or may not
2935 * have been verified, so we can't assume it's properly formed.
2936 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002937 MethodHelper mh(res_method);
2938 const DexFile::TypeList* params = mh.GetParameterTypeList();
2939 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002940 uint32_t arg[5];
2941 if (!is_range) {
2942 inst->GetArgs(arg);
2943 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002944 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002947 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2948 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 return NULL;
2950 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002951 const char* descriptor =
2952 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2953 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002955 << " missing signature component";
2956 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 }
Ian Rogersb4903572012-10-11 11:52:56 -07002958 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002959 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002960 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002961 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 }
2963 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2964 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002967 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 return NULL;
2969 } else {
2970 return res_method;
2971 }
2972}
2973
Sebastien Hertzc15853b2013-06-25 17:36:27 +02002974mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
2975 RegisterLine* reg_line,
2976 bool is_range) {
2977 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
2978 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2979 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002980 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
2981 return NULL;
2982 }
2983 mirror::Class* this_class = NULL;
2984 if (!actual_arg_type.IsUnresolvedTypes()) {
2985 this_class = actual_arg_type.GetClass();
2986 } else {
2987 const std::string& descriptor(actual_arg_type.GetDescriptor());
2988 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2989 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
2990 if (this_class == NULL) {
2991 Thread::Current()->ClearException();
2992 // Look for a system class
2993 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
2994 }
2995 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02002996 if (this_class == NULL) {
2997 return NULL;
2998 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002999 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3000 CHECK(vtable != NULL);
3001 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3002 CHECK(vtable_index < vtable->GetLength());
3003 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3004 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003005 return res_method;
3006}
3007
3008mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3009 bool is_range) {
3010 DCHECK(Runtime::Current()->IsStarted());
3011 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3012 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003013 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003015 return NULL;
3016 }
3017 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3018
3019 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3020 // match the call to the signature. Also, we might be calling through an abstract method
3021 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003022 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3023 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3024 return NULL;
3025 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003026 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3027 /* caught by static verifier */
3028 DCHECK(is_range || expected_args <= 5);
3029 if (expected_args > code_item_->outs_size_) {
3030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3031 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3032 return NULL;
3033 }
3034
3035 /*
3036 * Check the "this" argument, which must be an instance of the class that declared the method.
3037 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3038 * rigorous check here (which is okay since we have to do it at runtime).
3039 */
3040 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3042 return NULL;
3043 }
3044 if (!actual_arg_type.IsZero()) {
3045 mirror::Class* klass = res_method->GetDeclaringClass();
3046 const RegType& res_method_class =
3047 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3048 klass->CannotBeAssignedFromOtherTypes());
3049 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3050 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3051 << "' not instance of '" << res_method_class << "'";
3052 return NULL;
3053 }
3054 }
3055 /*
3056 * Process the target method's signature. This signature may or may not
3057 * have been verified, so we can't assume it's properly formed.
3058 */
3059 MethodHelper mh(res_method);
3060 const DexFile::TypeList* params = mh.GetParameterTypeList();
3061 size_t params_size = params == NULL ? 0 : params->Size();
3062 uint32_t arg[5];
3063 if (!is_range) {
3064 inst->GetArgs(arg);
3065 }
3066 size_t actual_args = 1;
3067 for (size_t param_index = 0; param_index < params_size; param_index++) {
3068 if (actual_args >= expected_args) {
3069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3070 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3071 << " (where longs/doubles count twice).";
3072 return NULL;
3073 }
3074 const char* descriptor =
3075 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3076 if (descriptor == NULL) {
3077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003078 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003079 return NULL;
3080 }
3081 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3082 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3083 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3084 return res_method;
3085 }
3086 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3087 }
3088 if (actual_args != expected_args) {
3089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3090 << " expected " << expected_args << " arguments, found " << actual_args;
3091 return NULL;
3092 } else {
3093 return res_method;
3094 }
3095}
3096
Ian Rogers62342ec2013-06-11 10:26:37 -07003097void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003098 uint32_t type_idx;
3099 if (!is_filled) {
3100 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3101 type_idx = inst->VRegC_22c();
3102 } else if (!is_range) {
3103 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3104 type_idx = inst->VRegB_35c();
3105 } else {
3106 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3107 type_idx = inst->VRegB_3rc();
3108 }
3109 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003110 if (res_type.IsConflict()) { // bad class
3111 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003112 } else {
3113 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3114 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003116 } else if (!is_filled) {
3117 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003118 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003119 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003120 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3121 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003122 } else {
3123 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3124 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003125 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003126 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3127 uint32_t arg[5];
3128 if (!is_range) {
3129 inst->GetArgs(arg);
3130 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003131 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003132 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003133 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003134 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003135 return;
3136 }
3137 }
3138 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003139 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3140 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003141 }
3142 }
3143}
3144
Sebastien Hertz5243e912013-05-21 10:55:07 +02003145void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003146 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003147 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003148 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003151 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003152 if (array_type.IsZero()) {
3153 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3154 // instruction type. TODO: have a proper notion of bottom here.
3155 if (!is_primitive || insn_type.IsCategory1Types()) {
3156 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003157 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003159 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003160 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003161 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003162 }
jeffhaofc3144e2012-02-01 17:21:15 -08003163 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003165 } else {
3166 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003167 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003168 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003170 << " source for aget-object";
3171 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003173 << " source for category 1 aget";
3174 } else if (is_primitive && !insn_type.Equals(component_type) &&
3175 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003176 (insn_type.IsLong() && component_type.IsDouble()))) {
3177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3178 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003179 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003180 // Use knowledge of the field type which is stronger than the type inferred from the
3181 // instruction, which can't differentiate object types and ints from floats, longs from
3182 // doubles.
3183 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003184 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003185 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003186 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003187 component_type.HighHalf(&reg_types_));
3188 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 }
3190 }
3191 }
3192}
3193
Sebastien Hertz5243e912013-05-21 10:55:07 +02003194void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003196 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003197 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003200 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003201 if (array_type.IsZero()) {
3202 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3203 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003204 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003206 } else {
3207 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003208 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003209 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003211 << " source for aput-object";
3212 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003214 << " source for category 1 aput";
3215 } else if (is_primitive && !insn_type.Equals(component_type) &&
3216 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3217 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003219 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003221 // The instruction agrees with the type of array, confirm the value to be stored does too
3222 // Note: we use the instruction type (rather than the component type) for aput-object as
3223 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003224 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 }
3226 }
3227 }
3228}
3229
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003230mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003231 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3232 // Check access to class
3233 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003234 if (klass_type.IsConflict()) { // bad class
3235 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3236 field_idx, dex_file_->GetFieldName(field_id),
3237 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003238 return NULL;
3239 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003240 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003241 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003242 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003243 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003246 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003247 << dex_file_->GetFieldName(field_id) << ") in "
3248 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 DCHECK(Thread::Current()->IsExceptionPending());
3250 Thread::Current()->ClearException();
3251 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003252 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3253 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003255 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 return NULL;
3257 } else if (!field->IsStatic()) {
3258 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3259 return NULL;
3260 } else {
3261 return field;
3262 }
3263}
3264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003265mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003266 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3267 // Check access to class
3268 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003269 if (klass_type.IsConflict()) {
3270 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3271 field_idx, dex_file_->GetFieldName(field_id),
3272 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003273 return NULL;
3274 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003275 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003276 return NULL; // Can't resolve Class so no more to do here
3277 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003278 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003279 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003281 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003282 << dex_file_->GetFieldName(field_id) << ") in "
3283 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 DCHECK(Thread::Current()->IsExceptionPending());
3285 Thread::Current()->ClearException();
3286 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003287 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3288 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003290 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 return NULL;
3292 } else if (field->IsStatic()) {
3293 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3294 << " to not be static";
3295 return NULL;
3296 } else if (obj_type.IsZero()) {
3297 // Cannot infer and check type, however, access will cause null pointer exception
3298 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003299 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003300 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003301 const RegType& field_klass =
3302 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003303 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003304 if (obj_type.IsUninitializedTypes() &&
3305 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3306 !field_klass.Equals(GetDeclaringClass()))) {
3307 // Field accesses through uninitialized references are only allowable for constructors where
3308 // the field is declared in this class
3309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3310 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003311 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003312 return NULL;
3313 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3314 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3315 // of C1. For resolution to occur the declared class of the field must be compatible with
3316 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3317 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3318 << " from object of type " << obj_type;
3319 return NULL;
3320 } else {
3321 return field;
3322 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 }
3324}
3325
Sebastien Hertz5243e912013-05-21 10:55:07 +02003326void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3327 bool is_primitive, bool is_static) {
3328 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003329 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003330 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003331 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003332 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003333 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003334 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003335 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003336 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003337 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003338 if (field != NULL) {
3339 descriptor = FieldHelper(field).GetTypeDescriptor();
3340 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003341 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003342 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3343 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3344 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003345 }
Ian Rogersb4903572012-10-11 11:52:56 -07003346 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003347 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003348 if (is_primitive) {
3349 if (field_type.Equals(insn_type) ||
3350 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3351 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3352 // expected that read is of the correct primitive type or that int reads are reading
3353 // floats or long reads are reading doubles
3354 } else {
3355 // This is a global failure rather than a class change failure as the instructions and
3356 // the descriptors for the type should have been consistent within the same file at
3357 // compile time
3358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3359 << " to be of type '" << insn_type
3360 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003361 return;
3362 }
3363 } else {
3364 if (!insn_type.IsAssignableFrom(field_type)) {
3365 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3366 << " to be compatible with type '" << insn_type
3367 << "' but found type '" << field_type
3368 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003369 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003370 return;
3371 }
3372 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003373 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003374 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003375 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003376 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003377 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003378}
3379
Sebastien Hertz5243e912013-05-21 10:55:07 +02003380void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3381 bool is_primitive, bool is_static) {
3382 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003383 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003384 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003385 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003386 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003387 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003388 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003389 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003390 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003391 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003392 if (field != NULL) {
3393 descriptor = FieldHelper(field).GetTypeDescriptor();
3394 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003395 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003396 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3397 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3398 loader = class_loader_;
3399 }
Ian Rogersb4903572012-10-11 11:52:56 -07003400 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003401 if (field != NULL) {
3402 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3403 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3404 << " from other class " << GetDeclaringClass();
3405 return;
3406 }
3407 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003408 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003409 if (is_primitive) {
3410 // Primitive field assignability rules are weaker than regular assignability rules
3411 bool instruction_compatible;
3412 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003413 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003414 if (field_type.IsIntegralTypes()) {
3415 instruction_compatible = insn_type.IsIntegralTypes();
3416 value_compatible = value_type.IsIntegralTypes();
3417 } else if (field_type.IsFloat()) {
3418 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3419 value_compatible = value_type.IsFloatTypes();
3420 } else if (field_type.IsLong()) {
3421 instruction_compatible = insn_type.IsLong();
3422 value_compatible = value_type.IsLongTypes();
3423 } else if (field_type.IsDouble()) {
3424 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3425 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003426 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003427 instruction_compatible = false; // reference field with primitive store
3428 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003430 if (!instruction_compatible) {
3431 // This is a global failure rather than a class change failure as the instructions and
3432 // the descriptors for the type should have been consistent within the same file at
3433 // compile time
3434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3435 << " to be of type '" << insn_type
3436 << "' but found type '" << field_type
3437 << "' in put";
3438 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003439 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003440 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003442 << " of type " << value_type
3443 << " but expected " << field_type
3444 << " for store to " << PrettyField(field) << " in put";
3445 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 } else {
3448 if (!insn_type.IsAssignableFrom(field_type)) {
3449 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3450 << " to be compatible with type '" << insn_type
3451 << "' but found type '" << field_type
3452 << "' in put-object";
3453 return;
3454 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003455 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 }
3457}
3458
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003459// Look for an instance field with this offset.
3460// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003461static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003462 uint32_t field_offset)
3463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003464 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003465 if (instance_fields != NULL) {
3466 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3467 mirror::Field* field = instance_fields->Get(i);
3468 if (field->GetOffset().Uint32Value() == field_offset) {
3469 return field;
3470 }
3471 }
3472 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003473 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003474 if (klass->GetSuperClass() != NULL) {
3475 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3476 } else {
3477 return NULL;
3478 }
3479}
3480
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003481// Returns the access field of a quick field access (iget/iput-quick) or NULL
3482// if it cannot be found.
3483mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3484 RegisterLine* reg_line) {
3485 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3486 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3487 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3488 inst->Opcode() == Instruction::IPUT_QUICK ||
3489 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3490 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3491 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003492 mirror::Class* object_class = NULL;
3493 if (!object_type.IsUnresolvedTypes()) {
3494 object_class = object_type.GetClass();
3495 } else {
3496 // We need to resolve the class from its descriptor.
3497 const std::string& descriptor(object_type.GetDescriptor());
3498 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3499 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3500 if (object_class == NULL) {
3501 Thread::Current()->ClearException();
3502 // Look for a system class
3503 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3504 }
3505 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003506 if (object_class == NULL) {
3507 // Failed to get the Class* from reg type.
3508 LOG(WARNING) << "Failed to get Class* from " << object_type;
3509 return NULL;
3510 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003511 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003512 return FindInstanceFieldWithOffset(object_class, field_offset);
3513}
3514
3515void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3516 bool is_primitive) {
3517 DCHECK(Runtime::Current()->IsStarted());
3518 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3519 if (field == NULL) {
3520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3521 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003522 }
3523 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3524 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3525 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3526 const uint32_t vregA = inst->VRegA_22c();
3527 if (is_primitive) {
3528 if (field_type.Equals(insn_type) ||
3529 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3530 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3531 // expected that read is of the correct primitive type or that int reads are reading
3532 // floats or long reads are reading doubles
3533 } else {
3534 // This is a global failure rather than a class change failure as the instructions and
3535 // the descriptors for the type should have been consistent within the same file at
3536 // compile time
3537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003538 << " to be of type '" << insn_type
3539 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003540 return;
3541 }
3542 } else {
3543 if (!insn_type.IsAssignableFrom(field_type)) {
3544 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003545 << " to be compatible with type '" << insn_type
3546 << "' but found type '" << field_type
3547 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003548 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3549 return;
3550 }
3551 }
3552 if (!field_type.IsLowHalf()) {
3553 work_line_->SetRegisterType(vregA, field_type);
3554 } else {
3555 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3556 }
3557}
3558
3559void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3560 bool is_primitive) {
3561 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003562 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3563 if (field == NULL) {
3564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3565 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003566 }
3567 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3568 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3569 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3570 if (field != NULL) {
3571 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3572 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003573 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003574 return;
3575 }
3576 }
3577 const uint32_t vregA = inst->VRegA_22c();
3578 if (is_primitive) {
3579 // Primitive field assignability rules are weaker than regular assignability rules
3580 bool instruction_compatible;
3581 bool value_compatible;
3582 const RegType& value_type = work_line_->GetRegisterType(vregA);
3583 if (field_type.IsIntegralTypes()) {
3584 instruction_compatible = insn_type.IsIntegralTypes();
3585 value_compatible = value_type.IsIntegralTypes();
3586 } else if (field_type.IsFloat()) {
3587 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3588 value_compatible = value_type.IsFloatTypes();
3589 } else if (field_type.IsLong()) {
3590 instruction_compatible = insn_type.IsLong();
3591 value_compatible = value_type.IsLongTypes();
3592 } else if (field_type.IsDouble()) {
3593 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3594 value_compatible = value_type.IsDoubleTypes();
3595 } else {
3596 instruction_compatible = false; // reference field with primitive store
3597 value_compatible = false; // unused
3598 }
3599 if (!instruction_compatible) {
3600 // This is a global failure rather than a class change failure as the instructions and
3601 // the descriptors for the type should have been consistent within the same file at
3602 // compile time
3603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003604 << " to be of type '" << insn_type
3605 << "' but found type '" << field_type
3606 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003607 return;
3608 }
3609 if (!value_compatible) {
3610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3611 << " of type " << value_type
3612 << " but expected " << field_type
3613 << " for store to " << PrettyField(field) << " in put";
3614 return;
3615 }
3616 } else {
3617 if (!insn_type.IsAssignableFrom(field_type)) {
3618 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003619 << " to be compatible with type '" << insn_type
3620 << "' but found type '" << field_type
3621 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003622 return;
3623 }
3624 work_line_->VerifyRegisterType(vregA, field_type);
3625 }
3626}
3627
Ian Rogers776ac1f2012-04-13 23:36:36 -07003628bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003629 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 return false;
3632 }
3633 return true;
3634}
3635
Ian Rogers776ac1f2012-04-13 23:36:36 -07003636bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003637 bool changed = true;
3638 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3639 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003640 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003641 * We haven't processed this instruction before, and we haven't touched the registers here, so
3642 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3643 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003644 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003645 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003646 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003647 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3648 if (gDebugVerify) {
3649 copy->CopyFromLine(target_line);
3650 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003651 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003652 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003654 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003655 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003656 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003657 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3658 << *copy.get() << " MERGE\n"
3659 << *merge_line << " ==\n"
3660 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003661 }
3662 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 if (changed) {
3664 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003665 }
3666 return true;
3667}
3668
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003669InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003670 return &insn_flags_[work_insn_idx_];
3671}
3672
Ian Rogersad0b3a32012-04-16 14:50:24 -07003673const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003674 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003675 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3676 uint16_t return_type_idx = proto_id.return_type_idx_;
3677 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003678 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003679}
3680
3681const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003682 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003683 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003684 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003685 if (mirror_method_ != NULL) {
3686 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003687 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3688 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003689 } else {
3690 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3691 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003692 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003693 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003694}
3695
Ian Rogers776ac1f2012-04-13 23:36:36 -07003696void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 size_t* log2_max_gc_pc) {
3698 size_t local_gc_points = 0;
3699 size_t max_insn = 0;
3700 size_t max_ref_reg = -1;
3701 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003702 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 local_gc_points++;
3704 max_insn = i;
3705 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003706 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003707 }
3708 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003709 *gc_points = local_gc_points;
3710 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3711 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003712 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003713 i++;
3714 }
3715 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003716}
3717
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003718MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3719 /*
3720 * Walks over the method code and adds any cast instructions in which
3721 * the type cast is implicit to a set, which is used in the code generation
3722 * to elide these casts.
3723 */
3724 if (!failure_messages_.empty()) {
3725 return NULL;
3726 }
3727 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003728 const Instruction* inst = Instruction::At(code_item_->insns_);
3729 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003730 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003731
3732 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003733 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003734 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003735 }
3736 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003737 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003738 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3739 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003740 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003741 if (mscs.get() == NULL) {
3742 mscs.reset(new MethodSafeCastSet());
3743 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003744 mscs->insert(dex_pc);
3745 }
3746 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003747 return mscs.release();
3748}
3749
Ian Rogersd0583802013-06-01 10:51:46 -07003750MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003751
3752 // It is risky to rely on reg_types for sharpening in cases of soft
3753 // verification, we might end up sharpening to a wrong implementation. Just abort.
3754 if (!failure_messages_.empty()) {
3755 return NULL;
3756 }
3757
Ian Rogersd0583802013-06-01 10:51:46 -07003758 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003759 const uint16_t* insns = code_item_->insns_ ;
3760 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003761 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003762
Ian Rogersd0583802013-06-01 10:51:46 -07003763 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003764 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3765 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3766 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3767 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3768
Ian Rogersd0583802013-06-01 10:51:46 -07003769 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003770 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003771 }
Ian Rogersd0583802013-06-01 10:51:46 -07003772 // Get reg type for register holding the reference to the object that will be dispatched upon.
3773 uint32_t dex_pc = inst->GetDexPc(insns);
3774 RegisterLine* line = reg_table_.GetLine(dex_pc);
3775 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3776 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3777 const RegType&
3778 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003779
Ian Rogersd0583802013-06-01 10:51:46 -07003780 if (!reg_type.HasClass()) {
3781 // We will compute devirtualization information only when we know the Class of the reg type.
3782 continue;
3783 }
3784 mirror::Class* reg_class = reg_type.GetClass();
3785 if (reg_class->IsInterface()) {
3786 // We can't devirtualize when the known type of the register is an interface.
3787 continue;
3788 }
3789 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3790 // We can't devirtualize abstract classes except on arrays of abstract classes.
3791 continue;
3792 }
3793 mirror::AbstractMethod* abstract_method =
3794 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003795 if(abstract_method == NULL) {
3796 // If the method is not found in the cache this means that it was never found
3797 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3798 continue;
3799 }
3800 // Find the concrete method.
3801 mirror::AbstractMethod* concrete_method = NULL;
3802 if (is_interface) {
3803 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3804 }
3805 if (is_virtual) {
3806 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3807 }
Ian Rogersd0583802013-06-01 10:51:46 -07003808 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3809 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003810 continue;
3811 }
Ian Rogersd0583802013-06-01 10:51:46 -07003812 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3813 concrete_method->GetDeclaringClass()->IsFinal()) {
3814 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3815 // overridden record the target to be used in the compiler driver.
3816 if (pc_to_concrete_method_map.get() == NULL) {
3817 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3818 }
3819 CompilerDriver::MethodReference concrete_ref(
3820 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3821 concrete_method->GetDexMethodIndex());
3822 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3823 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003824 }
Ian Rogersd0583802013-06-01 10:51:46 -07003825 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003826}
3827
Ian Rogers776ac1f2012-04-13 23:36:36 -07003828const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 size_t num_entries, ref_bitmap_bits, pc_bits;
3830 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3831 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003832 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003836 return NULL;
3837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3839 // There are 2 bytes to encode the number of entries
3840 if (num_entries >= 65536) {
3841 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003844 return NULL;
3845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003846 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003847 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003848 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003849 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003851 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003852 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003854 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3858 return NULL;
3859 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003860 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003861 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003864 return NULL;
3865 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003866 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003867 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003868 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3869 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003870 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003871 table->push_back(num_entries & 0xFF);
3872 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003875 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003876 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003878 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 }
3880 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003881 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003882 }
3883 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003884 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003885 return table;
3886}
jeffhaoa0a764a2011-09-16 10:43:38 -07003887
Ian Rogers776ac1f2012-04-13 23:36:36 -07003888void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3890 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003891 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003892 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003893 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003894 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003895 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003897 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3899 map_index++;
3900 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003901 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003902 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003903 CHECK_LT(j / 8, map.RegWidth());
3904 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3905 } else if ((j / 8) < map.RegWidth()) {
3906 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3907 } else {
3908 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3909 }
3910 }
3911 } else {
3912 CHECK(reg_bitmap == NULL);
3913 }
3914 }
3915}
jeffhaoa0a764a2011-09-16 10:43:38 -07003916
Ian Rogers637c65b2013-05-31 11:46:00 -07003917void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3918 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003919 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003920 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003921 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3922 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003923 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003924 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003925 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003926 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003927 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003928 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003929}
3930
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003931
Ian Rogersfae370a2013-06-05 08:33:27 -07003932void MethodVerifier::SetSafeCastMap(CompilerDriver::MethodReference ref,
3933 const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003934 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3935 SafeCastMap::iterator it = safecast_map_->find(ref);
3936 if (it != safecast_map_->end()) {
3937 delete it->second;
3938 safecast_map_->erase(it);
3939 }
3940
3941 safecast_map_->Put(ref, cast_set);
3942 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3943}
3944
3945bool MethodVerifier::IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) {
3946 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3947 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3948 if (it == safecast_map_->end()) {
3949 return false;
3950 }
3951
3952 // Look up the cast address in the set of safe casts
3953 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3954 return cast_it != it->second->end();
3955}
3956
Ian Rogers637c65b2013-05-31 11:46:00 -07003957const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3958 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3959 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3960 if (it == dex_gc_maps_->end()) {
3961 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3962 return NULL;
3963 }
3964 CHECK(it->second != NULL);
3965 return it->second;
3966}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003967
Ian Rogers637c65b2013-05-31 11:46:00 -07003968void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003969 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003970 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003971 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3972 if (it != devirt_maps_->end()) {
3973 delete it->second;
3974 devirt_maps_->erase(it);
3975 }
3976
3977 devirt_maps_->Put(ref, devirt_map);
3978 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3979}
3980
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003981const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3982 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003983 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003984 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3985 if (it == devirt_maps_->end()) {
3986 return NULL;
3987 }
3988
3989 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003990 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003991 if(pc_to_concrete_method != it->second->end()) {
3992 return &(pc_to_concrete_method->second);
3993 } else {
3994 return NULL;
3995 }
3996}
3997
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003998std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3999 RegisterLine* line = reg_table_.GetLine(dex_pc);
4000 std::vector<int32_t> result;
4001 for (size_t i = 0; i < line->NumRegs(); ++i) {
4002 const RegType& type = line->GetRegisterType(i);
4003 if (type.IsConstant()) {
4004 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4005 result.push_back(type.ConstantValue());
4006 } else if (type.IsConstantLo()) {
4007 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4008 result.push_back(type.ConstantValueLo());
4009 } else if (type.IsConstantHi()) {
4010 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4011 result.push_back(type.ConstantValueHi());
4012 } else if (type.IsIntegralTypes()) {
4013 result.push_back(kIntVReg);
4014 result.push_back(0);
4015 } else if (type.IsFloat()) {
4016 result.push_back(kFloatVReg);
4017 result.push_back(0);
4018 } else if (type.IsLong()) {
4019 result.push_back(kLongLoVReg);
4020 result.push_back(0);
4021 result.push_back(kLongHiVReg);
4022 result.push_back(0);
4023 ++i;
4024 } else if (type.IsDouble()) {
4025 result.push_back(kDoubleLoVReg);
4026 result.push_back(0);
4027 result.push_back(kDoubleHiVReg);
4028 result.push_back(0);
4029 ++i;
4030 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4031 result.push_back(kUndefined);
4032 result.push_back(0);
4033 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004034 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004035 result.push_back(kReferenceVReg);
4036 result.push_back(0);
4037 }
4038 }
4039 return result;
4040}
4041
Ian Rogers637c65b2013-05-31 11:46:00 -07004042ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004043MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004044
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004045Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4046MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4047
Ian Rogers637c65b2013-05-31 11:46:00 -07004048ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004049MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4050
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004051Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4052MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4053
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004054void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004055 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07004056 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004057 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004058 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004059 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004060 }
4061
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004062 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4063 {
4064 MutexLock mu(self, *safecast_map_lock_);
4065 safecast_map_ = new MethodVerifier::SafeCastMap();
4066 }
4067
Ian Rogers637c65b2013-05-31 11:46:00 -07004068 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004069
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004070 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004071 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004072 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4073 }
4074
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004075 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4076 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004077 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004078 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4079 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004080 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004081}
4082
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004083void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07004084 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004085 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004086 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004087 STLDeleteValues(dex_gc_maps_);
4088 delete dex_gc_maps_;
4089 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004090 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004091 delete dex_gc_maps_lock_;
4092 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004093
4094 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004095 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004096 STLDeleteValues(devirt_maps_);
4097 delete devirt_maps_;
4098 devirt_maps_ = NULL;
4099 }
4100 delete devirt_maps_lock_;
4101 devirt_maps_lock_ = NULL;
4102
4103 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004104 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004105 delete rejected_classes_;
4106 rejected_classes_ = NULL;
4107 }
4108 delete rejected_classes_lock_;
4109 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004110 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004111}
jeffhaod1224c72012-02-29 13:43:08 -08004112
Ian Rogers1212a022013-03-04 10:48:41 -08004113void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004114 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004115 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004116 rejected_classes_->insert(ref);
4117 }
jeffhaod1224c72012-02-29 13:43:08 -08004118 CHECK(IsClassRejected(ref));
4119}
4120
Ian Rogers1212a022013-03-04 10:48:41 -08004121bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07004122 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004123 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004124}
4125
Ian Rogersd81871c2011-10-03 13:57:23 -07004126} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004127} // namespace art