blob: 2798ab3fa78d695775b9653a738e693e34278831 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "gc/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
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700312 // If there aren't any instructions, make sure that's expected, then exit successfully.
313 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700316 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 } else {
318 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700319 }
jeffhaobdb76512011-09-07 11:43:16 -0700320 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700321 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
322 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
324 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700326 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800328 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 // Run through the instructions and see if the width checks out.
330 bool result = ComputeWidthsAndCountOps();
331 // Flag instructions guarded by a "try" block and check exception handlers.
332 result = result && ScanTryCatchBlocks();
333 // Perform static instruction verification.
334 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 // Perform code-flow analysis and return.
336 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700337}
338
Ian Rogers776ac1f2012-04-13 23:36:36 -0700339std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700340 switch (error) {
341 case VERIFY_ERROR_NO_CLASS:
342 case VERIFY_ERROR_NO_FIELD:
343 case VERIFY_ERROR_NO_METHOD:
344 case VERIFY_ERROR_ACCESS_CLASS:
345 case VERIFY_ERROR_ACCESS_FIELD:
346 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700347 case VERIFY_ERROR_INSTANTIATION:
348 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800349 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700350 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
351 // class change and instantiation errors into soft verification errors so that we re-verify
352 // at runtime. We may fail to find or to agree on access because of not yet available class
353 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
354 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
355 // paths" that dynamically perform the verification and cause the behavior to be that akin
356 // to an interpreter.
357 error = VERIFY_ERROR_BAD_CLASS_SOFT;
358 } else {
359 have_pending_runtime_throw_failure_ = true;
360 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700362 // Indication that verification should be retried at runtime.
363 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700364 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 have_pending_hard_failure_ = true;
366 }
367 break;
jeffhaod5347e02012-03-22 17:25:05 -0700368 // Hard verification failures at compile time will still fail at runtime, so the class is
369 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370 case VERIFY_ERROR_BAD_CLASS_HARD: {
371 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800372 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800373 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700375 have_pending_hard_failure_ = true;
376 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800377 }
378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800380 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700381 work_insn_idx_));
382 std::ostringstream* failure_message = new std::ostringstream(location);
383 failure_messages_.push_back(failure_message);
384 return *failure_message;
385}
386
387void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
388 size_t failure_num = failure_messages_.size();
389 DCHECK_NE(failure_num, 0U);
390 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
391 prepend += last_fail_message->str();
392 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
393 delete last_fail_message;
394}
395
396void MethodVerifier::AppendToLastFailMessage(std::string append) {
397 size_t failure_num = failure_messages_.size();
398 DCHECK_NE(failure_num, 0U);
399 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
400 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800401}
402
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 const uint16_t* insns = code_item_->insns_;
405 size_t insns_size = code_item_->insns_size_in_code_units_;
406 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700407 size_t new_instance_count = 0;
408 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700410
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700412 Instruction::Code opcode = inst->Opcode();
413 if (opcode == Instruction::NEW_INSTANCE) {
414 new_instance_count++;
415 } else if (opcode == Instruction::MONITOR_ENTER) {
416 monitor_enter_count++;
417 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 size_t inst_size = inst->SizeInCodeUnits();
419 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
420 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700421 inst = inst->Next();
422 }
423
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
426 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700427 return false;
428 }
429
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 new_instance_count_ = new_instance_count;
431 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700432 return true;
433}
434
Ian Rogers776ac1f2012-04-13 23:36:36 -0700435bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700437 if (tries_size == 0) {
438 return true;
439 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700441 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700442
443 for (uint32_t idx = 0; idx < tries_size; idx++) {
444 const DexFile::TryItem* try_item = &tries[idx];
445 uint32_t start = try_item->start_addr_;
446 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700447 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
449 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700450 return false;
451 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700454 return false;
455 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700456 for (uint32_t dex_pc = start; dex_pc < end;
457 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
458 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700459 }
460 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800461 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700462 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700463 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700464 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700465 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700466 CatchHandlerIterator iterator(handlers_ptr);
467 for (; iterator.HasNext(); iterator.Next()) {
468 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700469 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700471 return false;
472 }
jeffhao60f83e32012-02-13 17:16:30 -0800473 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
474 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800477 return false;
478 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700480 // Ensure exception types are resolved so that they don't need resolution to be delivered,
481 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700482 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
484 iterator.GetHandlerTypeIndex(),
485 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700486 if (exception_type == NULL) {
487 DCHECK(Thread::Current()->IsExceptionPending());
488 Thread::Current()->ClearException();
489 }
490 }
jeffhaobdb76512011-09-07 11:43:16 -0700491 }
Ian Rogers0571d352011-11-03 19:51:38 -0700492 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700493 }
jeffhaobdb76512011-09-07 11:43:16 -0700494 return true;
495}
496
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700499
Ian Rogers0c7abda2012-09-19 13:33:42 -0700500 /* 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 -0700501 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700502 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700503
504 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700505 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 return false;
509 }
510 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700511 // All invoke points are marked as "Throw" points already.
512 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700514 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 }
516 dex_pc += inst->SizeInCodeUnits();
517 inst = inst->Next();
518 }
519 return true;
520}
521
Ian Rogers776ac1f2012-04-13 23:36:36 -0700522bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800523 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 bool result = true;
525 switch (inst->GetVerifyTypeArgumentA()) {
526 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 break;
529 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800530 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 break;
532 }
533 switch (inst->GetVerifyTypeArgumentB()) {
534 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800535 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 break;
537 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800538 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 break;
540 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800541 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 break;
543 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800544 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 break;
546 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800547 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 break;
549 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 }
556 switch (inst->GetVerifyTypeArgumentC()) {
557 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800558 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 break;
560 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800561 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 break;
563 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800564 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 break;
566 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800567 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 break;
572 }
573 switch (inst->GetVerifyExtraFlags()) {
574 case Instruction::kVerifyArrayData:
575 result = result && CheckArrayData(code_offset);
576 break;
577 case Instruction::kVerifyBranchTarget:
578 result = result && CheckBranchTarget(code_offset);
579 break;
580 case Instruction::kVerifySwitchTargets:
581 result = result && CheckSwitchTargets(code_offset);
582 break;
583 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 result = false;
592 break;
593 }
594 return result;
595}
596
Ian Rogers776ac1f2012-04-13 23:36:36 -0700597bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
600 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 return false;
602 }
603 return true;
604}
605
Ian Rogers776ac1f2012-04-13 23:36:36 -0700606bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
609 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 return false;
611 }
612 return true;
613}
614
Ian Rogers776ac1f2012-04-13 23:36:36 -0700615bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
618 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 return false;
620 }
621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
627 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 return false;
629 }
630 return true;
631}
632
Ian Rogers776ac1f2012-04-13 23:36:36 -0700633bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
636 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 return false;
638 }
639 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700640 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 return false;
644 }
645 return true;
646}
647
Ian Rogers776ac1f2012-04-13 23:36:36 -0700648bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
651 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 return false;
653 }
654 return true;
655}
656
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
660 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700661 return false;
662 }
663 return true;
664}
665
Ian Rogers776ac1f2012-04-13 23:36:36 -0700666bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
669 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 return false;
671 }
672 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700673 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 const char* cp = descriptor;
675 while (*cp++ == '[') {
676 bracket_count++;
677 }
678 if (bracket_count == 0) {
679 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 return false;
682 } else if (bracket_count > 255) {
683 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
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::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
692 const uint16_t* insns = code_item_->insns_ + cur_offset;
693 const uint16_t* array_data;
694 int32_t array_data_offset;
695
696 DCHECK_LT(cur_offset, insn_count);
697 /* make sure the start of the array data table is in range */
698 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
699 if ((int32_t) cur_offset + array_data_offset < 0 ||
700 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
702 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 /* offset to array data table is a relative branch-style offset */
706 array_data = insns + array_data_offset;
707 /* make sure the table is 32-bit aligned */
708 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
710 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700714 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
716 /* make sure the end of the switch is in range */
717 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
719 << ", data offset " << array_data_offset << ", end "
720 << cur_offset + array_data_offset + table_size
721 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 return false;
723 }
724 return true;
725}
726
Ian Rogers776ac1f2012-04-13 23:36:36 -0700727bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 int32_t offset;
729 bool isConditional, selfOkay;
730 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
731 return false;
732 }
733 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700734 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 -0700735 return false;
736 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700737 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
738 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
744 int32_t abs_offset = cur_offset + offset;
745 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700747 << reinterpret_cast<void*>(abs_offset) << ") at "
748 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 insn_flags_[abs_offset].SetBranchTarget();
752 return true;
753}
754
Ian Rogers776ac1f2012-04-13 23:36:36 -0700755bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 bool* selfOkay) {
757 const uint16_t* insns = code_item_->insns_ + cur_offset;
758 *pConditional = false;
759 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700760 switch (*insns & 0xff) {
761 case Instruction::GOTO:
762 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700763 break;
764 case Instruction::GOTO_32:
765 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700766 *selfOkay = true;
767 break;
768 case Instruction::GOTO_16:
769 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700770 break;
771 case Instruction::IF_EQ:
772 case Instruction::IF_NE:
773 case Instruction::IF_LT:
774 case Instruction::IF_GE:
775 case Instruction::IF_GT:
776 case Instruction::IF_LE:
777 case Instruction::IF_EQZ:
778 case Instruction::IF_NEZ:
779 case Instruction::IF_LTZ:
780 case Instruction::IF_GEZ:
781 case Instruction::IF_GTZ:
782 case Instruction::IF_LEZ:
783 *pOffset = (int16_t) insns[1];
784 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700785 break;
786 default:
787 return false;
788 break;
789 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700795 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700797 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
799 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
801 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 return false;
803 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700806 /* make sure the table is 32-bit aligned */
807 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
809 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 return false;
811 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 uint32_t switch_count = switch_insns[1];
813 int32_t keys_offset, targets_offset;
814 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700815 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
816 /* 0=sig, 1=count, 2/3=firstKey */
817 targets_offset = 4;
818 keys_offset = -1;
819 expected_signature = Instruction::kPackedSwitchSignature;
820 } else {
821 /* 0=sig, 1=count, 2..count*2 = keys */
822 keys_offset = 2;
823 targets_offset = 2 + 2 * switch_count;
824 expected_signature = Instruction::kSparseSwitchSignature;
825 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
829 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 return false;
831 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 /* make sure the end of the switch is in range */
833 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
835 << switch_offset << ", end "
836 << (cur_offset + switch_offset + table_size)
837 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 return false;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 /* for a sparse switch, verify the keys are in ascending order */
841 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
843 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700844 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
845 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
846 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
848 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 return false;
850 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 last_key = key;
852 }
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 for (uint32_t targ = 0; targ < switch_count; targ++) {
856 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
857 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
858 int32_t abs_offset = cur_offset + offset;
859 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700861 << reinterpret_cast<void*>(abs_offset) << ") at "
862 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 insn_flags_[abs_offset].SetBranchTarget();
866 }
867 return true;
868}
869
Ian Rogers776ac1f2012-04-13 23:36:36 -0700870bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
874 }
875 uint16_t registers_size = code_item_->registers_size_;
876 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800877 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
879 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 }
883
884 return true;
885}
886
Ian Rogers776ac1f2012-04-13 23:36:36 -0700887bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 uint16_t registers_size = code_item_->registers_size_;
889 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
890 // integer overflow when adding them here.
891 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
893 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return true;
897}
898
Ian Rogers0c7abda2012-09-19 13:33:42 -0700899static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800900 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700901 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800902 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
903 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
904 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
905 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
906 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
907 gc_map.begin(),
908 gc_map.end());
909 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
910 DCHECK_EQ(gc_map.size(),
911 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
912 (length_prefixed_gc_map->at(1) << 16) |
913 (length_prefixed_gc_map->at(2) << 8) |
914 (length_prefixed_gc_map->at(3) << 0)));
915 return length_prefixed_gc_map;
916}
917
Ian Rogers776ac1f2012-04-13 23:36:36 -0700918bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 uint16_t registers_size = code_item_->registers_size_;
920 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700921
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800923 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
924 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 }
926 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700927 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
928
jeffhaobdb76512011-09-07 11:43:16 -0700929
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 work_line_.reset(new RegisterLine(registers_size, this));
931 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700932
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 /* Initialize register types of method arguments. */
934 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 DCHECK_NE(failures_.size(), 0U);
936 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800937 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 /* Perform code flow verification. */
942 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700943 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946
Ian Rogers1212a022013-03-04 10:48:41 -0800947 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700948
TDYa127b2eb5c12012-05-24 15:52:10 -0700949
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800951 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
952 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false; // Not a real failure, but a failure to encode
955 }
Ian Rogers39ebcb82013-05-30 16:57:23 -0700956 if (kIsDebugBuild) {
957 VerifyGcMap(*map);
958 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700959 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
960 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800961
Dragos Sbirlea980d16b2013-06-04 15:01:40 -0700962 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
963 if(method_to_safe_casts != NULL ) {
964 SetSafeCastMap(ref, method_to_safe_casts);
965 }
966
Ian Rogersd0583802013-06-01 10:51:46 -0700967 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700968 if(pc_to_concrete_method != NULL ) {
969 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700970 }
jeffhaobdb76512011-09-07 11:43:16 -0700971 return true;
972}
973
Ian Rogersad0b3a32012-04-16 14:50:24 -0700974std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
975 DCHECK_EQ(failures_.size(), failure_messages_.size());
976 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700977 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700978 }
979 return os;
980}
981
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700983 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700984 v->Dump(std::cerr);
985}
986
Ian Rogers776ac1f2012-04-13 23:36:36 -0700987void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800988 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700989 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 return;
jeffhaobdb76512011-09-07 11:43:16 -0700991 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800992 {
993 os << "Register Types:\n";
994 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
995 std::ostream indent_os(&indent_filter);
996 reg_types_.Dump(indent_os);
997 }
Ian Rogersb4903572012-10-11 11:52:56 -0700998 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800999 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1000 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 const Instruction* inst = Instruction::At(code_item_->insns_);
1002 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1003 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1005 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001006 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001007 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001008 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001009 const bool kDumpHexOfInstruction = false;
1010 if (kDumpHexOfInstruction) {
1011 indent_os << inst->DumpHex(5) << " ";
1012 }
1013 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001014 inst = inst->Next();
1015 }
jeffhaobdb76512011-09-07 11:43:16 -07001016}
1017
Ian Rogersd81871c2011-10-03 13:57:23 -07001018static bool IsPrimitiveDescriptor(char descriptor) {
1019 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001020 case 'I':
1021 case 'C':
1022 case 'S':
1023 case 'B':
1024 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001025 case 'F':
1026 case 'D':
1027 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001029 default:
1030 return false;
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 RegisterLine* reg_line = reg_table_.GetLine(0);
1036 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1037 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001038
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1040 //Include the "this" pointer.
1041 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1044 // argument as uninitialized. This restricts field access until the superclass constructor is
1045 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001046 const RegType& declaring_class = GetDeclaringClass();
1047 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 reg_line->SetRegisterType(arg_start + cur_arg,
1049 reg_types_.UninitializedThisArgument(declaring_class));
1050 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001051 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001054 }
1055
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001057 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001058 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001059
1060 for (; iterator.HasNext(); iterator.Next()) {
1061 const char* descriptor = iterator.GetDescriptor();
1062 if (descriptor == NULL) {
1063 LOG(FATAL) << "Null descriptor";
1064 }
1065 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1067 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 return false;
1069 }
1070 switch (descriptor[0]) {
1071 case 'L':
1072 case '[':
1073 // We assume that reference arguments are initialized. The only way it could be otherwise
1074 // (assuming the caller was verified) is if the current method is <init>, but in that case
1075 // it's effectively considered initialized the instant we reach here (in the sense that we
1076 // can return without doing anything or call virtual methods).
1077 {
Ian Rogersb4903572012-10-11 11:52:56 -07001078 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001079 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 }
1081 break;
1082 case 'Z':
1083 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1084 break;
1085 case 'C':
1086 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1087 break;
1088 case 'B':
1089 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1090 break;
1091 case 'I':
1092 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1093 break;
1094 case 'S':
1095 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1096 break;
1097 case 'F':
1098 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1099 break;
1100 case 'J':
1101 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1103 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1104 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 cur_arg++;
1106 break;
1107 }
1108 default:
jeffhaod5347e02012-03-22 17:25:05 -07001109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 return false;
1111 }
1112 cur_arg++;
1113 }
1114 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 return false;
1117 }
1118 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1119 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1120 // format. Only major difference from the method argument format is that 'V' is supported.
1121 bool result;
1122 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1123 result = descriptor[1] == '\0';
1124 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1125 size_t i = 0;
1126 do {
1127 i++;
1128 } while (descriptor[i] == '['); // process leading [
1129 if (descriptor[i] == 'L') { // object array
1130 do {
1131 i++; // find closing ;
1132 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1133 result = descriptor[i] == ';';
1134 } else { // primitive array
1135 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1136 }
1137 } else if (descriptor[0] == 'L') {
1138 // could be more thorough here, but shouldn't be required
1139 size_t i = 0;
1140 do {
1141 i++;
1142 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1143 result = descriptor[i] == ';';
1144 } else {
1145 result = false;
1146 }
1147 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1149 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 }
1151 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 const uint16_t* insns = code_item_->insns_;
1156 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001157
jeffhaobdb76512011-09-07 11:43:16 -07001158 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 insn_flags_[0].SetChanged();
1160 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001161
jeffhaobdb76512011-09-07 11:43:16 -07001162 /* Continue until no instructions are marked "changed". */
1163 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1165 uint32_t insn_idx = start_guess;
1166 for (; insn_idx < insns_size; insn_idx++) {
1167 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001168 break;
1169 }
jeffhaobdb76512011-09-07 11:43:16 -07001170 if (insn_idx == insns_size) {
1171 if (start_guess != 0) {
1172 /* try again, starting from the top */
1173 start_guess = 0;
1174 continue;
1175 } else {
1176 /* all flags are clear */
1177 break;
1178 }
1179 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 // We carry the working set of registers from instruction to instruction. If this address can
1181 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1182 // "changed" flags, we need to load the set of registers from the table.
1183 // Because we always prefer to continue on to the next instruction, we should never have a
1184 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1185 // target.
1186 work_insn_idx_ = insn_idx;
1187 if (insn_flags_[insn_idx].IsBranchTarget()) {
1188 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001189 } else {
1190#ifndef NDEBUG
1191 /*
1192 * Sanity check: retrieve the stored register line (assuming
1193 * a full table) and make sure it actually matches.
1194 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1196 if (register_line != NULL) {
1197 if (work_line_->CompareLine(register_line) != 0) {
1198 Dump(std::cout);
1199 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001201 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1202 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001203 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 }
jeffhaobdb76512011-09-07 11:43:16 -07001205 }
1206#endif
1207 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001210 prepend += " failed to verify: ";
1211 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 return false;
1213 }
jeffhaobdb76512011-09-07 11:43:16 -07001214 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 insn_flags_[insn_idx].SetVisited();
1216 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001217 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001218
Ian Rogers1c849e52012-06-28 14:00:33 -07001219 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001220 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001221 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001222 * (besides the wasted space), but it indicates a flaw somewhere
1223 * down the line, possibly in the verifier.
1224 *
1225 * If we've substituted "always throw" instructions into the stream,
1226 * we are almost certainly going to have some dead code.
1227 */
1228 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 uint32_t insn_idx = 0;
1230 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001231 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001232 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001233 * may or may not be preceded by a padding NOP (for alignment).
1234 */
1235 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1236 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1237 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001238 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001239 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1240 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1241 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001243 }
1244
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001246 if (dead_start < 0)
1247 dead_start = insn_idx;
1248 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001249 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001250 dead_start = -1;
1251 }
1252 }
1253 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001254 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001255 }
1256 }
jeffhaobdb76512011-09-07 11:43:16 -07001257 return true;
1258}
1259
Ian Rogers776ac1f2012-04-13 23:36:36 -07001260bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001261 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1262 // We want the state _before_ the instruction, for the case where the dex pc we're
1263 // interested in is itself a monitor-enter instruction (which is a likely place
1264 // for a thread to be suspended).
1265 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001266 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001267 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1268 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1269 }
1270 }
1271
jeffhaobdb76512011-09-07 11:43:16 -07001272 /*
1273 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001274 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001275 * control to another statement:
1276 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001277 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001278 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001279 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001280 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001281 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001282 * throw an exception that is handled by an encompassing "try"
1283 * block.
1284 *
1285 * We can also return, in which case there is no successor instruction
1286 * from this point.
1287 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001288 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001289 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1291 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001292 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001293
jeffhaobdb76512011-09-07 11:43:16 -07001294 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001295 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001296 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001298 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1299 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 }
jeffhaobdb76512011-09-07 11:43:16 -07001301
1302 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001303 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001304 * can throw an exception, we will copy/merge this into the "catch"
1305 * address rather than work_line, because we don't want the result
1306 * from the "successful" code path (e.g. a check-cast that "improves"
1307 * a type) to be visible to the exception handler.
1308 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001309 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001311 } else {
1312#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001314#endif
1315 }
1316
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001317
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001318 // We need to ensure the work line is consistent while performing validation. When we spot a
1319 // peephole pattern we compute a new line for either the fallthrough instruction or the
1320 // branch target.
1321 UniquePtr<RegisterLine> branch_line;
1322 UniquePtr<RegisterLine> fallthrough_line;
1323
Sebastien Hertz5243e912013-05-21 10:55:07 +02001324 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001325 case Instruction::NOP:
1326 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001327 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001328 * a signature that looks like a NOP; if we see one of these in
1329 * the course of executing code then we have a problem.
1330 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001331 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001333 }
1334 break;
1335
1336 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001337 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1338 break;
jeffhaobdb76512011-09-07 11:43:16 -07001339 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001340 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1341 break;
jeffhaobdb76512011-09-07 11:43:16 -07001342 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001343 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001344 break;
1345 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001346 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1347 break;
jeffhaobdb76512011-09-07 11:43:16 -07001348 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001349 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1350 break;
jeffhaobdb76512011-09-07 11:43:16 -07001351 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001352 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001353 break;
1354 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001355 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1356 break;
jeffhaobdb76512011-09-07 11:43:16 -07001357 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001358 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1359 break;
jeffhaobdb76512011-09-07 11:43:16 -07001360 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001361 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001362 break;
1363
1364 /*
1365 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001366 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001367 * might want to hold the result in an actual CPU register, so the
1368 * Dalvik spec requires that these only appear immediately after an
1369 * invoke or filled-new-array.
1370 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001371 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001372 * redundant with the reset done below, but it can make the debug info
1373 * easier to read in some cases.)
1374 */
1375 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001376 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001377 break;
1378 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001379 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001380 break;
1381 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001382 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001383 break;
1384
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001386 /*
jeffhao60f83e32012-02-13 17:16:30 -08001387 * This statement can only appear as the first instruction in an exception handler. We verify
1388 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001389 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001390 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001391 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001392 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 }
jeffhaobdb76512011-09-07 11:43:16 -07001394 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001395 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1396 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 }
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
1400 break;
1401 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001402 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001403 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 const RegType& return_type = GetMethodReturnType();
1405 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 } else {
1408 // Compilers may generate synthetic functions that write byte values into boolean fields.
1409 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001410 const uint32_t vregA = inst->VRegA_11x();
1411 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1413 ((return_type.IsBoolean() || return_type.IsByte() ||
1414 return_type.IsShort() || return_type.IsChar()) &&
1415 src_type.IsInteger()));
1416 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001417 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001418 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001419 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001420 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 }
jeffhaobdb76512011-09-07 11:43:16 -07001422 }
1423 }
1424 break;
1425 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001426 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001427 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 const RegType& return_type = GetMethodReturnType();
1429 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 } else {
1432 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001433 const uint32_t vregA = inst->VRegA_11x();
1434 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001435 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001436 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 }
jeffhaobdb76512011-09-07 11:43:16 -07001438 }
1439 }
1440 break;
1441 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001442 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 const RegType& return_type = GetMethodReturnType();
1444 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 } else {
1447 /* return_type is the *expected* return type, not register value */
1448 DCHECK(!return_type.IsZero());
1449 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001450 const uint32_t vregA = inst->VRegA_11x();
1451 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001452 // Disallow returning uninitialized values and verify that the reference in vAA is an
1453 // instance of the "return_type"
1454 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001456 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001457 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1458 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001459 }
1460 }
1461 }
1462 break;
1463
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001464 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001465 case Instruction::CONST_4: {
1466 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1467 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001468 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 }
1470 case Instruction::CONST_16: {
1471 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1472 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 }
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->SetRegisterType(inst->VRegA_31i(),
1477 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001478 break;
1479 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 work_line_->SetRegisterType(inst->VRegA_21h(),
1481 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
jeffhaobdb76512011-09-07 11:43:16 -07001483 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1487 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001489 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 }
1491 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1494 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 break;
1497 }
1498 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001500 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1501 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 break;
1504 }
1505 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001507 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1508 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001509 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001510 break;
1511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001513 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1514 break;
jeffhaobdb76512011-09-07 11:43:16 -07001515 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001516 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001517 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001519 // Get type from instruction if unresolved then we need an access check
1520 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001521 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001523 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001524 res_type.IsConflict() ? res_type
1525 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001526 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 }
jeffhaobdb76512011-09-07 11:43:16 -07001528 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001530 break;
1531 case Instruction::MONITOR_EXIT:
1532 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001533 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001534 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001535 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001536 * to the need to handle asynchronous exceptions, a now-deprecated
1537 * feature that Dalvik doesn't support.)
1538 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001539 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001540 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001541 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001542 * structured locking checks are working, the former would have
1543 * failed on the -enter instruction, and the latter is impossible.
1544 *
1545 * This is fortunate, because issue 3221411 prevents us from
1546 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001547 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001548 * some catch blocks (which will show up as "dead" code when
1549 * we skip them here); if we can't, then the code path could be
1550 * "live" so we still need to check it.
1551 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001553 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001554 break;
1555
Ian Rogers28ad40d2011-10-27 15:19:26 -07001556 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001558 /*
1559 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1560 * could be a "upcast" -- not expected, so we don't try to address it.)
1561 *
1562 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001563 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001564 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001565 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1566 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1567 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001568 if (res_type.IsConflict()) {
1569 DCHECK_NE(failures_.size(), 0U);
1570 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001572 }
1573 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001574 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001575 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001576 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1577 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001578 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 if (is_checkcast) {
1580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1581 } else {
1582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1583 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001584 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 if (is_checkcast) {
1586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1587 } else {
1588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1589 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001590 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001591 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001592 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
jeffhaobdb76512011-09-07 11:43:16 -07001596 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001597 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 }
1599 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001601 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001602 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 }
1607 }
1608 break;
1609 }
1610 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001611 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001612 if (res_type.IsConflict()) {
1613 DCHECK_NE(failures_.size(), 0U);
1614 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001615 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001616 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1617 // can't create an instance of an interface or abstract class */
1618 if (!res_type.IsInstantiableTypes()) {
1619 Fail(VERIFY_ERROR_INSTANTIATION)
1620 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001621 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001623 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1624 // Any registers holding previous allocations from this address that have not yet been
1625 // initialized must be marked invalid.
1626 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1627 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 break;
1630 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001631 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
1634 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001636 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001638 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001640 just_set_result = true; // Filled new array range sets result register
1641 break;
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::CMPL_FLOAT:
1643 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001645 break;
1646 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001648 break;
1649 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001651 break;
1652 case Instruction::CMPL_DOUBLE:
1653 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001656 break;
1657 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001660 break;
1661 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001663 break;
1664 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001667 break;
1668 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001669 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001670 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001671 break;
1672 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001676 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001677 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001678 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001679 }
1680 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 }
jeffhaobdb76512011-09-07 11:43:16 -07001682 case Instruction::GOTO:
1683 case Instruction::GOTO_16:
1684 case Instruction::GOTO_32:
1685 /* no effect on or use of registers */
1686 break;
1687
1688 case Instruction::PACKED_SWITCH:
1689 case Instruction::SPARSE_SWITCH:
1690 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
1693
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 case Instruction::FILL_ARRAY_DATA: {
1695 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001697 /* array_type can be null if the reg type is Zero */
1698 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001699 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001701 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001702 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1703 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001704 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1706 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 } else {
jeffhao457cc512012-02-02 16:55:13 -08001708 // Now verify if the element width in the table matches the element width declared in
1709 // the array
1710 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1711 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001713 } else {
1714 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1715 // Since we don't compress the data in Dex, expect to see equal width of data stored
1716 // in the table and expected from the array class.
1717 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1719 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001720 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 }
1722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 }
1724 }
1725 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 }
jeffhaobdb76512011-09-07 11:43:16 -07001727 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1730 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 bool mismatch = false;
1732 if (reg_type1.IsZero()) { // zero then integral or reference expected
1733 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1734 } else if (reg_type1.IsReferenceTypes()) { // both references?
1735 mismatch = !reg_type2.IsReferenceTypes();
1736 } else { // both integral?
1737 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1738 }
1739 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1741 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001742 }
1743 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 }
jeffhaobdb76512011-09-07 11:43:16 -07001745 case Instruction::IF_LT:
1746 case Instruction::IF_GE:
1747 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1750 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1753 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001754 }
1755 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 }
jeffhaobdb76512011-09-07 11:43:16 -07001757 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001763
1764 // Find previous instruction - its existence is a precondition to peephole optimization.
1765 uint32_t prev_idx = 0;
1766 if (0 != work_insn_idx_) {
1767 prev_idx = work_insn_idx_ - 1;
1768 while(0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1769 prev_idx--;
1770 }
1771 CHECK(insn_flags_[prev_idx].IsOpcode());
1772 } else {
1773 break;
1774 }
1775
1776 const Instruction* prev_inst = Instruction::At(code_item_->insns_+prev_idx);
1777
1778 /* Check for peep-hole pattern of:
1779 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001780 * instance-of vX, vY, T;
1781 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001782 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001783 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001784 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001785 * and sharpen the type of vY to be type T.
1786 * Note, this pattern can't be if:
1787 * - if there are other branches to this branch,
1788 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001789 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001790 if (!CurrentInsnFlags()->IsBranchTarget() &&
1791 (Instruction::INSTANCE_OF == prev_inst->Opcode()) &&
1792 (inst->VRegA_21t() == prev_inst->VRegA_22c()) &&
1793 (prev_inst->VRegA_22c() != prev_inst->VRegB_22c())) {
1794 // Check that the we are not attempting conversion to interface types,
1795 // which is not done because of the multiple inheritance implications.
1796 const RegType& cast_type = ResolveClassAndCheckAccess(prev_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001797
Ian Rogersfae370a2013-06-05 08:33:27 -07001798 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
1799 if (inst->Opcode() == Instruction::IF_EQZ) {
1800 fallthrough_line.reset(new RegisterLine(code_item_->registers_size_, this));
1801 fallthrough_line->CopyFromLine(work_line_.get());
1802 fallthrough_line->SetRegisterType(prev_inst->VRegB_22c(), cast_type);
1803 } else {
1804 branch_line.reset(new RegisterLine(code_item_->registers_size_, this));
1805 branch_line->CopyFromLine(work_line_.get());
1806 branch_line->SetRegisterType(prev_inst->VRegB_22c(), cast_type);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001807 }
1808 }
1809 }
1810
jeffhaobdb76512011-09-07 11:43:16 -07001811 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 }
jeffhaobdb76512011-09-07 11:43:16 -07001813 case Instruction::IF_LTZ:
1814 case Instruction::IF_GEZ:
1815 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1820 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 }
jeffhaobdb76512011-09-07 11:43:16 -07001824 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 break;
jeffhaobdb76512011-09-07 11:43:16 -07001827 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 break;
jeffhaobdb76512011-09-07 11:43:16 -07001830 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 break;
jeffhaobdb76512011-09-07 11:43:16 -07001833 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 break;
jeffhaobdb76512011-09-07 11:43:16 -07001839 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001840 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 break;
1842 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001843 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
1845
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001847 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 break;
1849 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 break;
1852 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001853 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 break;
1855 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
1858 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001860 break;
1861 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001863 break;
1864 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001866 break;
1867
jeffhaobdb76512011-09-07 11:43:16 -07001868 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001869 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 break;
jeffhaobdb76512011-09-07 11:43:16 -07001871 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001872 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 break;
jeffhaobdb76512011-09-07 11:43:16 -07001874 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001875 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 break;
jeffhaobdb76512011-09-07 11:43:16 -07001877 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 break;
1880 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001881 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001882 break;
1883 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001884 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
1886 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 break;
jeffhaobdb76512011-09-07 11:43:16 -07001889
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001891 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 break;
1893 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 break;
1896 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001897 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 break;
1899 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001900 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001906 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 break;
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
1911
jeffhaobdb76512011-09-07 11:43:16 -07001912 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001913 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 break;
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001916 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 break;
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 break;
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001922 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 break;
1924 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
1927 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001929 break;
1930 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 break;
1933
1934 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 break;
1937 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001938 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 break;
1940 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001941 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 break;
1943 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001944 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
1946 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001947 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001951 break;
1952 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
1955
1956 case Instruction::INVOKE_VIRTUAL:
1957 case Instruction::INVOKE_VIRTUAL_RANGE:
1958 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
1961 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1962 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
1963 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1964 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001965 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001966 const char* descriptor;
1967 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001969 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1970 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1971 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1972 } else {
1973 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001974 }
Ian Rogersb4903572012-10-11 11:52:56 -07001975 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 if (!return_type.IsLowHalf()) {
1977 work_line_->SetResultRegisterType(return_type);
1978 } else {
1979 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1980 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
1987 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001988 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001989 const char* return_type_descriptor;
1990 bool is_constructor;
1991 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07001993 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1994 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1995 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1996 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1997 } else {
1998 is_constructor = called_method->IsConstructor();
1999 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2000 }
2001 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002002 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 * Some additional checks when calling a constructor. We know from the invocation arg check
2004 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2005 * that to require that called_method->klass is the same as this->klass or this->super,
2006 * allowing the latter only if the "this" argument is the same as the "this" argument to
2007 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002008 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002010 if (this_type.IsConflict()) // failure.
2011 break;
jeffhaobdb76512011-09-07 11:43:16 -07002012
jeffhaob57e9522012-04-26 18:08:21 -07002013 /* no null refs allowed (?) */
2014 if (this_type.IsZero()) {
2015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2016 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002017 }
jeffhaob57e9522012-04-26 18:08:21 -07002018
2019 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002020 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2021 // TODO: re-enable constructor type verification
2022 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002023 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002024 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2025 // break;
2026 // }
jeffhaob57e9522012-04-26 18:08:21 -07002027
2028 /* arg must be an uninitialized reference */
2029 if (!this_type.IsUninitializedTypes()) {
2030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2031 << this_type;
2032 break;
2033 }
2034
2035 /*
2036 * Replace the uninitialized reference with an initialized one. We need to do this for all
2037 * registers that have the same object instance in them, not just the "this" register.
2038 */
2039 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002040 }
Ian Rogersb4903572012-10-11 11:52:56 -07002041 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2042 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002043 if (!return_type.IsLowHalf()) {
2044 work_line_->SetResultRegisterType(return_type);
2045 } else {
2046 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002048 just_set_result = true;
2049 break;
2050 }
2051 case Instruction::INVOKE_STATIC:
2052 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2054 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002055 const char* descriptor;
2056 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002058 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2059 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002060 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002061 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002062 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002063 }
Ian Rogersb4903572012-10-11 11:52:56 -07002064 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002065 if (!return_type.IsLowHalf()) {
2066 work_line_->SetResultRegisterType(return_type);
2067 } else {
2068 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2069 }
jeffhaobdb76512011-09-07 11:43:16 -07002070 just_set_result = true;
2071 }
2072 break;
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2076 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002077 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002078 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002079 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2080 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2081 << PrettyMethod(abs_method) << "'";
2082 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002083 }
Ian Rogers0d604842012-04-16 14:50:24 -07002084 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002085 /* Get the type of the "this" arg, which should either be a sub-interface of called
2086 * interface or Object (see comments in RegType::JoinClass).
2087 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002089 if (this_type.IsZero()) {
2090 /* null pointer always passes (and always fails at runtime) */
2091 } else {
2092 if (this_type.IsUninitializedTypes()) {
2093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2094 << this_type;
2095 break;
2096 }
2097 // In the past we have tried to assert that "called_interface" is assignable
2098 // from "this_type.GetClass()", however, as we do an imprecise Join
2099 // (RegType::JoinClass) we don't have full information on what interfaces are
2100 // implemented by "this_type". For example, two classes may implement the same
2101 // interfaces and have a common parent that doesn't implement the interface. The
2102 // join will set "this_type" to the parent class and a test that this implements
2103 // the interface will incorrectly fail.
2104 }
2105 /*
2106 * We don't have an object instance, so we can't find the concrete method. However, all of
2107 * the type information is in the abstract method, so we're good.
2108 */
2109 const char* descriptor;
2110 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002112 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2113 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2114 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2115 } else {
2116 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2117 }
Ian Rogersb4903572012-10-11 11:52:56 -07002118 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002119 if (!return_type.IsLowHalf()) {
2120 work_line_->SetResultRegisterType(return_type);
2121 } else {
2122 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2123 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002124 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 }
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::NEG_INT:
2128 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::NEG_LONG:
2132 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002145 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002160 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
2162 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002164 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002171 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002183 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
2185 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002187 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002188 break;
2189 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
2192 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002196 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198
2199 case Instruction::ADD_INT:
2200 case Instruction::SUB_INT:
2201 case Instruction::MUL_INT:
2202 case Instruction::REM_INT:
2203 case Instruction::DIV_INT:
2204 case Instruction::SHL_INT:
2205 case Instruction::SHR_INT:
2206 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002208 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002209 break;
2210 case Instruction::AND_INT:
2211 case Instruction::OR_INT:
2212 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002214 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
2216 case Instruction::ADD_LONG:
2217 case Instruction::SUB_LONG:
2218 case Instruction::MUL_LONG:
2219 case Instruction::DIV_LONG:
2220 case Instruction::REM_LONG:
2221 case Instruction::AND_LONG:
2222 case Instruction::OR_LONG:
2223 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 reg_types_.LongLo(), reg_types_.LongHi(),
2226 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228 case Instruction::SHL_LONG:
2229 case Instruction::SHR_LONG:
2230 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002233 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
2235 case Instruction::ADD_FLOAT:
2236 case Instruction::SUB_FLOAT:
2237 case Instruction::MUL_FLOAT:
2238 case Instruction::DIV_FLOAT:
2239 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::ADD_DOUBLE:
2243 case Instruction::SUB_DOUBLE:
2244 case Instruction::MUL_DOUBLE:
2245 case Instruction::DIV_DOUBLE:
2246 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2249 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002250 break;
2251 case Instruction::ADD_INT_2ADDR:
2252 case Instruction::SUB_INT_2ADDR:
2253 case Instruction::MUL_INT_2ADDR:
2254 case Instruction::REM_INT_2ADDR:
2255 case Instruction::SHL_INT_2ADDR:
2256 case Instruction::SHR_INT_2ADDR:
2257 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
2260 case Instruction::AND_INT_2ADDR:
2261 case Instruction::OR_INT_2ADDR:
2262 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002263 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::ADD_LONG_2ADDR:
2269 case Instruction::SUB_LONG_2ADDR:
2270 case Instruction::MUL_LONG_2ADDR:
2271 case Instruction::DIV_LONG_2ADDR:
2272 case Instruction::REM_LONG_2ADDR:
2273 case Instruction::AND_LONG_2ADDR:
2274 case Instruction::OR_LONG_2ADDR:
2275 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 reg_types_.LongLo(), reg_types_.LongHi(),
2278 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::SHL_LONG_2ADDR:
2281 case Instruction::SHR_LONG_2ADDR:
2282 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002284 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::ADD_FLOAT_2ADDR:
2287 case Instruction::SUB_FLOAT_2ADDR:
2288 case Instruction::MUL_FLOAT_2ADDR:
2289 case Instruction::DIV_FLOAT_2ADDR:
2290 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::ADD_DOUBLE_2ADDR:
2294 case Instruction::SUB_DOUBLE_2ADDR:
2295 case Instruction::MUL_DOUBLE_2ADDR:
2296 case Instruction::DIV_DOUBLE_2ADDR:
2297 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2300 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::ADD_INT_LIT16:
2303 case Instruction::RSUB_INT:
2304 case Instruction::MUL_INT_LIT16:
2305 case Instruction::DIV_INT_LIT16:
2306 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::AND_INT_LIT16:
2310 case Instruction::OR_INT_LIT16:
2311 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::ADD_INT_LIT8:
2315 case Instruction::RSUB_INT_LIT8:
2316 case Instruction::MUL_INT_LIT8:
2317 case Instruction::DIV_INT_LIT8:
2318 case Instruction::REM_INT_LIT8:
2319 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002321 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::AND_INT_LIT8:
2325 case Instruction::OR_INT_LIT8:
2326 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002331 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002332 case Instruction::UNUSED_EE:
2333 case Instruction::UNUSED_EF:
2334 case Instruction::UNUSED_F2:
2335 case Instruction::UNUSED_F3:
2336 case Instruction::UNUSED_F4:
2337 case Instruction::UNUSED_F5:
2338 case Instruction::UNUSED_F6:
2339 case Instruction::UNUSED_F7:
2340 case Instruction::UNUSED_F8:
2341 case Instruction::UNUSED_F9:
2342 case Instruction::UNUSED_FA:
2343 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002344 case Instruction::UNUSED_F0:
2345 case Instruction::UNUSED_F1:
2346 case Instruction::UNUSED_E3:
2347 case Instruction::UNUSED_E8:
2348 case Instruction::UNUSED_E7:
2349 case Instruction::UNUSED_E4:
2350 case Instruction::UNUSED_E9:
2351 case Instruction::UNUSED_FC:
2352 case Instruction::UNUSED_E5:
2353 case Instruction::UNUSED_EA:
2354 case Instruction::UNUSED_FD:
2355 case Instruction::UNUSED_E6:
2356 case Instruction::UNUSED_EB:
2357 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002358 case Instruction::UNUSED_3E:
2359 case Instruction::UNUSED_3F:
2360 case Instruction::UNUSED_40:
2361 case Instruction::UNUSED_41:
2362 case Instruction::UNUSED_42:
2363 case Instruction::UNUSED_43:
2364 case Instruction::UNUSED_73:
2365 case Instruction::UNUSED_79:
2366 case Instruction::UNUSED_7A:
2367 case Instruction::UNUSED_EC:
2368 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371
2372 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002373 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002374 * complain if an instruction is missing (which is desirable).
2375 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002376 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002377
Ian Rogersad0b3a32012-04-16 14:50:24 -07002378 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002379 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002380 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002381 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002382 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002383 /* immediate failure, reject class */
2384 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2385 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002386 } else if (have_pending_runtime_throw_failure_) {
2387 /* slow path will throw, mark following code as unreachable */
2388 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002389 }
jeffhaobdb76512011-09-07 11:43:16 -07002390 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 * If we didn't just set the result register, clear it out. This ensures that you can only use
2392 * "move-result" immediately after the result is set. (We could check this statically, but it's
2393 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002394 */
2395 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002397 }
2398
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002399
jeffhaobdb76512011-09-07 11:43:16 -07002400
2401 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002402 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002403 *
2404 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002405 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002406 * somebody could get a reference field, check it for zero, and if the
2407 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002408 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002409 * that, and will reject the code.
2410 *
2411 * TODO: avoid re-fetching the branch target
2412 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002413 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002414 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002416 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002418 return false;
2419 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002420 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002421 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002422 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 }
jeffhaobdb76512011-09-07 11:43:16 -07002424 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002425 if (NULL != branch_line.get()) {
2426 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2427 return false;
2428 }
2429 } else {
2430 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2431 return false;
2432 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 }
jeffhaobdb76512011-09-07 11:43:16 -07002434 }
2435
2436 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002437 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002438 *
2439 * We've already verified that the table is structurally sound, so we
2440 * just need to walk through and tag the targets.
2441 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002442 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002443 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2444 const uint16_t* switch_insns = insns + offset_to_switch;
2445 int switch_count = switch_insns[1];
2446 int offset_to_targets, targ;
2447
2448 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2449 /* 0 = sig, 1 = count, 2/3 = first key */
2450 offset_to_targets = 4;
2451 } else {
2452 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002453 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002454 offset_to_targets = 2 + 2 * switch_count;
2455 }
2456
2457 /* verify each switch target */
2458 for (targ = 0; targ < switch_count; targ++) {
2459 int offset;
2460 uint32_t abs_offset;
2461
2462 /* offsets are 32-bit, and only partly endian-swapped */
2463 offset = switch_insns[offset_to_targets + targ * 2] |
2464 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 abs_offset = work_insn_idx_ + offset;
2466 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002467 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002468 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 }
2470 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002471 return false;
2472 }
2473 }
2474
2475 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2477 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002478 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002479 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002481 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002482
Ian Rogers0571d352011-11-03 19:51:38 -07002483 for (; iterator.HasNext(); iterator.Next()) {
2484 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 within_catch_all = true;
2486 }
jeffhaobdb76512011-09-07 11:43:16 -07002487 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2489 * "work_regs", because at runtime the exception will be thrown before the instruction
2490 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002491 */
Ian Rogers0571d352011-11-03 19:51:38 -07002492 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002493 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 }
jeffhaobdb76512011-09-07 11:43:16 -07002495 }
2496
2497 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2499 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002500 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002502 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 * The state in work_line reflects the post-execution state. If the current instruction is a
2504 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002505 * it will do so before grabbing the lock).
2506 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002507 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002508 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002509 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002510 return false;
2511 }
2512 }
2513 }
2514
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002515 /* Handle "continue". Tag the next consecutive instruction.
2516 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2517 * because it changes work_line_ when performing peephole optimization
2518 * and this change should not be used in those cases.
2519 */
2520 if ((opcode_flags & Instruction::kContinue) != 0) {
2521 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2522 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2524 return false;
2525 }
2526 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2527 // next instruction isn't one.
2528 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2529 return false;
2530 }
2531 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2532 if (next_line != NULL) {
2533 if (NULL != fallthrough_line.get()) {
2534 // Make workline consistent with fallthrough computed from peephole optimization.
2535 work_line_->CopyFromLine(fallthrough_line.get());
2536 }
2537 // Merge registers into what we have for the next instruction,
2538 // and set the "changed" flag if needed.
2539 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2540 return false;
2541 }
2542 } else {
2543 /*
2544 * We're not recording register data for the next instruction, so we don't know what the
2545 * prior state was. We have to assume that something has changed and re-evaluate it.
2546 */
2547 insn_flags_[next_insn_idx].SetChanged();
2548 }
2549 }
2550
jeffhaod1f0fde2011-09-08 17:25:33 -07002551 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002552 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002553 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 return false;
2555 }
jeffhaobdb76512011-09-07 11:43:16 -07002556 }
2557
2558 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002559 * Update start_guess. Advance to the next instruction of that's
2560 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002561 * neither of those exists we're in a return or throw; leave start_guess
2562 * alone and let the caller sort it out.
2563 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002564 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002566 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002567 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002569 }
2570
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2572 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002573
2574 return true;
2575}
2576
Ian Rogers776ac1f2012-04-13 23:36:36 -07002577const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002578 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002579 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002580 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002581 const RegType& result =
Ian Rogers637c65b2013-05-31 11:46:00 -07002582 klass != NULL ? reg_types_.FromClass(descriptor, klass, klass->IsFinal())
Ian Rogersb4903572012-10-11 11:52:56 -07002583 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 if (result.IsConflict()) {
2585 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2586 << "' in " << referrer;
2587 return result;
2588 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002589 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002590 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002591 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002592 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002593 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002594 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002595 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002596 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002597 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002598 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002599}
2600
Ian Rogers776ac1f2012-04-13 23:36:36 -07002601const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002602 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002604 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2606 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002607 CatchHandlerIterator iterator(handlers_ptr);
2608 for (; iterator.HasNext(); iterator.Next()) {
2609 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2610 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002611 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002613 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002614 if (common_super == NULL) {
2615 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2616 // as that is caught at runtime
2617 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002618 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002619 // We don't know enough about the type and the common path merge will result in
2620 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002621 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002622 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002623 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002624 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002626 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002627 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 }
2629 }
2630 }
2631 }
Ian Rogers0571d352011-11-03 19:51:38 -07002632 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 }
2634 }
2635 if (common_super == NULL) {
2636 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002637 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002640 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002641}
2642
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002643mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2644 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002645 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002646 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002647 if (klass_type.IsConflict()) {
2648 std::string append(" in attempt to access method ");
2649 append += dex_file_->GetMethodName(method_id);
2650 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002651 return NULL;
2652 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002653 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002654 return NULL; // Can't resolve Class so no more to do here
2655 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002656 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002657 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002658 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002660 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002661 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002662
2663 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002665 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 res_method = klass->FindInterfaceMethod(name, signature);
2667 } else {
2668 res_method = klass->FindVirtualMethod(name, signature);
2669 }
2670 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002671 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002673 // If a virtual or interface method wasn't found with the expected type, look in
2674 // the direct methods. This can happen when the wrong invoke type is used or when
2675 // a class has changed, and will be flagged as an error in later checks.
2676 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2677 res_method = klass->FindDirectMethod(name, signature);
2678 }
2679 if (res_method == NULL) {
2680 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2681 << PrettyDescriptor(klass) << "." << name
2682 << " " << signature;
2683 return NULL;
2684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 }
2686 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2688 // enforce them here.
2689 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2691 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 return NULL;
2693 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002694 // Disallow any calls to class initializers.
2695 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2697 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002698 return NULL;
2699 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002700 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002701 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002702 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002703 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002704 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002705 }
jeffhaode0d9c92012-02-27 13:58:13 -08002706 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2707 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2709 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002710 return NULL;
2711 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002712 // Check that interface methods match interface classes.
2713 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2714 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2715 << " is in an interface class " << PrettyClass(klass);
2716 return NULL;
2717 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2718 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2719 << " is in a non-interface class " << PrettyClass(klass);
2720 return NULL;
2721 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 // See if the method type implied by the invoke instruction matches the access flags for the
2723 // target method.
2724 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2725 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2726 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2727 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002728 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2729 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 return NULL;
2731 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002732 return res_method;
2733}
2734
Sebastien Hertz5243e912013-05-21 10:55:07 +02002735mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2736 MethodType method_type,
2737 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002738 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002739 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2740 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002741 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2742 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002743 if (res_method == NULL) { // error or class is unresolved
2744 return NULL;
2745 }
2746
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2748 // has a vtable entry for the target method.
2749 if (is_super) {
2750 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002751 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002752 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002753 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002754 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002755 << " to super " << PrettyMethod(res_method);
2756 return NULL;
2757 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002758 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002759 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002760 MethodHelper mh(res_method);
2761 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002762 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002763 << " to super " << super
2764 << "." << mh.GetName()
2765 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 return NULL;
2767 }
2768 }
2769 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2770 // match the call to the signature. Also, we might might be calling through an abstract method
2771 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002772 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 /* caught by static verifier */
2774 DCHECK(is_range || expected_args <= 5);
2775 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2778 return NULL;
2779 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002780
jeffhaobdb76512011-09-07 11:43:16 -07002781 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002782 * Check the "this" argument, which must be an instance of the class that declared the method.
2783 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2784 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002785 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002786 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002788 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002789 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 return NULL;
2791 }
2792 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 return NULL;
2795 }
2796 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002797 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07002798 const RegType& res_method_class = reg_types_.FromClass(ClassHelper(klass).GetDescriptor(),
2799 klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002800 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002801 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002802 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 return NULL;
2804 }
2805 }
2806 actual_args++;
2807 }
2808 /*
2809 * Process the target method's signature. This signature may or may not
2810 * have been verified, so we can't assume it's properly formed.
2811 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002812 MethodHelper mh(res_method);
2813 const DexFile::TypeList* params = mh.GetParameterTypeList();
2814 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002815 uint32_t arg[5];
2816 if (!is_range) {
2817 inst->GetArgs(arg);
2818 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002819 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002822 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2823 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 return NULL;
2825 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002826 const char* descriptor =
2827 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2828 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002830 << " missing signature component";
2831 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 }
Ian Rogersb4903572012-10-11 11:52:56 -07002833 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002834 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002835 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002836 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 }
2838 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2839 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002842 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 return NULL;
2844 } else {
2845 return res_method;
2846 }
2847}
2848
Sebastien Hertz5243e912013-05-21 10:55:07 +02002849void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled,
2850 bool is_range) {
2851 uint32_t type_idx;
2852 if (!is_filled) {
2853 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
2854 type_idx = inst->VRegC_22c();
2855 } else if (!is_range) {
2856 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
2857 type_idx = inst->VRegB_35c();
2858 } else {
2859 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
2860 type_idx = inst->VRegB_3rc();
2861 }
2862 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 if (res_type.IsConflict()) { // bad class
2864 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002865 } else {
2866 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2867 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002869 } else if (!is_filled) {
2870 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002871 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002872 /* set register type to array class */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002873 work_line_->SetRegisterType(inst->VRegA_22c(), res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002874 } else {
2875 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2876 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002877 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002878 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
2879 uint32_t arg[5];
2880 if (!is_range) {
2881 inst->GetArgs(arg);
2882 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002883 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002884 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002885 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002887 return;
2888 }
2889 }
2890 // filled-array result goes into "result" register
2891 work_line_->SetResultRegisterType(res_type);
2892 }
2893 }
2894}
2895
Sebastien Hertz5243e912013-05-21 10:55:07 +02002896void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002897 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002898 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002902 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002903 if (array_type.IsZero()) {
2904 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2905 // instruction type. TODO: have a proper notion of bottom here.
2906 if (!is_primitive || insn_type.IsCategory1Types()) {
2907 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02002908 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002910 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02002911 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002912 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002913 }
jeffhaofc3144e2012-02-01 17:21:15 -08002914 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002916 } else {
2917 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002918 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002919 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002921 << " source for aget-object";
2922 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002924 << " source for category 1 aget";
2925 } else if (is_primitive && !insn_type.Equals(component_type) &&
2926 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002927 (insn_type.IsLong() && component_type.IsDouble()))) {
2928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2929 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002930 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002931 // Use knowledge of the field type which is stronger than the type inferred from the
2932 // instruction, which can't differentiate object types and ints from floats, longs from
2933 // doubles.
2934 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002935 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002936 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002937 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002938 component_type.HighHalf(&reg_types_));
2939 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 }
2941 }
2942 }
2943}
2944
Sebastien Hertz5243e912013-05-21 10:55:07 +02002945void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002947 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002951 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002952 if (array_type.IsZero()) {
2953 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2954 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002955 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002957 } else {
2958 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002959 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002960 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002962 << " source for aput-object";
2963 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002965 << " source for category 1 aput";
2966 } else if (is_primitive && !insn_type.Equals(component_type) &&
2967 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2968 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002970 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002972 // The instruction agrees with the type of array, confirm the value to be stored does too
2973 // Note: we use the instruction type (rather than the component type) for aput-object as
2974 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02002975 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 }
2977 }
2978 }
2979}
2980
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002981mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002982 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2983 // Check access to class
2984 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002985 if (klass_type.IsConflict()) { // bad class
2986 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2987 field_idx, dex_file_->GetFieldName(field_id),
2988 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002989 return NULL;
2990 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002991 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002992 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002993 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002994 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07002997 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07002998 << dex_file_->GetFieldName(field_id) << ") in "
2999 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 DCHECK(Thread::Current()->IsExceptionPending());
3001 Thread::Current()->ClearException();
3002 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003003 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3004 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003006 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 return NULL;
3008 } else if (!field->IsStatic()) {
3009 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3010 return NULL;
3011 } else {
3012 return field;
3013 }
3014}
3015
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003016mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003017 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3018 // Check access to class
3019 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 if (klass_type.IsConflict()) {
3021 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3022 field_idx, dex_file_->GetFieldName(field_id),
3023 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003024 return NULL;
3025 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003026 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003027 return NULL; // Can't resolve Class so no more to do here
3028 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003029 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003032 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003033 << dex_file_->GetFieldName(field_id) << ") in "
3034 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 DCHECK(Thread::Current()->IsExceptionPending());
3036 Thread::Current()->ClearException();
3037 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3039 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 return NULL;
3043 } else if (field->IsStatic()) {
3044 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3045 << " to not be static";
3046 return NULL;
3047 } else if (obj_type.IsZero()) {
3048 // Cannot infer and check type, however, access will cause null pointer exception
3049 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003050 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003051 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003052 const RegType& field_klass =
3053 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
3054 klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 if (obj_type.IsUninitializedTypes() &&
3056 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3057 !field_klass.Equals(GetDeclaringClass()))) {
3058 // Field accesses through uninitialized references are only allowable for constructors where
3059 // the field is declared in this class
3060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3061 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003062 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 return NULL;
3064 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3065 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3066 // of C1. For resolution to occur the declared class of the field must be compatible with
3067 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3068 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3069 << " from object of type " << obj_type;
3070 return NULL;
3071 } else {
3072 return field;
3073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 }
3075}
3076
Sebastien Hertz5243e912013-05-21 10:55:07 +02003077void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3078 bool is_primitive, bool is_static) {
3079 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003080 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003081 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003082 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003083 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003084 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003085 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003086 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003087 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003088 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003089 if (field != NULL) {
3090 descriptor = FieldHelper(field).GetTypeDescriptor();
3091 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003092 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003093 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3094 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3095 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003096 }
Ian Rogersb4903572012-10-11 11:52:56 -07003097 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003098 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003099 if (is_primitive) {
3100 if (field_type.Equals(insn_type) ||
3101 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3102 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3103 // expected that read is of the correct primitive type or that int reads are reading
3104 // floats or long reads are reading doubles
3105 } else {
3106 // This is a global failure rather than a class change failure as the instructions and
3107 // the descriptors for the type should have been consistent within the same file at
3108 // compile time
3109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3110 << " to be of type '" << insn_type
3111 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003112 return;
3113 }
3114 } else {
3115 if (!insn_type.IsAssignableFrom(field_type)) {
3116 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3117 << " to be compatible with type '" << insn_type
3118 << "' but found type '" << field_type
3119 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003120 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003121 return;
3122 }
3123 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003124 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003125 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003126 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003127 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003129}
3130
Sebastien Hertz5243e912013-05-21 10:55:07 +02003131void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3132 bool is_primitive, bool is_static) {
3133 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003134 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003135 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003136 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003137 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003138 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003139 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003140 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003141 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003142 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003143 if (field != NULL) {
3144 descriptor = FieldHelper(field).GetTypeDescriptor();
3145 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003146 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003147 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3148 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3149 loader = class_loader_;
3150 }
Ian Rogersb4903572012-10-11 11:52:56 -07003151 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003152 if (field != NULL) {
3153 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3154 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3155 << " from other class " << GetDeclaringClass();
3156 return;
3157 }
3158 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003159 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003160 if (is_primitive) {
3161 // Primitive field assignability rules are weaker than regular assignability rules
3162 bool instruction_compatible;
3163 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003164 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003165 if (field_type.IsIntegralTypes()) {
3166 instruction_compatible = insn_type.IsIntegralTypes();
3167 value_compatible = value_type.IsIntegralTypes();
3168 } else if (field_type.IsFloat()) {
3169 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3170 value_compatible = value_type.IsFloatTypes();
3171 } else if (field_type.IsLong()) {
3172 instruction_compatible = insn_type.IsLong();
3173 value_compatible = value_type.IsLongTypes();
3174 } else if (field_type.IsDouble()) {
3175 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3176 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003177 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003178 instruction_compatible = false; // reference field with primitive store
3179 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003181 if (!instruction_compatible) {
3182 // This is a global failure rather than a class change failure as the instructions and
3183 // the descriptors for the type should have been consistent within the same file at
3184 // compile time
3185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3186 << " to be of type '" << insn_type
3187 << "' but found type '" << field_type
3188 << "' in put";
3189 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003190 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003191 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003193 << " of type " << value_type
3194 << " but expected " << field_type
3195 << " for store to " << PrettyField(field) << " in put";
3196 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003197 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003198 } else {
3199 if (!insn_type.IsAssignableFrom(field_type)) {
3200 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3201 << " to be compatible with type '" << insn_type
3202 << "' but found type '" << field_type
3203 << "' in put-object";
3204 return;
3205 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003206 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003207 }
3208}
3209
Ian Rogers776ac1f2012-04-13 23:36:36 -07003210bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 return false;
3214 }
3215 return true;
3216}
3217
Ian Rogers776ac1f2012-04-13 23:36:36 -07003218bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 bool changed = true;
3220 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3221 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003222 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 * We haven't processed this instruction before, and we haven't touched the registers here, so
3224 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3225 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003226 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003228 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003229 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3230 if (gDebugVerify) {
3231 copy->CopyFromLine(target_line);
3232 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003234 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003235 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003236 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003237 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003238 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003239 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3240 << *copy.get() << " MERGE\n"
3241 << *merge_line << " ==\n"
3242 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003243 }
3244 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 if (changed) {
3246 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003247 }
3248 return true;
3249}
3250
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003251InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003252 return &insn_flags_[work_insn_idx_];
3253}
3254
Ian Rogersad0b3a32012-04-16 14:50:24 -07003255const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003256 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003257 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3258 uint16_t return_type_idx = proto_id.return_type_idx_;
3259 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003260 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003261}
3262
3263const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003264 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003265 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003266 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003267 if (mirror_method_ != NULL) {
3268 mirror::Class* klass = mirror_method_->GetDeclaringClass();
3269 declaring_class_ = &reg_types_.FromClass(descriptor, klass, klass->IsFinal());
3270 } else {
3271 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3272 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003274 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275}
3276
Ian Rogers776ac1f2012-04-13 23:36:36 -07003277void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 size_t* log2_max_gc_pc) {
3279 size_t local_gc_points = 0;
3280 size_t max_insn = 0;
3281 size_t max_ref_reg = -1;
3282 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003283 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 local_gc_points++;
3285 max_insn = i;
3286 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003287 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003288 }
3289 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 *gc_points = local_gc_points;
3291 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3292 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003293 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 i++;
3295 }
3296 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003297}
3298
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003299MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3300 /*
3301 * Walks over the method code and adds any cast instructions in which
3302 * the type cast is implicit to a set, which is used in the code generation
3303 * to elide these casts.
3304 */
3305 if (!failure_messages_.empty()) {
3306 return NULL;
3307 }
3308 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003309 const Instruction* inst = Instruction::At(code_item_->insns_);
3310 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003311 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003312
3313 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003314 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003315 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003316 }
3317 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003318 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003319 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3320 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003321 if (cast_type.IsAssignableFrom(reg_type)) {
3322 if (mscs.get() == NULL) {
3323 mscs.reset(new MethodSafeCastSet());
3324 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003325 mscs->insert(dex_pc);
3326 }
3327 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003328 return mscs.release();
3329}
3330
Ian Rogersd0583802013-06-01 10:51:46 -07003331MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003332
3333 // It is risky to rely on reg_types for sharpening in cases of soft
3334 // verification, we might end up sharpening to a wrong implementation. Just abort.
3335 if (!failure_messages_.empty()) {
3336 return NULL;
3337 }
3338
Ian Rogersd0583802013-06-01 10:51:46 -07003339 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003340 const uint16_t* insns = code_item_->insns_ ;
3341 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003342 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003343
Ian Rogersd0583802013-06-01 10:51:46 -07003344 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003345 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3346 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3347 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3348 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3349
Ian Rogersd0583802013-06-01 10:51:46 -07003350 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003351 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003352 }
Ian Rogersd0583802013-06-01 10:51:46 -07003353 // Get reg type for register holding the reference to the object that will be dispatched upon.
3354 uint32_t dex_pc = inst->GetDexPc(insns);
3355 RegisterLine* line = reg_table_.GetLine(dex_pc);
3356 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3357 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3358 const RegType&
3359 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003360
Ian Rogersd0583802013-06-01 10:51:46 -07003361 if (!reg_type.HasClass()) {
3362 // We will compute devirtualization information only when we know the Class of the reg type.
3363 continue;
3364 }
3365 mirror::Class* reg_class = reg_type.GetClass();
3366 if (reg_class->IsInterface()) {
3367 // We can't devirtualize when the known type of the register is an interface.
3368 continue;
3369 }
3370 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3371 // We can't devirtualize abstract classes except on arrays of abstract classes.
3372 continue;
3373 }
3374 mirror::AbstractMethod* abstract_method =
3375 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003376 if(abstract_method == NULL) {
3377 // If the method is not found in the cache this means that it was never found
3378 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3379 continue;
3380 }
3381 // Find the concrete method.
3382 mirror::AbstractMethod* concrete_method = NULL;
3383 if (is_interface) {
3384 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3385 }
3386 if (is_virtual) {
3387 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3388 }
Ian Rogersd0583802013-06-01 10:51:46 -07003389 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3390 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003391 continue;
3392 }
Ian Rogersd0583802013-06-01 10:51:46 -07003393 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3394 concrete_method->GetDeclaringClass()->IsFinal()) {
3395 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3396 // overridden record the target to be used in the compiler driver.
3397 if (pc_to_concrete_method_map.get() == NULL) {
3398 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3399 }
3400 CompilerDriver::MethodReference concrete_ref(
3401 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3402 concrete_method->GetDexMethodIndex());
3403 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3404 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003405 }
Ian Rogersd0583802013-06-01 10:51:46 -07003406 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003407}
3408
Ian Rogers776ac1f2012-04-13 23:36:36 -07003409const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 size_t num_entries, ref_bitmap_bits, pc_bits;
3411 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3412 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003413 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003417 return NULL;
3418 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3420 // There are 2 bytes to encode the number of entries
3421 if (num_entries >= 65536) {
3422 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003424 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003425 return NULL;
3426 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003428 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003429 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003430 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003432 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003433 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003435 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3439 return NULL;
3440 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003441 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003442 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 return NULL;
3446 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003447 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003449 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3450 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003451 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003452 table->push_back(num_entries & 0xFF);
3453 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003456 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003457 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003459 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 }
3461 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003462 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 }
3464 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003465 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 return table;
3467}
jeffhaoa0a764a2011-09-16 10:43:38 -07003468
Ian Rogers776ac1f2012-04-13 23:36:36 -07003469void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3471 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003472 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003474 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003476 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003477 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003478 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3480 map_index++;
3481 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003482 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003483 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 CHECK_LT(j / 8, map.RegWidth());
3485 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3486 } else if ((j / 8) < map.RegWidth()) {
3487 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3488 } else {
3489 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3490 }
3491 }
3492 } else {
3493 CHECK(reg_bitmap == NULL);
3494 }
3495 }
3496}
jeffhaoa0a764a2011-09-16 10:43:38 -07003497
Ian Rogers637c65b2013-05-31 11:46:00 -07003498void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3499 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003500 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003501 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003502 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3503 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003504 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003505 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003506 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003507 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003508 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003509 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003510}
3511
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003512
Ian Rogersfae370a2013-06-05 08:33:27 -07003513void MethodVerifier::SetSafeCastMap(CompilerDriver::MethodReference ref,
3514 const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003515 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3516 SafeCastMap::iterator it = safecast_map_->find(ref);
3517 if (it != safecast_map_->end()) {
3518 delete it->second;
3519 safecast_map_->erase(it);
3520 }
3521
3522 safecast_map_->Put(ref, cast_set);
3523 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3524}
3525
3526bool MethodVerifier::IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) {
3527 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3528 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3529 if (it == safecast_map_->end()) {
3530 return false;
3531 }
3532
3533 // Look up the cast address in the set of safe casts
3534 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3535 return cast_it != it->second->end();
3536}
3537
Ian Rogers637c65b2013-05-31 11:46:00 -07003538const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3539 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3540 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3541 if (it == dex_gc_maps_->end()) {
3542 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3543 return NULL;
3544 }
3545 CHECK(it->second != NULL);
3546 return it->second;
3547}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003548
Ian Rogers637c65b2013-05-31 11:46:00 -07003549void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003550 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003551 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003552 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3553 if (it != devirt_maps_->end()) {
3554 delete it->second;
3555 devirt_maps_->erase(it);
3556 }
3557
3558 devirt_maps_->Put(ref, devirt_map);
3559 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3560}
3561
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003562const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3563 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003564 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003565 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3566 if (it == devirt_maps_->end()) {
3567 return NULL;
3568 }
3569
3570 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003571 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003572 if(pc_to_concrete_method != it->second->end()) {
3573 return &(pc_to_concrete_method->second);
3574 } else {
3575 return NULL;
3576 }
3577}
3578
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003579std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3580 RegisterLine* line = reg_table_.GetLine(dex_pc);
3581 std::vector<int32_t> result;
3582 for (size_t i = 0; i < line->NumRegs(); ++i) {
3583 const RegType& type = line->GetRegisterType(i);
3584 if (type.IsConstant()) {
3585 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3586 result.push_back(type.ConstantValue());
3587 } else if (type.IsConstantLo()) {
3588 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3589 result.push_back(type.ConstantValueLo());
3590 } else if (type.IsConstantHi()) {
3591 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3592 result.push_back(type.ConstantValueHi());
3593 } else if (type.IsIntegralTypes()) {
3594 result.push_back(kIntVReg);
3595 result.push_back(0);
3596 } else if (type.IsFloat()) {
3597 result.push_back(kFloatVReg);
3598 result.push_back(0);
3599 } else if (type.IsLong()) {
3600 result.push_back(kLongLoVReg);
3601 result.push_back(0);
3602 result.push_back(kLongHiVReg);
3603 result.push_back(0);
3604 ++i;
3605 } else if (type.IsDouble()) {
3606 result.push_back(kDoubleLoVReg);
3607 result.push_back(0);
3608 result.push_back(kDoubleHiVReg);
3609 result.push_back(0);
3610 ++i;
3611 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3612 result.push_back(kUndefined);
3613 result.push_back(0);
3614 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003615 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003616 result.push_back(kReferenceVReg);
3617 result.push_back(0);
3618 }
3619 }
3620 return result;
3621}
3622
Ian Rogers637c65b2013-05-31 11:46:00 -07003623ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003624MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003625
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003626Mutex* MethodVerifier::safecast_map_lock_ = NULL;
3627MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
3628
Ian Rogers637c65b2013-05-31 11:46:00 -07003629ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003630MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3631
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003632Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3633MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3634
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003635void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003636 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003637 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003638 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003639 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003640 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003641 }
3642
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003643 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
3644 {
3645 MutexLock mu(self, *safecast_map_lock_);
3646 safecast_map_ = new MethodVerifier::SafeCastMap();
3647 }
3648
Ian Rogers637c65b2013-05-31 11:46:00 -07003649 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003650
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003651 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003652 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003653 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3654 }
3655
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003656 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3657 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003658 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003659 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3660 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003661 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003662}
3663
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003664void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003665 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003666 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003667 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003668 STLDeleteValues(dex_gc_maps_);
3669 delete dex_gc_maps_;
3670 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003671 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003672 delete dex_gc_maps_lock_;
3673 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003674
3675 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003676 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003677 STLDeleteValues(devirt_maps_);
3678 delete devirt_maps_;
3679 devirt_maps_ = NULL;
3680 }
3681 delete devirt_maps_lock_;
3682 devirt_maps_lock_ = NULL;
3683
3684 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003685 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003686 delete rejected_classes_;
3687 rejected_classes_ = NULL;
3688 }
3689 delete rejected_classes_lock_;
3690 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003691 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003692}
jeffhaod1224c72012-02-29 13:43:08 -08003693
Ian Rogers1212a022013-03-04 10:48:41 -08003694void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003695 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003696 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003697 rejected_classes_->insert(ref);
3698 }
jeffhaod1224c72012-02-29 13:43:08 -08003699 CHECK(IsClassRejected(ref));
3700}
3701
Ian Rogers1212a022013-03-04 10:48:41 -08003702bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003703 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003704 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003705}
3706
Ian Rogersd81871c2011-10-03 13:57:23 -07003707} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003708} // namespace art