blob: 1b9e2b2aa1583d30d6e03f9eddc7ba10f8423e62 [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
Ian Rogersd0583802013-06-01 10:51:46 -0700962 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700963 if(pc_to_concrete_method != NULL ) {
964 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700965 }
jeffhaobdb76512011-09-07 11:43:16 -0700966 return true;
967}
968
Ian Rogersad0b3a32012-04-16 14:50:24 -0700969std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
970 DCHECK_EQ(failures_.size(), failure_messages_.size());
971 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700972 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700973 }
974 return os;
975}
976
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700978 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700979 v->Dump(std::cerr);
980}
981
Ian Rogers776ac1f2012-04-13 23:36:36 -0700982void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800983 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700984 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 return;
jeffhaobdb76512011-09-07 11:43:16 -0700986 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 {
988 os << "Register Types:\n";
989 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
990 std::ostream indent_os(&indent_filter);
991 reg_types_.Dump(indent_os);
992 }
Ian Rogersb4903572012-10-11 11:52:56 -0700993 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800994 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
995 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 const Instruction* inst = Instruction::At(code_item_->insns_);
997 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
998 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1000 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001002 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001003 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001004 const bool kDumpHexOfInstruction = false;
1005 if (kDumpHexOfInstruction) {
1006 indent_os << inst->DumpHex(5) << " ";
1007 }
1008 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 inst = inst->Next();
1010 }
jeffhaobdb76512011-09-07 11:43:16 -07001011}
1012
Ian Rogersd81871c2011-10-03 13:57:23 -07001013static bool IsPrimitiveDescriptor(char descriptor) {
1014 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001015 case 'I':
1016 case 'C':
1017 case 'S':
1018 case 'B':
1019 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001020 case 'F':
1021 case 'D':
1022 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001024 default:
1025 return false;
1026 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001027}
1028
Ian Rogers776ac1f2012-04-13 23:36:36 -07001029bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 RegisterLine* reg_line = reg_table_.GetLine(0);
1031 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1032 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1035 //Include the "this" pointer.
1036 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001037 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1039 // argument as uninitialized. This restricts field access until the superclass constructor is
1040 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001041 const RegType& declaring_class = GetDeclaringClass();
1042 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 reg_line->SetRegisterType(arg_start + cur_arg,
1044 reg_types_.UninitializedThisArgument(declaring_class));
1045 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001046 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001047 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001049 }
1050
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001051 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001052 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001053 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001054
1055 for (; iterator.HasNext(); iterator.Next()) {
1056 const char* descriptor = iterator.GetDescriptor();
1057 if (descriptor == NULL) {
1058 LOG(FATAL) << "Null descriptor";
1059 }
1060 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1062 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 return false;
1064 }
1065 switch (descriptor[0]) {
1066 case 'L':
1067 case '[':
1068 // We assume that reference arguments are initialized. The only way it could be otherwise
1069 // (assuming the caller was verified) is if the current method is <init>, but in that case
1070 // it's effectively considered initialized the instant we reach here (in the sense that we
1071 // can return without doing anything or call virtual methods).
1072 {
Ian Rogersb4903572012-10-11 11:52:56 -07001073 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001074 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 }
1076 break;
1077 case 'Z':
1078 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1079 break;
1080 case 'C':
1081 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1082 break;
1083 case 'B':
1084 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1085 break;
1086 case 'I':
1087 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1088 break;
1089 case 'S':
1090 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1091 break;
1092 case 'F':
1093 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1094 break;
1095 case 'J':
1096 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001097 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1098 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1099 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 cur_arg++;
1101 break;
1102 }
1103 default:
jeffhaod5347e02012-03-22 17:25:05 -07001104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 cur_arg++;
1108 }
1109 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1114 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1115 // format. Only major difference from the method argument format is that 'V' is supported.
1116 bool result;
1117 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1118 result = descriptor[1] == '\0';
1119 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1120 size_t i = 0;
1121 do {
1122 i++;
1123 } while (descriptor[i] == '['); // process leading [
1124 if (descriptor[i] == 'L') { // object array
1125 do {
1126 i++; // find closing ;
1127 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1128 result = descriptor[i] == ';';
1129 } else { // primitive array
1130 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1131 }
1132 } else if (descriptor[0] == 'L') {
1133 // could be more thorough here, but shouldn't be required
1134 size_t i = 0;
1135 do {
1136 i++;
1137 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1138 result = descriptor[i] == ';';
1139 } else {
1140 result = false;
1141 }
1142 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1144 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 }
1146 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001147}
1148
Ian Rogers776ac1f2012-04-13 23:36:36 -07001149bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 const uint16_t* insns = code_item_->insns_;
1151 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001152
jeffhaobdb76512011-09-07 11:43:16 -07001153 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 insn_flags_[0].SetChanged();
1155 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001156
jeffhaobdb76512011-09-07 11:43:16 -07001157 /* Continue until no instructions are marked "changed". */
1158 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1160 uint32_t insn_idx = start_guess;
1161 for (; insn_idx < insns_size; insn_idx++) {
1162 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001163 break;
1164 }
jeffhaobdb76512011-09-07 11:43:16 -07001165 if (insn_idx == insns_size) {
1166 if (start_guess != 0) {
1167 /* try again, starting from the top */
1168 start_guess = 0;
1169 continue;
1170 } else {
1171 /* all flags are clear */
1172 break;
1173 }
1174 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 // We carry the working set of registers from instruction to instruction. If this address can
1176 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1177 // "changed" flags, we need to load the set of registers from the table.
1178 // Because we always prefer to continue on to the next instruction, we should never have a
1179 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1180 // target.
1181 work_insn_idx_ = insn_idx;
1182 if (insn_flags_[insn_idx].IsBranchTarget()) {
1183 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001184 } else {
1185#ifndef NDEBUG
1186 /*
1187 * Sanity check: retrieve the stored register line (assuming
1188 * a full table) and make sure it actually matches.
1189 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1191 if (register_line != NULL) {
1192 if (work_line_->CompareLine(register_line) != 0) {
1193 Dump(std::cout);
1194 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001196 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1197 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001198 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 }
jeffhaobdb76512011-09-07 11:43:16 -07001200 }
1201#endif
1202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001205 prepend += " failed to verify: ";
1206 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 return false;
1208 }
jeffhaobdb76512011-09-07 11:43:16 -07001209 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 insn_flags_[insn_idx].SetVisited();
1211 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213
Ian Rogers1c849e52012-06-28 14:00:33 -07001214 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001215 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001216 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001217 * (besides the wasted space), but it indicates a flaw somewhere
1218 * down the line, possibly in the verifier.
1219 *
1220 * If we've substituted "always throw" instructions into the stream,
1221 * we are almost certainly going to have some dead code.
1222 */
1223 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 uint32_t insn_idx = 0;
1225 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001226 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001227 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001228 * may or may not be preceded by a padding NOP (for alignment).
1229 */
1230 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1231 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1232 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001233 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001234 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1235 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1236 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001238 }
1239
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001241 if (dead_start < 0)
1242 dead_start = insn_idx;
1243 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001244 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001245 dead_start = -1;
1246 }
1247 }
1248 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);
jeffhaoba5ebb92011-08-25 17:24:37 -07001250 }
1251 }
jeffhaobdb76512011-09-07 11:43:16 -07001252 return true;
1253}
1254
Ian Rogers776ac1f2012-04-13 23:36:36 -07001255bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001256 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1257 // We want the state _before_ the instruction, for the case where the dex pc we're
1258 // interested in is itself a monitor-enter instruction (which is a likely place
1259 // for a thread to be suspended).
1260 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001261 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001262 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1263 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1264 }
1265 }
1266
jeffhaobdb76512011-09-07 11:43:16 -07001267 /*
1268 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001269 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001270 * control to another statement:
1271 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001272 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001273 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001274 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001275 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001276 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001277 * throw an exception that is handled by an encompassing "try"
1278 * block.
1279 *
1280 * We can also return, in which case there is no successor instruction
1281 * from this point.
1282 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001283 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001284 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1286 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001287 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001288
jeffhaobdb76512011-09-07 11:43:16 -07001289 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001290 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001291 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001293 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1294 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 }
jeffhaobdb76512011-09-07 11:43:16 -07001296
1297 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001298 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001299 * can throw an exception, we will copy/merge this into the "catch"
1300 * address rather than work_line, because we don't want the result
1301 * from the "successful" code path (e.g. a check-cast that "improves"
1302 * a type) to be visible to the exception handler.
1303 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001304 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001306 } else {
1307#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001309#endif
1310 }
1311
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001312 // We need to ensure the work line is consistent while performing validation. When we spot a
1313 // peephole pattern we compute a new line for either the fallthrough instruction or the
1314 // branch target.
1315 UniquePtr<RegisterLine> branch_line;
1316 UniquePtr<RegisterLine> fallthrough_line;
1317
Sebastien Hertz5243e912013-05-21 10:55:07 +02001318 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001319 case Instruction::NOP:
1320 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001321 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001322 * a signature that looks like a NOP; if we see one of these in
1323 * the course of executing code then we have a problem.
1324 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001325 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001327 }
1328 break;
1329
1330 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001331 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1332 break;
jeffhaobdb76512011-09-07 11:43:16 -07001333 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001334 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1335 break;
jeffhaobdb76512011-09-07 11:43:16 -07001336 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001337 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001338 break;
1339 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001340 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1341 break;
jeffhaobdb76512011-09-07 11:43:16 -07001342 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001343 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1344 break;
jeffhaobdb76512011-09-07 11:43:16 -07001345 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001346 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001347 break;
1348 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001349 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1350 break;
jeffhaobdb76512011-09-07 11:43:16 -07001351 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001352 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1353 break;
jeffhaobdb76512011-09-07 11:43:16 -07001354 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001355 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001356 break;
1357
1358 /*
1359 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001360 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001361 * might want to hold the result in an actual CPU register, so the
1362 * Dalvik spec requires that these only appear immediately after an
1363 * invoke or filled-new-array.
1364 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001365 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001366 * redundant with the reset done below, but it can make the debug info
1367 * easier to read in some cases.)
1368 */
1369 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001370 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001371 break;
1372 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001373 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001374 break;
1375 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001376 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001377 break;
1378
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001380 /*
jeffhao60f83e32012-02-13 17:16:30 -08001381 * This statement can only appear as the first instruction in an exception handler. We verify
1382 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001383 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001384 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001385 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001386 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 }
jeffhaobdb76512011-09-07 11:43:16 -07001388 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001389 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1390 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 }
jeffhaobdb76512011-09-07 11:43:16 -07001393 }
1394 break;
1395 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001396 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001397 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 const RegType& return_type = GetMethodReturnType();
1399 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 } else {
1402 // Compilers may generate synthetic functions that write byte values into boolean fields.
1403 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001404 const uint32_t vregA = inst->VRegA_11x();
1405 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1407 ((return_type.IsBoolean() || return_type.IsByte() ||
1408 return_type.IsShort() || return_type.IsChar()) &&
1409 src_type.IsInteger()));
1410 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001411 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001412 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001413 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001414 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 }
jeffhaobdb76512011-09-07 11:43:16 -07001416 }
1417 }
1418 break;
1419 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001420 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001421 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 const RegType& return_type = GetMethodReturnType();
1423 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 } else {
1426 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001427 const uint32_t vregA = inst->VRegA_11x();
1428 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001429 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001430 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 }
jeffhaobdb76512011-09-07 11:43:16 -07001432 }
1433 }
1434 break;
1435 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001436 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 const RegType& return_type = GetMethodReturnType();
1438 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 } else {
1441 /* return_type is the *expected* return type, not register value */
1442 DCHECK(!return_type.IsZero());
1443 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001444 const uint32_t vregA = inst->VRegA_11x();
1445 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001446 // Disallow returning uninitialized values and verify that the reference in vAA is an
1447 // instance of the "return_type"
1448 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001449 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001450 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001451 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1452 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001453 }
1454 }
1455 }
1456 break;
1457
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001458 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001459 case Instruction::CONST_4: {
1460 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1461 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001462 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 }
1464 case Instruction::CONST_16: {
1465 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1466 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001467 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001468 }
jeffhaobdb76512011-09-07 11:43:16 -07001469 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->SetRegisterType(inst->VRegA_31i(),
1471 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001472 break;
1473 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 work_line_->SetRegisterType(inst->VRegA_21h(),
1475 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001478 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001480 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1481 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001483 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 }
1485 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001486 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1488 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 break;
1491 }
1492 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1495 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001497 break;
1498 }
1499 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001501 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1502 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001504 break;
1505 }
jeffhaobdb76512011-09-07 11:43:16 -07001506 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001507 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1508 break;
jeffhaobdb76512011-09-07 11:43:16 -07001509 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001510 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001511 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001513 // Get type from instruction if unresolved then we need an access check
1514 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001515 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001516 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001518 res_type.IsConflict() ? res_type
1519 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 }
jeffhaobdb76512011-09-07 11:43:16 -07001522 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001523 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001524 break;
1525 case Instruction::MONITOR_EXIT:
1526 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001527 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001528 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001530 * to the need to handle asynchronous exceptions, a now-deprecated
1531 * feature that Dalvik doesn't support.)
1532 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001533 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001534 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001535 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001536 * structured locking checks are working, the former would have
1537 * failed on the -enter instruction, and the latter is impossible.
1538 *
1539 * This is fortunate, because issue 3221411 prevents us from
1540 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001541 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001542 * some catch blocks (which will show up as "dead" code when
1543 * we skip them here); if we can't, then the code path could be
1544 * "live" so we still need to check it.
1545 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001546 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001548 break;
1549
Ian Rogers28ad40d2011-10-27 15:19:26 -07001550 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001552 /*
1553 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1554 * could be a "upcast" -- not expected, so we don't try to address it.)
1555 *
1556 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001558 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001559 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1560 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1561 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001562 if (res_type.IsConflict()) {
1563 DCHECK_NE(failures_.size(), 0U);
1564 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001565 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001566 }
1567 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001568 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001569 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1571 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001572 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001573 if (is_checkcast) {
1574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1575 } else {
1576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1577 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001578 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 if (is_checkcast) {
1580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1581 } else {
1582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1583 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001584 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001585 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001588 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001589 }
jeffhaobdb76512011-09-07 11:43:16 -07001590 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001591 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 }
1593 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001595 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001596 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 }
1601 }
1602 break;
1603 }
1604 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001606 if (res_type.IsConflict()) {
1607 DCHECK_NE(failures_.size(), 0U);
1608 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001609 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001610 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1611 // can't create an instance of an interface or abstract class */
1612 if (!res_type.IsInstantiableTypes()) {
1613 Fail(VERIFY_ERROR_INSTANTIATION)
1614 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001615 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001617 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1618 // Any registers holding previous allocations from this address that have not yet been
1619 // initialized must be marked invalid.
1620 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1621 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 break;
1624 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001625 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001627 break;
1628 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001630 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001631 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001632 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001634 just_set_result = true; // Filled new array range sets result register
1635 break;
jeffhaobdb76512011-09-07 11:43:16 -07001636 case Instruction::CMPL_FLOAT:
1637 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001639 break;
1640 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001642 break;
1643 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001645 break;
1646 case Instruction::CMPL_DOUBLE:
1647 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001650 break;
1651 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001652 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001653 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001654 break;
1655 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001657 break;
1658 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001660 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001661 break;
1662 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001664 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001665 break;
1666 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001671 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001672 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001673 }
1674 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 }
jeffhaobdb76512011-09-07 11:43:16 -07001676 case Instruction::GOTO:
1677 case Instruction::GOTO_16:
1678 case Instruction::GOTO_32:
1679 /* no effect on or use of registers */
1680 break;
1681
1682 case Instruction::PACKED_SWITCH:
1683 case Instruction::SPARSE_SWITCH:
1684 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
1687
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 case Instruction::FILL_ARRAY_DATA: {
1689 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001691 /* array_type can be null if the reg type is Zero */
1692 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001693 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001695 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001696 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1697 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001698 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1700 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 } else {
jeffhao457cc512012-02-02 16:55:13 -08001702 // Now verify if the element width in the table matches the element width declared in
1703 // the array
1704 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1705 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001707 } else {
1708 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1709 // Since we don't compress the data in Dex, expect to see equal width of data stored
1710 // in the table and expected from the array class.
1711 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1713 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001714 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 }
1716 }
jeffhaobdb76512011-09-07 11:43:16 -07001717 }
1718 }
1719 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1724 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 bool mismatch = false;
1726 if (reg_type1.IsZero()) { // zero then integral or reference expected
1727 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1728 } else if (reg_type1.IsReferenceTypes()) { // both references?
1729 mismatch = !reg_type2.IsReferenceTypes();
1730 } else { // both integral?
1731 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1732 }
1733 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1735 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001736 }
1737 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::IF_LT:
1740 case Instruction::IF_GE:
1741 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1744 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1747 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001748 }
1749 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
jeffhaobdb76512011-09-07 11:43:16 -07001751 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001757
1758 // Find previous instruction - its existence is a precondition to peephole optimization.
1759 uint32_t prev_idx = 0;
1760 if (0 != work_insn_idx_) {
1761 prev_idx = work_insn_idx_ - 1;
1762 while(0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1763 prev_idx--;
1764 }
1765 CHECK(insn_flags_[prev_idx].IsOpcode());
1766 } else {
1767 break;
1768 }
1769
1770 const Instruction* prev_inst = Instruction::At(code_item_->insns_+prev_idx);
1771
1772 /* Check for peep-hole pattern of:
1773 * ...;
1774 * instance-of vX, vO, T;
1775 * ifXXX vX, b ;
1776 * ...;
1777 * b: INST;
1778 * ...;
1779 * and sharpen the type for either the fall-through or the branch case.
1780 */
1781 if (!CurrentInsnFlags()->IsBranchTarget()) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001782 if ((Instruction::INSTANCE_OF == prev_inst->Opcode())
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 && (inst->VRegA_21t() == prev_inst->VRegA_22c())) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001784 // Check that the we are not attempting conversion to interface types,
1785 // which is not done because of the multiple inheritance implications.
1786 const RegType& cast_type =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 ResolveClassAndCheckAccess(prev_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001788
1789 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 if (inst->Opcode() == Instruction::IF_EQZ) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001791 fallthrough_line.reset(new RegisterLine(code_item_->registers_size_, this));
1792 fallthrough_line->CopyFromLine(work_line_.get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 fallthrough_line->SetRegisterType(prev_inst->VRegB_22c(), cast_type);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001794 } else {
1795 branch_line.reset(new RegisterLine(code_item_->registers_size_, this));
1796 branch_line->CopyFromLine(work_line_.get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 branch_line->SetRegisterType(prev_inst->VRegB_22c(), cast_type);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001798 }
1799 }
1800 }
1801 }
1802
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 }
jeffhaobdb76512011-09-07 11:43:16 -07001805 case Instruction::IF_LTZ:
1806 case Instruction::IF_GEZ:
1807 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1812 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 }
jeffhaobdb76512011-09-07 11:43:16 -07001814 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 break;
jeffhaobdb76512011-09-07 11:43:16 -07001819 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 break;
jeffhaobdb76512011-09-07 11:43:16 -07001822 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 break;
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001827 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 break;
jeffhaobdb76512011-09-07 11:43:16 -07001831 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001832 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 break;
1834 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001835 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001836 break;
1837
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 break;
1841 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 break;
1844 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001845 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 break;
1847 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001848 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001849 break;
1850 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001851 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001852 break;
1853 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001854 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
1856 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001858 break;
1859
jeffhaobdb76512011-09-07 11:43:16 -07001860 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001861 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 break;
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 break;
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001867 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 break;
jeffhaobdb76512011-09-07 11:43:16 -07001869 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001870 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 break;
1872 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001873 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001876 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001877 break;
1878 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 break;
1885 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 break;
1888 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 break;
1891 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001892 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
1897 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001898 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 break;
jeffhaobdb76512011-09-07 11:43:16 -07001900 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001901 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903
jeffhaobdb76512011-09-07 11:43:16 -07001904 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 break;
jeffhaobdb76512011-09-07 11:43:16 -07001907 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001908 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 break;
jeffhaobdb76512011-09-07 11:43:16 -07001910 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 break;
jeffhaobdb76512011-09-07 11:43:16 -07001913 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 break;
1916 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001917 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001918 break;
1919 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001920 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001921 break;
1922 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001923 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 break;
1925
1926 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 break;
1929 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001930 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 break;
1932 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 break;
1935 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001936 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001937 break;
1938 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001939 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001940 break;
1941 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001942 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001943 break;
1944 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001945 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
1947
1948 case Instruction::INVOKE_VIRTUAL:
1949 case Instruction::INVOKE_VIRTUAL_RANGE:
1950 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001952 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
1953 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1954 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
1955 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1956 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001957 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 const char* descriptor;
1959 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001961 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1962 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1963 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1964 } else {
1965 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001966 }
Ian Rogersb4903572012-10-11 11:52:56 -07001967 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 if (!return_type.IsLowHalf()) {
1969 work_line_->SetResultRegisterType(return_type);
1970 } else {
1971 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1972 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001973 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001974 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
1979 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001980 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001981 const char* return_type_descriptor;
1982 bool is_constructor;
1983 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07001985 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1986 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1987 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1988 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1989 } else {
1990 is_constructor = called_method->IsConstructor();
1991 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1992 }
1993 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001994 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 * Some additional checks when calling a constructor. We know from the invocation arg check
1996 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1997 * that to require that called_method->klass is the same as this->klass or this->super,
1998 * allowing the latter only if the "this" argument is the same as the "this" argument to
1999 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002000 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002002 if (this_type.IsConflict()) // failure.
2003 break;
jeffhaobdb76512011-09-07 11:43:16 -07002004
jeffhaob57e9522012-04-26 18:08:21 -07002005 /* no null refs allowed (?) */
2006 if (this_type.IsZero()) {
2007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2008 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002009 }
jeffhaob57e9522012-04-26 18:08:21 -07002010
2011 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002012 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2013 // TODO: re-enable constructor type verification
2014 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002015 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002016 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2017 // break;
2018 // }
jeffhaob57e9522012-04-26 18:08:21 -07002019
2020 /* arg must be an uninitialized reference */
2021 if (!this_type.IsUninitializedTypes()) {
2022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2023 << this_type;
2024 break;
2025 }
2026
2027 /*
2028 * Replace the uninitialized reference with an initialized one. We need to do this for all
2029 * registers that have the same object instance in them, not just the "this" register.
2030 */
2031 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002032 }
Ian Rogersb4903572012-10-11 11:52:56 -07002033 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2034 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002035 if (!return_type.IsLowHalf()) {
2036 work_line_->SetResultRegisterType(return_type);
2037 } else {
2038 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2039 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002040 just_set_result = true;
2041 break;
2042 }
2043 case Instruction::INVOKE_STATIC:
2044 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2046 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002047 const char* descriptor;
2048 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002050 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2051 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002052 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002053 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002054 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002055 }
Ian Rogersb4903572012-10-11 11:52:56 -07002056 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002057 if (!return_type.IsLowHalf()) {
2058 work_line_->SetResultRegisterType(return_type);
2059 } else {
2060 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2061 }
jeffhaobdb76512011-09-07 11:43:16 -07002062 just_set_result = true;
2063 }
2064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2068 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002069 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002070 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002071 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2072 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2073 << PrettyMethod(abs_method) << "'";
2074 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002075 }
Ian Rogers0d604842012-04-16 14:50:24 -07002076 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002077 /* Get the type of the "this" arg, which should either be a sub-interface of called
2078 * interface or Object (see comments in RegType::JoinClass).
2079 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002081 if (this_type.IsZero()) {
2082 /* null pointer always passes (and always fails at runtime) */
2083 } else {
2084 if (this_type.IsUninitializedTypes()) {
2085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2086 << this_type;
2087 break;
2088 }
2089 // In the past we have tried to assert that "called_interface" is assignable
2090 // from "this_type.GetClass()", however, as we do an imprecise Join
2091 // (RegType::JoinClass) we don't have full information on what interfaces are
2092 // implemented by "this_type". For example, two classes may implement the same
2093 // interfaces and have a common parent that doesn't implement the interface. The
2094 // join will set "this_type" to the parent class and a test that this implements
2095 // the interface will incorrectly fail.
2096 }
2097 /*
2098 * We don't have an object instance, so we can't find the concrete method. However, all of
2099 * the type information is in the abstract method, so we're good.
2100 */
2101 const char* descriptor;
2102 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002104 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2105 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2106 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2107 } else {
2108 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2109 }
Ian Rogersb4903572012-10-11 11:52:56 -07002110 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002111 if (!return_type.IsLowHalf()) {
2112 work_line_->SetResultRegisterType(return_type);
2113 } else {
2114 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2115 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002116 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 }
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::NEG_INT:
2120 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
2123 case Instruction::NEG_LONG:
2124 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002126 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002133 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002137 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
2142 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
2146 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002148 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
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::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002163 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002167 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002171 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
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::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
2187 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190
2191 case Instruction::ADD_INT:
2192 case Instruction::SUB_INT:
2193 case Instruction::MUL_INT:
2194 case Instruction::REM_INT:
2195 case Instruction::DIV_INT:
2196 case Instruction::SHL_INT:
2197 case Instruction::SHR_INT:
2198 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002200 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::AND_INT:
2203 case Instruction::OR_INT:
2204 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002206 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208 case Instruction::ADD_LONG:
2209 case Instruction::SUB_LONG:
2210 case Instruction::MUL_LONG:
2211 case Instruction::DIV_LONG:
2212 case Instruction::REM_LONG:
2213 case Instruction::AND_LONG:
2214 case Instruction::OR_LONG:
2215 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002217 reg_types_.LongLo(), reg_types_.LongHi(),
2218 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::SHL_LONG:
2221 case Instruction::SHR_LONG:
2222 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::ADD_FLOAT:
2228 case Instruction::SUB_FLOAT:
2229 case Instruction::MUL_FLOAT:
2230 case Instruction::DIV_FLOAT:
2231 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::ADD_DOUBLE:
2235 case Instruction::SUB_DOUBLE:
2236 case Instruction::MUL_DOUBLE:
2237 case Instruction::DIV_DOUBLE:
2238 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2241 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002242 break;
2243 case Instruction::ADD_INT_2ADDR:
2244 case Instruction::SUB_INT_2ADDR:
2245 case Instruction::MUL_INT_2ADDR:
2246 case Instruction::REM_INT_2ADDR:
2247 case Instruction::SHL_INT_2ADDR:
2248 case Instruction::SHR_INT_2ADDR:
2249 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::AND_INT_2ADDR:
2253 case Instruction::OR_INT_2ADDR:
2254 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::DIV_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::ADD_LONG_2ADDR:
2261 case Instruction::SUB_LONG_2ADDR:
2262 case Instruction::MUL_LONG_2ADDR:
2263 case Instruction::DIV_LONG_2ADDR:
2264 case Instruction::REM_LONG_2ADDR:
2265 case Instruction::AND_LONG_2ADDR:
2266 case Instruction::OR_LONG_2ADDR:
2267 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 reg_types_.LongLo(), reg_types_.LongHi(),
2270 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272 case Instruction::SHL_LONG_2ADDR:
2273 case Instruction::SHR_LONG_2ADDR:
2274 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002275 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
2278 case Instruction::ADD_FLOAT_2ADDR:
2279 case Instruction::SUB_FLOAT_2ADDR:
2280 case Instruction::MUL_FLOAT_2ADDR:
2281 case Instruction::DIV_FLOAT_2ADDR:
2282 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::ADD_DOUBLE_2ADDR:
2286 case Instruction::SUB_DOUBLE_2ADDR:
2287 case Instruction::MUL_DOUBLE_2ADDR:
2288 case Instruction::DIV_DOUBLE_2ADDR:
2289 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2292 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::ADD_INT_LIT16:
2295 case Instruction::RSUB_INT:
2296 case Instruction::MUL_INT_LIT16:
2297 case Instruction::DIV_INT_LIT16:
2298 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::AND_INT_LIT16:
2302 case Instruction::OR_INT_LIT16:
2303 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::ADD_INT_LIT8:
2307 case Instruction::RSUB_INT_LIT8:
2308 case Instruction::MUL_INT_LIT8:
2309 case Instruction::DIV_INT_LIT8:
2310 case Instruction::REM_INT_LIT8:
2311 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002313 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::AND_INT_LIT8:
2317 case Instruction::OR_INT_LIT8:
2318 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002323 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002324 case Instruction::UNUSED_EE:
2325 case Instruction::UNUSED_EF:
2326 case Instruction::UNUSED_F2:
2327 case Instruction::UNUSED_F3:
2328 case Instruction::UNUSED_F4:
2329 case Instruction::UNUSED_F5:
2330 case Instruction::UNUSED_F6:
2331 case Instruction::UNUSED_F7:
2332 case Instruction::UNUSED_F8:
2333 case Instruction::UNUSED_F9:
2334 case Instruction::UNUSED_FA:
2335 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002336 case Instruction::UNUSED_F0:
2337 case Instruction::UNUSED_F1:
2338 case Instruction::UNUSED_E3:
2339 case Instruction::UNUSED_E8:
2340 case Instruction::UNUSED_E7:
2341 case Instruction::UNUSED_E4:
2342 case Instruction::UNUSED_E9:
2343 case Instruction::UNUSED_FC:
2344 case Instruction::UNUSED_E5:
2345 case Instruction::UNUSED_EA:
2346 case Instruction::UNUSED_FD:
2347 case Instruction::UNUSED_E6:
2348 case Instruction::UNUSED_EB:
2349 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002350 case Instruction::UNUSED_3E:
2351 case Instruction::UNUSED_3F:
2352 case Instruction::UNUSED_40:
2353 case Instruction::UNUSED_41:
2354 case Instruction::UNUSED_42:
2355 case Instruction::UNUSED_43:
2356 case Instruction::UNUSED_73:
2357 case Instruction::UNUSED_79:
2358 case Instruction::UNUSED_7A:
2359 case Instruction::UNUSED_EC:
2360 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363
2364 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002365 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002366 * complain if an instruction is missing (which is desirable).
2367 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002369
Ian Rogersad0b3a32012-04-16 14:50:24 -07002370 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002372 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002373 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 /* immediate failure, reject class */
2376 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2377 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002378 } else if (have_pending_runtime_throw_failure_) {
2379 /* slow path will throw, mark following code as unreachable */
2380 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002381 }
jeffhaobdb76512011-09-07 11:43:16 -07002382 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 * If we didn't just set the result register, clear it out. This ensures that you can only use
2384 * "move-result" immediately after the result is set. (We could check this statically, but it's
2385 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002386 */
2387 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002389 }
2390
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002391
jeffhaobdb76512011-09-07 11:43:16 -07002392
2393 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002394 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002395 *
2396 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002397 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002398 * somebody could get a reference field, check it for zero, and if the
2399 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002401 * that, and will reject the code.
2402 *
2403 * TODO: avoid re-fetching the branch target
2404 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002406 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002408 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002410 return false;
2411 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002413 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002414 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 }
jeffhaobdb76512011-09-07 11:43:16 -07002416 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002417 if (NULL != branch_line.get()) {
2418 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2419 return false;
2420 }
2421 } else {
2422 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2423 return false;
2424 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 }
jeffhaobdb76512011-09-07 11:43:16 -07002426 }
2427
2428 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002429 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002430 *
2431 * We've already verified that the table is structurally sound, so we
2432 * just need to walk through and tag the targets.
2433 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002434 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002435 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2436 const uint16_t* switch_insns = insns + offset_to_switch;
2437 int switch_count = switch_insns[1];
2438 int offset_to_targets, targ;
2439
2440 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2441 /* 0 = sig, 1 = count, 2/3 = first key */
2442 offset_to_targets = 4;
2443 } else {
2444 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002445 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002446 offset_to_targets = 2 + 2 * switch_count;
2447 }
2448
2449 /* verify each switch target */
2450 for (targ = 0; targ < switch_count; targ++) {
2451 int offset;
2452 uint32_t abs_offset;
2453
2454 /* offsets are 32-bit, and only partly endian-swapped */
2455 offset = switch_insns[offset_to_targets + targ * 2] |
2456 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 abs_offset = work_insn_idx_ + offset;
2458 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002459 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002460 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 }
2462 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002463 return false;
2464 }
2465 }
2466
2467 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2469 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002470 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002471 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002473 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002474
Ian Rogers0571d352011-11-03 19:51:38 -07002475 for (; iterator.HasNext(); iterator.Next()) {
2476 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 within_catch_all = true;
2478 }
jeffhaobdb76512011-09-07 11:43:16 -07002479 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2481 * "work_regs", because at runtime the exception will be thrown before the instruction
2482 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002483 */
Ian Rogers0571d352011-11-03 19:51:38 -07002484 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002485 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 }
jeffhaobdb76512011-09-07 11:43:16 -07002487 }
2488
2489 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2491 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002492 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002494 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 * The state in work_line reflects the post-execution state. If the current instruction is a
2496 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002497 * it will do so before grabbing the lock).
2498 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002499 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002500 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002502 return false;
2503 }
2504 }
2505 }
2506
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002507 /* Handle "continue". Tag the next consecutive instruction.
2508 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2509 * because it changes work_line_ when performing peephole optimization
2510 * and this change should not be used in those cases.
2511 */
2512 if ((opcode_flags & Instruction::kContinue) != 0) {
2513 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2514 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2516 return false;
2517 }
2518 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2519 // next instruction isn't one.
2520 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2521 return false;
2522 }
2523 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2524 if (next_line != NULL) {
2525 if (NULL != fallthrough_line.get()) {
2526 // Make workline consistent with fallthrough computed from peephole optimization.
2527 work_line_->CopyFromLine(fallthrough_line.get());
2528 }
2529 // Merge registers into what we have for the next instruction,
2530 // and set the "changed" flag if needed.
2531 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2532 return false;
2533 }
2534 } else {
2535 /*
2536 * We're not recording register data for the next instruction, so we don't know what the
2537 * prior state was. We have to assume that something has changed and re-evaluate it.
2538 */
2539 insn_flags_[next_insn_idx].SetChanged();
2540 }
2541 }
2542
jeffhaod1f0fde2011-09-08 17:25:33 -07002543 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002544 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002545 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 return false;
2547 }
jeffhaobdb76512011-09-07 11:43:16 -07002548 }
2549
2550 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002551 * Update start_guess. Advance to the next instruction of that's
2552 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002553 * neither of those exists we're in a return or throw; leave start_guess
2554 * alone and let the caller sort it out.
2555 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002556 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002558 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002559 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002561 }
2562
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2564 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002565
2566 return true;
2567}
2568
Ian Rogers776ac1f2012-04-13 23:36:36 -07002569const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002570 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002571 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002572 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 const RegType& result =
Ian Rogers637c65b2013-05-31 11:46:00 -07002574 klass != NULL ? reg_types_.FromClass(descriptor, klass, klass->IsFinal())
Ian Rogersb4903572012-10-11 11:52:56 -07002575 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002576 if (result.IsConflict()) {
2577 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2578 << "' in " << referrer;
2579 return result;
2580 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002581 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002582 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002583 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002585 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002586 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002587 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002588 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002589 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002590 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002591}
2592
Ian Rogers776ac1f2012-04-13 23:36:36 -07002593const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002594 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002596 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2598 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002599 CatchHandlerIterator iterator(handlers_ptr);
2600 for (; iterator.HasNext(); iterator.Next()) {
2601 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2602 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002603 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002605 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002606 if (common_super == NULL) {
2607 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2608 // as that is caught at runtime
2609 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002610 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002611 // We don't know enough about the type and the common path merge will result in
2612 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002613 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002614 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002615 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002616 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002618 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002619 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 }
2621 }
2622 }
2623 }
Ian Rogers0571d352011-11-03 19:51:38 -07002624 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 }
2626 }
2627 if (common_super == NULL) {
2628 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002629 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002630 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002632 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002633}
2634
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002635mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2636 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002637 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002638 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002639 if (klass_type.IsConflict()) {
2640 std::string append(" in attempt to access method ");
2641 append += dex_file_->GetMethodName(method_id);
2642 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002643 return NULL;
2644 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002645 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002646 return NULL; // Can't resolve Class so no more to do here
2647 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002648 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002649 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002650 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002652 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002653 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002654
2655 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002657 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 res_method = klass->FindInterfaceMethod(name, signature);
2659 } else {
2660 res_method = klass->FindVirtualMethod(name, signature);
2661 }
2662 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002663 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002665 // If a virtual or interface method wasn't found with the expected type, look in
2666 // the direct methods. This can happen when the wrong invoke type is used or when
2667 // a class has changed, and will be flagged as an error in later checks.
2668 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2669 res_method = klass->FindDirectMethod(name, signature);
2670 }
2671 if (res_method == NULL) {
2672 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2673 << PrettyDescriptor(klass) << "." << name
2674 << " " << signature;
2675 return NULL;
2676 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 }
2678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2680 // enforce them here.
2681 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2683 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 return NULL;
2685 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002686 // Disallow any calls to class initializers.
2687 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2689 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002690 return NULL;
2691 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002692 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002693 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002694 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002695 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002696 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002697 }
jeffhaode0d9c92012-02-27 13:58:13 -08002698 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2699 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2701 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002702 return NULL;
2703 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002704 // Check that interface methods match interface classes.
2705 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2706 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2707 << " is in an interface class " << PrettyClass(klass);
2708 return NULL;
2709 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2710 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2711 << " is in a non-interface class " << PrettyClass(klass);
2712 return NULL;
2713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 // See if the method type implied by the invoke instruction matches the access flags for the
2715 // target method.
2716 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2717 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2718 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2719 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002720 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2721 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 return NULL;
2723 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002724 return res_method;
2725}
2726
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2728 MethodType method_type,
2729 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002730 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002731 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2732 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002733 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2734 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002735 if (res_method == NULL) { // error or class is unresolved
2736 return NULL;
2737 }
2738
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2740 // has a vtable entry for the target method.
2741 if (is_super) {
2742 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002743 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002744 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002745 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002746 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002747 << " to super " << PrettyMethod(res_method);
2748 return NULL;
2749 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002750 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002751 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002752 MethodHelper mh(res_method);
2753 Fail(VERIFY_ERROR_NO_METHOD) << "invalid 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 " << super
2756 << "." << mh.GetName()
2757 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 return NULL;
2759 }
2760 }
2761 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2762 // match the call to the signature. Also, we might might be calling through an abstract method
2763 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002764 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 /* caught by static verifier */
2766 DCHECK(is_range || expected_args <= 5);
2767 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2770 return NULL;
2771 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002772
jeffhaobdb76512011-09-07 11:43:16 -07002773 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002774 * Check the "this" argument, which must be an instance of the class that declared the method.
2775 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2776 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002777 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002778 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002780 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002781 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 return NULL;
2783 }
2784 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 return NULL;
2787 }
2788 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002789 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07002790 const RegType& res_method_class = reg_types_.FromClass(ClassHelper(klass).GetDescriptor(),
2791 klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002792 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002794 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 return NULL;
2796 }
2797 }
2798 actual_args++;
2799 }
2800 /*
2801 * Process the target method's signature. This signature may or may not
2802 * have been verified, so we can't assume it's properly formed.
2803 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002804 MethodHelper mh(res_method);
2805 const DexFile::TypeList* params = mh.GetParameterTypeList();
2806 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002807 uint32_t arg[5];
2808 if (!is_range) {
2809 inst->GetArgs(arg);
2810 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002811 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002814 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2815 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 return NULL;
2817 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002818 const char* descriptor =
2819 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2820 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002822 << " missing signature component";
2823 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 }
Ian Rogersb4903572012-10-11 11:52:56 -07002825 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002826 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002827 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002828 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 }
2830 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2831 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002834 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 return NULL;
2836 } else {
2837 return res_method;
2838 }
2839}
2840
Sebastien Hertz5243e912013-05-21 10:55:07 +02002841void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled,
2842 bool is_range) {
2843 uint32_t type_idx;
2844 if (!is_filled) {
2845 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
2846 type_idx = inst->VRegC_22c();
2847 } else if (!is_range) {
2848 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
2849 type_idx = inst->VRegB_35c();
2850 } else {
2851 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
2852 type_idx = inst->VRegB_3rc();
2853 }
2854 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002855 if (res_type.IsConflict()) { // bad class
2856 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002857 } else {
2858 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2859 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002861 } else if (!is_filled) {
2862 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002863 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002864 /* set register type to array class */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002865 work_line_->SetRegisterType(inst->VRegA_22c(), res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002866 } else {
2867 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2868 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002869 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002870 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
2871 uint32_t arg[5];
2872 if (!is_range) {
2873 inst->GetArgs(arg);
2874 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002875 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002876 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002877 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002878 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002879 return;
2880 }
2881 }
2882 // filled-array result goes into "result" register
2883 work_line_->SetResultRegisterType(res_type);
2884 }
2885 }
2886}
2887
Sebastien Hertz5243e912013-05-21 10:55:07 +02002888void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002889 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002890 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002894 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002895 if (array_type.IsZero()) {
2896 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2897 // instruction type. TODO: have a proper notion of bottom here.
2898 if (!is_primitive || insn_type.IsCategory1Types()) {
2899 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02002900 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002902 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02002903 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002904 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002905 }
jeffhaofc3144e2012-02-01 17:21:15 -08002906 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002908 } else {
2909 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002911 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002913 << " source for aget-object";
2914 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002916 << " source for category 1 aget";
2917 } else if (is_primitive && !insn_type.Equals(component_type) &&
2918 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002919 (insn_type.IsLong() && component_type.IsDouble()))) {
2920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2921 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002922 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002923 // Use knowledge of the field type which is stronger than the type inferred from the
2924 // instruction, which can't differentiate object types and ints from floats, longs from
2925 // doubles.
2926 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002927 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002928 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002929 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002930 component_type.HighHalf(&reg_types_));
2931 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 }
2933 }
2934 }
2935}
2936
Sebastien Hertz5243e912013-05-21 10:55:07 +02002937void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002939 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002943 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002944 if (array_type.IsZero()) {
2945 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2946 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002947 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002949 } else {
2950 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002952 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002954 << " source for aput-object";
2955 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002957 << " source for category 1 aput";
2958 } else if (is_primitive && !insn_type.Equals(component_type) &&
2959 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2960 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002962 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002964 // The instruction agrees with the type of array, confirm the value to be stored does too
2965 // Note: we use the instruction type (rather than the component type) for aput-object as
2966 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02002967 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 }
2969 }
2970 }
2971}
2972
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002973mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002974 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2975 // Check access to class
2976 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 if (klass_type.IsConflict()) { // bad class
2978 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2979 field_idx, dex_file_->GetFieldName(field_id),
2980 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002981 return NULL;
2982 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002983 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002984 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002985 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002986 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002987 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07002989 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07002990 << dex_file_->GetFieldName(field_id) << ") in "
2991 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 DCHECK(Thread::Current()->IsExceptionPending());
2993 Thread::Current()->ClearException();
2994 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2996 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 return NULL;
3000 } else if (!field->IsStatic()) {
3001 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3002 return NULL;
3003 } else {
3004 return field;
3005 }
3006}
3007
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003008mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003009 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3010 // Check access to class
3011 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 if (klass_type.IsConflict()) {
3013 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3014 field_idx, dex_file_->GetFieldName(field_id),
3015 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003016 return NULL;
3017 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003018 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003019 return NULL; // Can't resolve Class so no more to do here
3020 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003021 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003024 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003025 << dex_file_->GetFieldName(field_id) << ") in "
3026 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 DCHECK(Thread::Current()->IsExceptionPending());
3028 Thread::Current()->ClearException();
3029 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3031 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003033 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 return NULL;
3035 } else if (field->IsStatic()) {
3036 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3037 << " to not be static";
3038 return NULL;
3039 } else if (obj_type.IsZero()) {
3040 // Cannot infer and check type, however, access will cause null pointer exception
3041 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003042 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003043 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003044 const RegType& field_klass =
3045 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
3046 klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003047 if (obj_type.IsUninitializedTypes() &&
3048 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3049 !field_klass.Equals(GetDeclaringClass()))) {
3050 // Field accesses through uninitialized references are only allowable for constructors where
3051 // the field is declared in this class
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3053 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003054 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 return NULL;
3056 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3057 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3058 // of C1. For resolution to occur the declared class of the field must be compatible with
3059 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3060 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3061 << " from object of type " << obj_type;
3062 return NULL;
3063 } else {
3064 return field;
3065 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 }
3067}
3068
Sebastien Hertz5243e912013-05-21 10:55:07 +02003069void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3070 bool is_primitive, bool is_static) {
3071 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003072 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003073 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003074 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003075 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003076 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003077 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003078 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003079 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003080 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 if (field != NULL) {
3082 descriptor = FieldHelper(field).GetTypeDescriptor();
3083 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003084 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3086 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3087 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003088 }
Ian Rogersb4903572012-10-11 11:52:56 -07003089 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003090 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 if (is_primitive) {
3092 if (field_type.Equals(insn_type) ||
3093 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3094 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3095 // expected that read is of the correct primitive type or that int reads are reading
3096 // floats or long reads are reading doubles
3097 } else {
3098 // This is a global failure rather than a class change failure as the instructions and
3099 // the descriptors for the type should have been consistent within the same file at
3100 // compile time
3101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3102 << " to be of type '" << insn_type
3103 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003104 return;
3105 }
3106 } else {
3107 if (!insn_type.IsAssignableFrom(field_type)) {
3108 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3109 << " to be compatible with type '" << insn_type
3110 << "' but found type '" << field_type
3111 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003112 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003113 return;
3114 }
3115 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003116 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003117 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003118 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003119 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003120 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003121}
3122
Sebastien Hertz5243e912013-05-21 10:55:07 +02003123void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3124 bool is_primitive, bool is_static) {
3125 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003126 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003127 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003128 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003129 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003130 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003131 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003132 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003133 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003134 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003135 if (field != NULL) {
3136 descriptor = FieldHelper(field).GetTypeDescriptor();
3137 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003138 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3140 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3141 loader = class_loader_;
3142 }
Ian Rogersb4903572012-10-11 11:52:56 -07003143 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003144 if (field != NULL) {
3145 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3146 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3147 << " from other class " << GetDeclaringClass();
3148 return;
3149 }
3150 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003151 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003152 if (is_primitive) {
3153 // Primitive field assignability rules are weaker than regular assignability rules
3154 bool instruction_compatible;
3155 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003156 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003157 if (field_type.IsIntegralTypes()) {
3158 instruction_compatible = insn_type.IsIntegralTypes();
3159 value_compatible = value_type.IsIntegralTypes();
3160 } else if (field_type.IsFloat()) {
3161 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3162 value_compatible = value_type.IsFloatTypes();
3163 } else if (field_type.IsLong()) {
3164 instruction_compatible = insn_type.IsLong();
3165 value_compatible = value_type.IsLongTypes();
3166 } else if (field_type.IsDouble()) {
3167 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3168 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003169 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003170 instruction_compatible = false; // reference field with primitive store
3171 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003173 if (!instruction_compatible) {
3174 // This is a global failure rather than a class change failure as the instructions and
3175 // the descriptors for the type should have been consistent within the same file at
3176 // compile time
3177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3178 << " to be of type '" << insn_type
3179 << "' but found type '" << field_type
3180 << "' in put";
3181 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003183 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003185 << " of type " << value_type
3186 << " but expected " << field_type
3187 << " for store to " << PrettyField(field) << " in put";
3188 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003190 } else {
3191 if (!insn_type.IsAssignableFrom(field_type)) {
3192 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3193 << " to be compatible with type '" << insn_type
3194 << "' but found type '" << field_type
3195 << "' in put-object";
3196 return;
3197 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003198 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003199 }
3200}
3201
Ian Rogers776ac1f2012-04-13 23:36:36 -07003202bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 return false;
3206 }
3207 return true;
3208}
3209
Ian Rogers776ac1f2012-04-13 23:36:36 -07003210bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 bool changed = true;
3212 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3213 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003214 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 * We haven't processed this instruction before, and we haven't touched the registers here, so
3216 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3217 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003218 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003220 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003221 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3222 if (gDebugVerify) {
3223 copy->CopyFromLine(target_line);
3224 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003226 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003228 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003229 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003230 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003231 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3232 << *copy.get() << " MERGE\n"
3233 << *merge_line << " ==\n"
3234 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003235 }
3236 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 if (changed) {
3238 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003239 }
3240 return true;
3241}
3242
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003243InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003244 return &insn_flags_[work_insn_idx_];
3245}
3246
Ian Rogersad0b3a32012-04-16 14:50:24 -07003247const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3250 uint16_t return_type_idx = proto_id.return_type_idx_;
3251 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003252 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003253}
3254
3255const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003256 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003257 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003259 if (mirror_method_ != NULL) {
3260 mirror::Class* klass = mirror_method_->GetDeclaringClass();
3261 declaring_class_ = &reg_types_.FromClass(descriptor, klass, klass->IsFinal());
3262 } else {
3263 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3264 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003265 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003266 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267}
3268
Ian Rogers776ac1f2012-04-13 23:36:36 -07003269void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 size_t* log2_max_gc_pc) {
3271 size_t local_gc_points = 0;
3272 size_t max_insn = 0;
3273 size_t max_ref_reg = -1;
3274 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003275 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 local_gc_points++;
3277 max_insn = i;
3278 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003279 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003280 }
3281 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003282 *gc_points = local_gc_points;
3283 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3284 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003285 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 i++;
3287 }
3288 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003289}
3290
Ian Rogersd0583802013-06-01 10:51:46 -07003291MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003292
3293 // It is risky to rely on reg_types for sharpening in cases of soft
3294 // verification, we might end up sharpening to a wrong implementation. Just abort.
3295 if (!failure_messages_.empty()) {
3296 return NULL;
3297 }
3298
Ian Rogersd0583802013-06-01 10:51:46 -07003299 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003300 const uint16_t* insns = code_item_->insns_ ;
3301 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003302 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003303
Ian Rogersd0583802013-06-01 10:51:46 -07003304 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003305 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3306 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3307 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3308 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3309
Ian Rogersd0583802013-06-01 10:51:46 -07003310 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003311 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003312 }
Ian Rogersd0583802013-06-01 10:51:46 -07003313 // Get reg type for register holding the reference to the object that will be dispatched upon.
3314 uint32_t dex_pc = inst->GetDexPc(insns);
3315 RegisterLine* line = reg_table_.GetLine(dex_pc);
3316 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3317 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3318 const RegType&
3319 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003320
Ian Rogersd0583802013-06-01 10:51:46 -07003321 if (!reg_type.HasClass()) {
3322 // We will compute devirtualization information only when we know the Class of the reg type.
3323 continue;
3324 }
3325 mirror::Class* reg_class = reg_type.GetClass();
3326 if (reg_class->IsInterface()) {
3327 // We can't devirtualize when the known type of the register is an interface.
3328 continue;
3329 }
3330 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3331 // We can't devirtualize abstract classes except on arrays of abstract classes.
3332 continue;
3333 }
3334 mirror::AbstractMethod* abstract_method =
3335 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003336 if(abstract_method == NULL) {
3337 // If the method is not found in the cache this means that it was never found
3338 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3339 continue;
3340 }
3341 // Find the concrete method.
3342 mirror::AbstractMethod* concrete_method = NULL;
3343 if (is_interface) {
3344 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3345 }
3346 if (is_virtual) {
3347 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3348 }
Ian Rogersd0583802013-06-01 10:51:46 -07003349 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3350 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003351 continue;
3352 }
Ian Rogersd0583802013-06-01 10:51:46 -07003353 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3354 concrete_method->GetDeclaringClass()->IsFinal()) {
3355 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3356 // overridden record the target to be used in the compiler driver.
3357 if (pc_to_concrete_method_map.get() == NULL) {
3358 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3359 }
3360 CompilerDriver::MethodReference concrete_ref(
3361 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3362 concrete_method->GetDexMethodIndex());
3363 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3364 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003365 }
Ian Rogersd0583802013-06-01 10:51:46 -07003366 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003367}
3368
Ian Rogers776ac1f2012-04-13 23:36:36 -07003369const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 size_t num_entries, ref_bitmap_bits, pc_bits;
3371 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3372 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003373 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003377 return NULL;
3378 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003379 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3380 // There are 2 bytes to encode the number of entries
3381 if (num_entries >= 65536) {
3382 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003385 return NULL;
3386 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003388 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003389 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003390 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003392 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003393 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003395 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3399 return NULL;
3400 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003401 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003402 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 return NULL;
3406 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003407 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003409 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3410 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003411 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003412 table->push_back(num_entries & 0xFF);
3413 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003416 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003417 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003419 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 }
3421 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003422 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 }
3424 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003425 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 return table;
3427}
jeffhaoa0a764a2011-09-16 10:43:38 -07003428
Ian Rogers776ac1f2012-04-13 23:36:36 -07003429void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3431 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003432 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003434 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003436 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003438 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3440 map_index++;
3441 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003442 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003443 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 CHECK_LT(j / 8, map.RegWidth());
3445 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3446 } else if ((j / 8) < map.RegWidth()) {
3447 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3448 } else {
3449 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3450 }
3451 }
3452 } else {
3453 CHECK(reg_bitmap == NULL);
3454 }
3455 }
3456}
jeffhaoa0a764a2011-09-16 10:43:38 -07003457
Ian Rogers637c65b2013-05-31 11:46:00 -07003458void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3459 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003460 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003461 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003462 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3463 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003464 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003465 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003466 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003467 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003468 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003469 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003470}
3471
Ian Rogers637c65b2013-05-31 11:46:00 -07003472const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3473 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3474 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3475 if (it == dex_gc_maps_->end()) {
3476 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3477 return NULL;
3478 }
3479 CHECK(it->second != NULL);
3480 return it->second;
3481}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003482
Ian Rogers637c65b2013-05-31 11:46:00 -07003483void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003484 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003485 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003486 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3487 if (it != devirt_maps_->end()) {
3488 delete it->second;
3489 devirt_maps_->erase(it);
3490 }
3491
3492 devirt_maps_->Put(ref, devirt_map);
3493 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3494}
3495
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003496const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3497 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003498 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003499 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3500 if (it == devirt_maps_->end()) {
3501 return NULL;
3502 }
3503
3504 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003505 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003506 if(pc_to_concrete_method != it->second->end()) {
3507 return &(pc_to_concrete_method->second);
3508 } else {
3509 return NULL;
3510 }
3511}
3512
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003513std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3514 RegisterLine* line = reg_table_.GetLine(dex_pc);
3515 std::vector<int32_t> result;
3516 for (size_t i = 0; i < line->NumRegs(); ++i) {
3517 const RegType& type = line->GetRegisterType(i);
3518 if (type.IsConstant()) {
3519 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3520 result.push_back(type.ConstantValue());
3521 } else if (type.IsConstantLo()) {
3522 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3523 result.push_back(type.ConstantValueLo());
3524 } else if (type.IsConstantHi()) {
3525 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3526 result.push_back(type.ConstantValueHi());
3527 } else if (type.IsIntegralTypes()) {
3528 result.push_back(kIntVReg);
3529 result.push_back(0);
3530 } else if (type.IsFloat()) {
3531 result.push_back(kFloatVReg);
3532 result.push_back(0);
3533 } else if (type.IsLong()) {
3534 result.push_back(kLongLoVReg);
3535 result.push_back(0);
3536 result.push_back(kLongHiVReg);
3537 result.push_back(0);
3538 ++i;
3539 } else if (type.IsDouble()) {
3540 result.push_back(kDoubleLoVReg);
3541 result.push_back(0);
3542 result.push_back(kDoubleHiVReg);
3543 result.push_back(0);
3544 ++i;
3545 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3546 result.push_back(kUndefined);
3547 result.push_back(0);
3548 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003549 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003550 result.push_back(kReferenceVReg);
3551 result.push_back(0);
3552 }
3553 }
3554 return result;
3555}
3556
Ian Rogers637c65b2013-05-31 11:46:00 -07003557ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003558MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003559
Ian Rogers637c65b2013-05-31 11:46:00 -07003560ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003561MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3562
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003563Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3564MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3565
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003566void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003567 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003568 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003569 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003570 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003571 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003572 }
3573
Ian Rogers637c65b2013-05-31 11:46:00 -07003574 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003575 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003576 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003577 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3578 }
3579
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003580 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3581 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003582 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003583 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3584 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003585 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003586}
3587
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003588void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003589 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003590 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003591 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003592 STLDeleteValues(dex_gc_maps_);
3593 delete dex_gc_maps_;
3594 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003595 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003596 delete dex_gc_maps_lock_;
3597 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003598
3599 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003600 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003601 STLDeleteValues(devirt_maps_);
3602 delete devirt_maps_;
3603 devirt_maps_ = NULL;
3604 }
3605 delete devirt_maps_lock_;
3606 devirt_maps_lock_ = NULL;
3607
3608 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003609 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003610 delete rejected_classes_;
3611 rejected_classes_ = NULL;
3612 }
3613 delete rejected_classes_lock_;
3614 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003615 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003616}
jeffhaod1224c72012-02-29 13:43:08 -08003617
Ian Rogers1212a022013-03-04 10:48:41 -08003618void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003619 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003620 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003621 rejected_classes_->insert(ref);
3622 }
jeffhaod1224c72012-02-29 13:43:08 -08003623 CHECK(IsClassRejected(ref));
3624}
3625
Ian Rogers1212a022013-03-04 10:48:41 -08003626bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003627 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003628 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003629}
3630
Ian Rogersd81871c2011-10-03 13:57:23 -07003631} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003632} // namespace art