blob: 1bb163a9afad76a03e59473e8c9b55846f69ac51 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers1212a022013-03-04 10:48:41 -080025#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/abstract_method-inl.h"
34#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/field-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
49
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
62 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070077 std::string& error,
78 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080084 error = "Verifier rejected class ";
85 error += PrettyDescriptor(klass);
86 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
90 error = "Verifier rejected class ";
91 error += PrettyDescriptor(klass);
92 error += " that attempts to sub-class final class ";
93 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
98 uint32_t class_def_idx;
99 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
100 error = "Verifier rejected class ";
101 error += PrettyDescriptor(klass);
102 error += " that isn't present in dex file ";
103 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Jeff Haoee988952013-04-16 14:23:47 -0700106 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700107}
108
Ian Rogers365c1022012-06-22 15:05:28 -0700109MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::DexCache* dex_cache,
111 mirror::ClassLoader* class_loader,
112 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700113 std::string& error,
114 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800115 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
116 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700117 if (class_data == NULL) {
118 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700119 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700120 }
jeffhaof56197c2012-03-05 18:01:54 -0800121 ClassDataItemIterator it(*dex_file, class_data);
122 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
123 it.Next();
124 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700126 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700127 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700128 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800129 while (it.HasNextDirectMethod()) {
130 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700131 if (method_idx == previous_direct_method_idx) {
132 // smali can create dex files with two encoded_methods sharing the same method_idx
133 // http://code.google.com/p/smali/issues/detail?id=119
134 it.Next();
135 continue;
136 }
137 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::AbstractMethod* method =
140 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 if (method == NULL) {
142 DCHECK(Thread::Current()->IsExceptionPending());
143 // We couldn't resolve the method, but continue regardless.
144 Thread::Current()->ClearException();
145 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700147 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700148 if (result != kNoFailure) {
149 if (result == kHardFailure) {
150 hard_fail = true;
151 if (error_count > 0) {
152 error += "\n";
153 }
154 error = "Verifier rejected class ";
155 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
156 error += " due to bad method ";
157 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800160 }
161 it.Next();
162 }
jeffhao9b0b1882012-10-01 16:51:22 -0700163 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800164 while (it.HasNextVirtualMethod()) {
165 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700166 if (method_idx == previous_virtual_method_idx) {
167 // smali can create dex files with two encoded_methods sharing the same method_idx
168 // http://code.google.com/p/smali/issues/detail?id=119
169 it.Next();
170 continue;
171 }
172 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::AbstractMethod* method =
175 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 if (method == NULL) {
177 DCHECK(Thread::Current()->IsExceptionPending());
178 // We couldn't resolve the method, but continue regardless.
179 Thread::Current()->ClearException();
180 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700182 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700183 if (result != kNoFailure) {
184 if (result == kHardFailure) {
185 hard_fail = true;
186 if (error_count > 0) {
187 error += "\n";
188 }
189 error = "Verifier rejected class ";
190 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
191 error += " due to bad method ";
192 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800195 }
196 it.Next();
197 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 if (error_count == 0) {
199 return kNoFailure;
200 } else {
201 return hard_fail ? kHardFailure : kSoftFailure;
202 }
jeffhaof56197c2012-03-05 18:01:54 -0800203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
206 const DexFile* dex_file,
207 mirror::DexCache* dex_cache,
208 mirror::ClassLoader* class_loader,
209 uint32_t class_def_idx,
210 const DexFile::CodeItem* code_item,
211 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700212 uint32_t method_access_flags,
213 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700214 MethodVerifier::FailureKind result = kNoFailure;
215 uint64_t start_ns = NanoTime();
216
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700218 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Verification completed, however failures may be pending that didn't cause the verification
221 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700222 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 if (verifier.failures_.size() != 0) {
224 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700225 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700226 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // Bad method data.
230 CHECK_NE(verifier.failures_.size(), 0U);
231 CHECK(verifier.have_pending_hard_failure_);
232 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800234 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700235 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800236 verifier.Dump(std::cout);
237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800239 }
Ian Rogersc8982582012-09-07 16:53:25 -0700240 uint64_t duration_ns = NanoTime() - start_ns;
241 if (duration_ns > MsToNs(100)) {
242 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
243 << " took " << PrettyDuration(duration_ns);
244 }
245 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800246}
247
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile* dex_file, mirror::DexCache* dex_cache,
250 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
251 const DexFile::CodeItem* code_item,
252 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800253 uint32_t method_access_flags) {
254 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700255 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257 verifier.DumpFailures(os);
258 os << verifier.info_messages_.str();
259 verifier.Dump(os);
260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
263 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
264 const DexFile::CodeItem* code_item,
265 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700266 uint32_t method_access_flags, bool can_load_classes,
267 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800268 : reg_types_(can_load_classes),
269 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700271 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800273 dex_file_(dex_file),
274 dex_cache_(dex_cache),
275 class_loader_(class_loader),
276 class_def_idx_(class_def_idx),
277 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700278 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700279 interesting_dex_pc_(-1),
280 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700281 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700282 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800283 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800284 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700285 can_load_classes_(can_load_classes),
286 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800290 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700291 MethodHelper mh(m);
292 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
293 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700294 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700295 verifier.interesting_dex_pc_ = dex_pc;
296 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
297 verifier.FindLocksAtDexPc();
298}
299
300void MethodVerifier::FindLocksAtDexPc() {
301 CHECK(monitor_enter_dex_pcs_ != NULL);
302 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
303
304 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
305 // verification. In practice, the phase we want relies on data structures set up by all the
306 // earlier passes, so we just run the full method verification and bail out early when we've
307 // got what we wanted.
308 Verify();
309}
310
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700312 // If there aren't any instructions, make sure that's expected, then exit successfully.
313 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700316 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 } else {
318 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700319 }
jeffhaobdb76512011-09-07 11:43:16 -0700320 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700321 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
322 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
324 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700326 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800328 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 // Run through the instructions and see if the width checks out.
330 bool result = ComputeWidthsAndCountOps();
331 // Flag instructions guarded by a "try" block and check exception handlers.
332 result = result && ScanTryCatchBlocks();
333 // Perform static instruction verification.
334 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 // Perform code-flow analysis and return.
336 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700337}
338
Ian Rogers776ac1f2012-04-13 23:36:36 -0700339std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700340 switch (error) {
341 case VERIFY_ERROR_NO_CLASS:
342 case VERIFY_ERROR_NO_FIELD:
343 case VERIFY_ERROR_NO_METHOD:
344 case VERIFY_ERROR_ACCESS_CLASS:
345 case VERIFY_ERROR_ACCESS_FIELD:
346 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700347 case VERIFY_ERROR_INSTANTIATION:
348 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800349 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700350 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
351 // class change and instantiation errors into soft verification errors so that we re-verify
352 // at runtime. We may fail to find or to agree on access because of not yet available class
353 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
354 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
355 // paths" that dynamically perform the verification and cause the behavior to be that akin
356 // to an interpreter.
357 error = VERIFY_ERROR_BAD_CLASS_SOFT;
358 } else {
359 have_pending_runtime_throw_failure_ = true;
360 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700362 // Indication that verification should be retried at runtime.
363 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700364 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 have_pending_hard_failure_ = true;
366 }
367 break;
jeffhaod5347e02012-03-22 17:25:05 -0700368 // Hard verification failures at compile time will still fail at runtime, so the class is
369 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370 case VERIFY_ERROR_BAD_CLASS_HARD: {
371 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800372 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800373 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700375 have_pending_hard_failure_ = true;
376 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800377 }
378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800380 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700381 work_insn_idx_));
382 std::ostringstream* failure_message = new std::ostringstream(location);
383 failure_messages_.push_back(failure_message);
384 return *failure_message;
385}
386
387void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
388 size_t failure_num = failure_messages_.size();
389 DCHECK_NE(failure_num, 0U);
390 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
391 prepend += last_fail_message->str();
392 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
393 delete last_fail_message;
394}
395
396void MethodVerifier::AppendToLastFailMessage(std::string append) {
397 size_t failure_num = failure_messages_.size();
398 DCHECK_NE(failure_num, 0U);
399 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
400 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800401}
402
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 const uint16_t* insns = code_item_->insns_;
405 size_t insns_size = code_item_->insns_size_in_code_units_;
406 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700407 size_t new_instance_count = 0;
408 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700410
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700412 Instruction::Code opcode = inst->Opcode();
413 if (opcode == Instruction::NEW_INSTANCE) {
414 new_instance_count++;
415 } else if (opcode == Instruction::MONITOR_ENTER) {
416 monitor_enter_count++;
417 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 size_t inst_size = inst->SizeInCodeUnits();
419 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
420 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700421 inst = inst->Next();
422 }
423
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
426 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700427 return false;
428 }
429
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 new_instance_count_ = new_instance_count;
431 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700432 return true;
433}
434
Ian Rogers776ac1f2012-04-13 23:36:36 -0700435bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700437 if (tries_size == 0) {
438 return true;
439 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700441 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700442
443 for (uint32_t idx = 0; idx < tries_size; idx++) {
444 const DexFile::TryItem* try_item = &tries[idx];
445 uint32_t start = try_item->start_addr_;
446 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700447 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
449 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700450 return false;
451 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700454 return false;
455 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700456 for (uint32_t dex_pc = start; dex_pc < end;
457 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
458 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700459 }
460 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800461 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700462 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700463 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700464 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700465 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700466 CatchHandlerIterator iterator(handlers_ptr);
467 for (; iterator.HasNext(); iterator.Next()) {
468 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700469 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700471 return false;
472 }
jeffhao60f83e32012-02-13 17:16:30 -0800473 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
474 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800477 return false;
478 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700480 // Ensure exception types are resolved so that they don't need resolution to be delivered,
481 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700482 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
484 iterator.GetHandlerTypeIndex(),
485 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700486 if (exception_type == NULL) {
487 DCHECK(Thread::Current()->IsExceptionPending());
488 Thread::Current()->ClearException();
489 }
490 }
jeffhaobdb76512011-09-07 11:43:16 -0700491 }
Ian Rogers0571d352011-11-03 19:51:38 -0700492 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700493 }
jeffhaobdb76512011-09-07 11:43:16 -0700494 return true;
495}
496
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700499
Ian Rogers0c7abda2012-09-19 13:33:42 -0700500 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700502 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700503
504 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700505 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 return false;
509 }
510 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700511 // All invoke points are marked as "Throw" points already.
512 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700514 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 }
516 dex_pc += inst->SizeInCodeUnits();
517 inst = inst->Next();
518 }
519 return true;
520}
521
Ian Rogers776ac1f2012-04-13 23:36:36 -0700522bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800523 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 bool result = true;
525 switch (inst->GetVerifyTypeArgumentA()) {
526 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 break;
529 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800530 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 break;
532 }
533 switch (inst->GetVerifyTypeArgumentB()) {
534 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800535 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 break;
537 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800538 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 break;
540 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800541 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 break;
543 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800544 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 break;
546 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800547 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 break;
549 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 }
556 switch (inst->GetVerifyTypeArgumentC()) {
557 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800558 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 break;
560 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800561 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 break;
563 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800564 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 break;
566 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800567 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 break;
572 }
573 switch (inst->GetVerifyExtraFlags()) {
574 case Instruction::kVerifyArrayData:
575 result = result && CheckArrayData(code_offset);
576 break;
577 case Instruction::kVerifyBranchTarget:
578 result = result && CheckBranchTarget(code_offset);
579 break;
580 case Instruction::kVerifySwitchTargets:
581 result = result && CheckSwitchTargets(code_offset);
582 break;
583 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 result = false;
592 break;
593 }
594 return result;
595}
596
Ian Rogers776ac1f2012-04-13 23:36:36 -0700597bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
600 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 return false;
602 }
603 return true;
604}
605
Ian Rogers776ac1f2012-04-13 23:36:36 -0700606bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
609 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 return false;
611 }
612 return true;
613}
614
Ian Rogers776ac1f2012-04-13 23:36:36 -0700615bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
618 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 return false;
620 }
621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
627 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 return false;
629 }
630 return true;
631}
632
Ian Rogers776ac1f2012-04-13 23:36:36 -0700633bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
636 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 return false;
638 }
639 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700640 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 return false;
644 }
645 return true;
646}
647
Ian Rogers776ac1f2012-04-13 23:36:36 -0700648bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
651 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 return false;
653 }
654 return true;
655}
656
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
660 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700661 return false;
662 }
663 return true;
664}
665
Ian Rogers776ac1f2012-04-13 23:36:36 -0700666bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
669 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 return false;
671 }
672 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700673 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 const char* cp = descriptor;
675 while (*cp++ == '[') {
676 bracket_count++;
677 }
678 if (bracket_count == 0) {
679 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 return false;
682 } else if (bracket_count > 255) {
683 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 return false;
686 }
687 return true;
688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
692 const uint16_t* insns = code_item_->insns_ + cur_offset;
693 const uint16_t* array_data;
694 int32_t array_data_offset;
695
696 DCHECK_LT(cur_offset, insn_count);
697 /* make sure the start of the array data table is in range */
698 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
699 if ((int32_t) cur_offset + array_data_offset < 0 ||
700 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
702 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 /* offset to array data table is a relative branch-style offset */
706 array_data = insns + array_data_offset;
707 /* make sure the table is 32-bit aligned */
708 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
710 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700714 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
716 /* make sure the end of the switch is in range */
717 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
719 << ", data offset " << array_data_offset << ", end "
720 << cur_offset + array_data_offset + table_size
721 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 return false;
723 }
724 return true;
725}
726
Ian Rogers776ac1f2012-04-13 23:36:36 -0700727bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 int32_t offset;
729 bool isConditional, selfOkay;
730 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
731 return false;
732 }
733 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700737 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
738 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
744 int32_t abs_offset = cur_offset + offset;
745 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700747 << reinterpret_cast<void*>(abs_offset) << ") at "
748 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 insn_flags_[abs_offset].SetBranchTarget();
752 return true;
753}
754
Ian Rogers776ac1f2012-04-13 23:36:36 -0700755bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 bool* selfOkay) {
757 const uint16_t* insns = code_item_->insns_ + cur_offset;
758 *pConditional = false;
759 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700760 switch (*insns & 0xff) {
761 case Instruction::GOTO:
762 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700763 break;
764 case Instruction::GOTO_32:
765 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700766 *selfOkay = true;
767 break;
768 case Instruction::GOTO_16:
769 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700770 break;
771 case Instruction::IF_EQ:
772 case Instruction::IF_NE:
773 case Instruction::IF_LT:
774 case Instruction::IF_GE:
775 case Instruction::IF_GT:
776 case Instruction::IF_LE:
777 case Instruction::IF_EQZ:
778 case Instruction::IF_NEZ:
779 case Instruction::IF_LTZ:
780 case Instruction::IF_GEZ:
781 case Instruction::IF_GTZ:
782 case Instruction::IF_LEZ:
783 *pOffset = (int16_t) insns[1];
784 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700785 break;
786 default:
787 return false;
788 break;
789 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700795 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700797 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
799 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
801 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 return false;
803 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700806 /* make sure the table is 32-bit aligned */
807 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
809 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 return false;
811 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 uint32_t switch_count = switch_insns[1];
813 int32_t keys_offset, targets_offset;
814 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700815 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
816 /* 0=sig, 1=count, 2/3=firstKey */
817 targets_offset = 4;
818 keys_offset = -1;
819 expected_signature = Instruction::kPackedSwitchSignature;
820 } else {
821 /* 0=sig, 1=count, 2..count*2 = keys */
822 keys_offset = 2;
823 targets_offset = 2 + 2 * switch_count;
824 expected_signature = Instruction::kSparseSwitchSignature;
825 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
829 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 return false;
831 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 /* make sure the end of the switch is in range */
833 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
835 << switch_offset << ", end "
836 << (cur_offset + switch_offset + table_size)
837 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 return false;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 /* for a sparse switch, verify the keys are in ascending order */
841 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
843 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700844 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
845 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
846 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
848 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 return false;
850 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 last_key = key;
852 }
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 for (uint32_t targ = 0; targ < switch_count; targ++) {
856 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
857 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
858 int32_t abs_offset = cur_offset + offset;
859 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700861 << reinterpret_cast<void*>(abs_offset) << ") at "
862 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 insn_flags_[abs_offset].SetBranchTarget();
866 }
867 return true;
868}
869
Ian Rogers776ac1f2012-04-13 23:36:36 -0700870bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
874 }
875 uint16_t registers_size = code_item_->registers_size_;
876 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800877 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
879 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 }
883
884 return true;
885}
886
Ian Rogers776ac1f2012-04-13 23:36:36 -0700887bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 uint16_t registers_size = code_item_->registers_size_;
889 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
890 // integer overflow when adding them here.
891 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
893 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return true;
897}
898
Ian Rogers0c7abda2012-09-19 13:33:42 -0700899static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800900 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700901 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800902 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
903 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
904 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
905 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
906 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
907 gc_map.begin(),
908 gc_map.end());
909 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
910 DCHECK_EQ(gc_map.size(),
911 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
912 (length_prefixed_gc_map->at(1) << 16) |
913 (length_prefixed_gc_map->at(2) << 8) |
914 (length_prefixed_gc_map->at(3) << 0)));
915 return length_prefixed_gc_map;
916}
917
Ian Rogers776ac1f2012-04-13 23:36:36 -0700918bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 uint16_t registers_size = code_item_->registers_size_;
920 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700921
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800923 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
924 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 }
926 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700927 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
928
jeffhaobdb76512011-09-07 11:43:16 -0700929
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 work_line_.reset(new RegisterLine(registers_size, this));
931 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700932
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 /* Initialize register types of method arguments. */
934 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 DCHECK_NE(failures_.size(), 0U);
936 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800937 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 /* Perform code flow verification. */
942 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700943 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946
Ian Rogers1212a022013-03-04 10:48:41 -0800947 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700948
TDYa127b2eb5c12012-05-24 15:52:10 -0700949
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800951 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
952 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false; // Not a real failure, but a failure to encode
955 }
Ian Rogers39ebcb82013-05-30 16:57:23 -0700956 if (kIsDebugBuild) {
957 VerifyGcMap(*map);
958 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700959 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
960 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800961
Dragos Sbirlea980d16b2013-06-04 15:01:40 -0700962 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
963 if(method_to_safe_casts != NULL ) {
964 SetSafeCastMap(ref, method_to_safe_casts);
965 }
966
Ian Rogersd0583802013-06-01 10:51:46 -0700967 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700968 if(pc_to_concrete_method != NULL ) {
969 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700970 }
jeffhaobdb76512011-09-07 11:43:16 -0700971 return true;
972}
973
Ian Rogersad0b3a32012-04-16 14:50:24 -0700974std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
975 DCHECK_EQ(failures_.size(), failure_messages_.size());
976 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700977 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700978 }
979 return os;
980}
981
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700983 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700984 v->Dump(std::cerr);
985}
986
Ian Rogers776ac1f2012-04-13 23:36:36 -0700987void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800988 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700989 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 return;
jeffhaobdb76512011-09-07 11:43:16 -0700991 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800992 {
993 os << "Register Types:\n";
994 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
995 std::ostream indent_os(&indent_filter);
996 reg_types_.Dump(indent_os);
997 }
Ian Rogersb4903572012-10-11 11:52:56 -0700998 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800999 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1000 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 const Instruction* inst = Instruction::At(code_item_->insns_);
1002 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1003 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1005 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001006 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001007 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001008 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001009 const bool kDumpHexOfInstruction = false;
1010 if (kDumpHexOfInstruction) {
1011 indent_os << inst->DumpHex(5) << " ";
1012 }
1013 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001014 inst = inst->Next();
1015 }
jeffhaobdb76512011-09-07 11:43:16 -07001016}
1017
Ian Rogersd81871c2011-10-03 13:57:23 -07001018static bool IsPrimitiveDescriptor(char descriptor) {
1019 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001020 case 'I':
1021 case 'C':
1022 case 'S':
1023 case 'B':
1024 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001025 case 'F':
1026 case 'D':
1027 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001029 default:
1030 return false;
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 RegisterLine* reg_line = reg_table_.GetLine(0);
1036 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1037 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001038
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1040 //Include the "this" pointer.
1041 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1044 // argument as uninitialized. This restricts field access until the superclass constructor is
1045 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001046 const RegType& declaring_class = GetDeclaringClass();
1047 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 reg_line->SetRegisterType(arg_start + cur_arg,
1049 reg_types_.UninitializedThisArgument(declaring_class));
1050 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001051 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001054 }
1055
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001056 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001057 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001058 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001059
1060 for (; iterator.HasNext(); iterator.Next()) {
1061 const char* descriptor = iterator.GetDescriptor();
1062 if (descriptor == NULL) {
1063 LOG(FATAL) << "Null descriptor";
1064 }
1065 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1067 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 return false;
1069 }
1070 switch (descriptor[0]) {
1071 case 'L':
1072 case '[':
1073 // We assume that reference arguments are initialized. The only way it could be otherwise
1074 // (assuming the caller was verified) is if the current method is <init>, but in that case
1075 // it's effectively considered initialized the instant we reach here (in the sense that we
1076 // can return without doing anything or call virtual methods).
1077 {
Ian Rogersb4903572012-10-11 11:52:56 -07001078 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001079 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 }
1081 break;
1082 case 'Z':
1083 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1084 break;
1085 case 'C':
1086 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1087 break;
1088 case 'B':
1089 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1090 break;
1091 case 'I':
1092 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1093 break;
1094 case 'S':
1095 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1096 break;
1097 case 'F':
1098 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1099 break;
1100 case 'J':
1101 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1103 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1104 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 cur_arg++;
1106 break;
1107 }
1108 default:
jeffhaod5347e02012-03-22 17:25:05 -07001109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 return false;
1111 }
1112 cur_arg++;
1113 }
1114 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 return false;
1117 }
1118 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1119 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1120 // format. Only major difference from the method argument format is that 'V' is supported.
1121 bool result;
1122 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1123 result = descriptor[1] == '\0';
1124 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1125 size_t i = 0;
1126 do {
1127 i++;
1128 } while (descriptor[i] == '['); // process leading [
1129 if (descriptor[i] == 'L') { // object array
1130 do {
1131 i++; // find closing ;
1132 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1133 result = descriptor[i] == ';';
1134 } else { // primitive array
1135 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1136 }
1137 } else if (descriptor[0] == 'L') {
1138 // could be more thorough here, but shouldn't be required
1139 size_t i = 0;
1140 do {
1141 i++;
1142 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1143 result = descriptor[i] == ';';
1144 } else {
1145 result = false;
1146 }
1147 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1149 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 }
1151 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 const uint16_t* insns = code_item_->insns_;
1156 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001157
jeffhaobdb76512011-09-07 11:43:16 -07001158 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 insn_flags_[0].SetChanged();
1160 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001161
jeffhaobdb76512011-09-07 11:43:16 -07001162 /* Continue until no instructions are marked "changed". */
1163 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1165 uint32_t insn_idx = start_guess;
1166 for (; insn_idx < insns_size; insn_idx++) {
1167 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001168 break;
1169 }
jeffhaobdb76512011-09-07 11:43:16 -07001170 if (insn_idx == insns_size) {
1171 if (start_guess != 0) {
1172 /* try again, starting from the top */
1173 start_guess = 0;
1174 continue;
1175 } else {
1176 /* all flags are clear */
1177 break;
1178 }
1179 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 // We carry the working set of registers from instruction to instruction. If this address can
1181 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1182 // "changed" flags, we need to load the set of registers from the table.
1183 // Because we always prefer to continue on to the next instruction, we should never have a
1184 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1185 // target.
1186 work_insn_idx_ = insn_idx;
1187 if (insn_flags_[insn_idx].IsBranchTarget()) {
1188 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001189 } else {
1190#ifndef NDEBUG
1191 /*
1192 * Sanity check: retrieve the stored register line (assuming
1193 * a full table) and make sure it actually matches.
1194 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1196 if (register_line != NULL) {
1197 if (work_line_->CompareLine(register_line) != 0) {
1198 Dump(std::cout);
1199 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001201 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1202 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001203 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 }
jeffhaobdb76512011-09-07 11:43:16 -07001205 }
1206#endif
1207 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001210 prepend += " failed to verify: ";
1211 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 return false;
1213 }
jeffhaobdb76512011-09-07 11:43:16 -07001214 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 insn_flags_[insn_idx].SetVisited();
1216 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001217 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001218
Ian Rogers1c849e52012-06-28 14:00:33 -07001219 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001220 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001221 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001222 * (besides the wasted space), but it indicates a flaw somewhere
1223 * down the line, possibly in the verifier.
1224 *
1225 * If we've substituted "always throw" instructions into the stream,
1226 * we are almost certainly going to have some dead code.
1227 */
1228 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 uint32_t insn_idx = 0;
1230 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001231 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001232 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001233 * may or may not be preceded by a padding NOP (for alignment).
1234 */
1235 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1236 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1237 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001238 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001239 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1240 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1241 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001243 }
1244
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001246 if (dead_start < 0)
1247 dead_start = insn_idx;
1248 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001249 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001250 dead_start = -1;
1251 }
1252 }
1253 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001254 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001255 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001256 // To dump the state of the verify after a method, do something like:
1257 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1258 // "boolean java.lang.String.equals(java.lang.Object)") {
1259 // LOG(INFO) << info_messages_.str();
1260 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001261 }
jeffhaobdb76512011-09-07 11:43:16 -07001262 return true;
1263}
1264
Ian Rogers776ac1f2012-04-13 23:36:36 -07001265bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001266 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1267 // We want the state _before_ the instruction, for the case where the dex pc we're
1268 // interested in is itself a monitor-enter instruction (which is a likely place
1269 // for a thread to be suspended).
1270 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001271 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001272 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1273 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1274 }
1275 }
1276
jeffhaobdb76512011-09-07 11:43:16 -07001277 /*
1278 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001279 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001280 * control to another statement:
1281 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001282 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001283 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001284 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001285 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001286 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001287 * throw an exception that is handled by an encompassing "try"
1288 * block.
1289 *
1290 * We can also return, in which case there is no successor instruction
1291 * from this point.
1292 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001293 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001294 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1296 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001297 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001298
jeffhaobdb76512011-09-07 11:43:16 -07001299 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001300 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001301 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001303 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1304 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 }
jeffhaobdb76512011-09-07 11:43:16 -07001306
1307 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001308 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001309 * can throw an exception, we will copy/merge this into the "catch"
1310 * address rather than work_line, because we don't want the result
1311 * from the "successful" code path (e.g. a check-cast that "improves"
1312 * a type) to be visible to the exception handler.
1313 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001314 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001316 } else {
1317#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001319#endif
1320 }
1321
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001322
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001323 // We need to ensure the work line is consistent while performing validation. When we spot a
1324 // peephole pattern we compute a new line for either the fallthrough instruction or the
1325 // branch target.
1326 UniquePtr<RegisterLine> branch_line;
1327 UniquePtr<RegisterLine> fallthrough_line;
1328
Sebastien Hertz5243e912013-05-21 10:55:07 +02001329 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001330 case Instruction::NOP:
1331 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001332 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001333 * a signature that looks like a NOP; if we see one of these in
1334 * the course of executing code then we have a problem.
1335 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001336 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001338 }
1339 break;
1340
1341 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001342 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1343 break;
jeffhaobdb76512011-09-07 11:43:16 -07001344 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001345 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1346 break;
jeffhaobdb76512011-09-07 11:43:16 -07001347 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001348 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001349 break;
1350 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001351 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1352 break;
jeffhaobdb76512011-09-07 11:43:16 -07001353 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001354 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1355 break;
jeffhaobdb76512011-09-07 11:43:16 -07001356 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001357 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001358 break;
1359 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001360 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1361 break;
jeffhaobdb76512011-09-07 11:43:16 -07001362 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001363 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1364 break;
jeffhaobdb76512011-09-07 11:43:16 -07001365 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001366 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001367 break;
1368
1369 /*
1370 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001371 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001372 * might want to hold the result in an actual CPU register, so the
1373 * Dalvik spec requires that these only appear immediately after an
1374 * invoke or filled-new-array.
1375 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001376 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001377 * redundant with the reset done below, but it can make the debug info
1378 * easier to read in some cases.)
1379 */
1380 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001381 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001382 break;
1383 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001384 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001385 break;
1386 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001387 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001388 break;
1389
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001391 /*
jeffhao60f83e32012-02-13 17:16:30 -08001392 * This statement can only appear as the first instruction in an exception handler. We verify
1393 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001394 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001395 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001396 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001397 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 }
jeffhaobdb76512011-09-07 11:43:16 -07001399 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001400 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1401 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 }
jeffhaobdb76512011-09-07 11:43:16 -07001404 }
1405 break;
1406 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001407 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001408 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 const RegType& return_type = GetMethodReturnType();
1410 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 } else {
1413 // Compilers may generate synthetic functions that write byte values into boolean fields.
1414 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001415 const uint32_t vregA = inst->VRegA_11x();
1416 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1418 ((return_type.IsBoolean() || return_type.IsByte() ||
1419 return_type.IsShort() || return_type.IsChar()) &&
1420 src_type.IsInteger()));
1421 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001422 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001423 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001424 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001425 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 }
jeffhaobdb76512011-09-07 11:43:16 -07001427 }
1428 }
1429 break;
1430 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001431 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001432 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 const RegType& return_type = GetMethodReturnType();
1434 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 } else {
1437 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001438 const uint32_t vregA = inst->VRegA_11x();
1439 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001440 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001441 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 }
jeffhaobdb76512011-09-07 11:43:16 -07001443 }
1444 }
1445 break;
1446 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001447 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 const RegType& return_type = GetMethodReturnType();
1449 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 } else {
1452 /* return_type is the *expected* return type, not register value */
1453 DCHECK(!return_type.IsZero());
1454 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001455 const uint32_t vregA = inst->VRegA_11x();
1456 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001457 // Disallow returning uninitialized values and verify that the reference in vAA is an
1458 // instance of the "return_type"
1459 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001460 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001461 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001462 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1463 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001464 }
1465 }
1466 }
1467 break;
1468
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001469 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 case Instruction::CONST_4: {
1471 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1472 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 }
1475 case Instruction::CONST_16: {
1476 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1477 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001478 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 }
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->SetRegisterType(inst->VRegA_31i(),
1482 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001483 break;
1484 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 work_line_->SetRegisterType(inst->VRegA_21h(),
1486 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001487 break;
jeffhaobdb76512011-09-07 11:43:16 -07001488 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001489 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1492 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 }
1496 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1499 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001501 break;
1502 }
1503 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001504 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1506 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001507 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001508 break;
1509 }
1510 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001512 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1513 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 break;
1516 }
jeffhaobdb76512011-09-07 11:43:16 -07001517 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001518 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1519 break;
jeffhaobdb76512011-09-07 11:43:16 -07001520 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001521 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001522 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001524 // Get type from instruction if unresolved then we need an access check
1525 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001526 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001527 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001529 res_type.IsConflict() ? res_type
1530 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001531 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 }
jeffhaobdb76512011-09-07 11:43:16 -07001533 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 case Instruction::MONITOR_EXIT:
1537 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001538 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001539 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001540 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001541 * to the need to handle asynchronous exceptions, a now-deprecated
1542 * feature that Dalvik doesn't support.)
1543 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001544 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001545 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001546 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001547 * structured locking checks are working, the former would have
1548 * failed on the -enter instruction, and the latter is impossible.
1549 *
1550 * This is fortunate, because issue 3221411 prevents us from
1551 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001552 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001553 * some catch blocks (which will show up as "dead" code when
1554 * we skip them here); if we can't, then the code path could be
1555 * "live" so we still need to check it.
1556 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001557 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001559 break;
1560
Ian Rogers28ad40d2011-10-27 15:19:26 -07001561 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001563 /*
1564 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1565 * could be a "upcast" -- not expected, so we don't try to address it.)
1566 *
1567 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001569 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1571 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1572 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001573 if (res_type.IsConflict()) {
1574 DCHECK_NE(failures_.size(), 0U);
1575 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001576 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 }
1578 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001579 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001581 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1582 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001583 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 if (is_checkcast) {
1585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1586 } else {
1587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1588 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001589 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 if (is_checkcast) {
1591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1592 } else {
1593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1594 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001595 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001596 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001597 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001600 }
jeffhaobdb76512011-09-07 11:43:16 -07001601 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001602 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 }
1604 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001606 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001607 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 }
1612 }
1613 break;
1614 }
1615 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001617 if (res_type.IsConflict()) {
1618 DCHECK_NE(failures_.size(), 0U);
1619 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001620 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001621 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1622 // can't create an instance of an interface or abstract class */
1623 if (!res_type.IsInstantiableTypes()) {
1624 Fail(VERIFY_ERROR_INSTANTIATION)
1625 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001626 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001628 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1629 // Any registers holding previous allocations from this address that have not yet been
1630 // initialized must be marked invalid.
1631 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1632 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 break;
1635 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001636 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001638 break;
1639 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001641 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001642 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001643 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001645 just_set_result = true; // Filled new array range sets result register
1646 break;
jeffhaobdb76512011-09-07 11:43:16 -07001647 case Instruction::CMPL_FLOAT:
1648 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001650 break;
1651 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001652 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001653 break;
1654 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 case Instruction::CMPL_DOUBLE:
1658 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001660 reg_types_.DoubleHi())) {
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_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001664 reg_types_.DoubleHi())) {
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;
1669 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001671 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001672 break;
1673 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001676 break;
1677 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001679 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001682 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001683 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001684 }
1685 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 }
jeffhaobdb76512011-09-07 11:43:16 -07001687 case Instruction::GOTO:
1688 case Instruction::GOTO_16:
1689 case Instruction::GOTO_32:
1690 /* no effect on or use of registers */
1691 break;
1692
1693 case Instruction::PACKED_SWITCH:
1694 case Instruction::SPARSE_SWITCH:
1695 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001697 break;
1698
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 case Instruction::FILL_ARRAY_DATA: {
1700 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001702 /* array_type can be null if the reg type is Zero */
1703 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001704 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001706 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001707 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1708 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001709 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1711 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 } else {
jeffhao457cc512012-02-02 16:55:13 -08001713 // Now verify if the element width in the table matches the element width declared in
1714 // the array
1715 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1716 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001718 } else {
1719 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1720 // Since we don't compress the data in Dex, expect to see equal width of data stored
1721 // in the table and expected from the array class.
1722 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1724 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001725 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 }
1727 }
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
1729 }
1730 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1735 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 bool mismatch = false;
1737 if (reg_type1.IsZero()) { // zero then integral or reference expected
1738 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1739 } else if (reg_type1.IsReferenceTypes()) { // both references?
1740 mismatch = !reg_type2.IsReferenceTypes();
1741 } else { // both integral?
1742 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1743 }
1744 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1746 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
1748 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 }
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::IF_LT:
1751 case Instruction::IF_GE:
1752 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1755 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1758 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001759 }
1760 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 }
jeffhaobdb76512011-09-07 11:43:16 -07001762 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001768
1769 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001770 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001771 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001772 instance_of_idx = work_insn_idx_ - 1;
1773 while(0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
1774 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001775 }
Ian Rogers9b360392013-06-06 14:45:07 -07001776 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001777 } else {
1778 break;
1779 }
1780
Ian Rogers9b360392013-06-06 14:45:07 -07001781 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001782
1783 /* Check for peep-hole pattern of:
1784 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001785 * instance-of vX, vY, T;
1786 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001787 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001788 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001789 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001790 * and sharpen the type of vY to be type T.
1791 * Note, this pattern can't be if:
1792 * - if there are other branches to this branch,
1793 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001794 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001795 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001796 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1797 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1798 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001799 // Check that the we are not attempting conversion to interface types,
1800 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001801 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001802
Ian Rogersfae370a2013-06-05 08:33:27 -07001803 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001804 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001805 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001806 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001807 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001808 branch_line.reset(update_line);
1809 }
1810 update_line->CopyFromLine(work_line_.get());
1811 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1812 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1813 // See if instance-of was preceded by a move-object operation, common due to the small
1814 // register encoding space of instance-of, and propagate type information to the source
1815 // of the move-object.
1816 uint32_t move_idx = instance_of_idx - 1;
1817 while(0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
1818 move_idx--;
1819 }
1820 CHECK(insn_flags_[move_idx].IsOpcode());
1821 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1822 switch (move_inst->Opcode()) {
1823 case Instruction::MOVE_OBJECT:
1824 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1825 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1826 }
1827 break;
1828 case Instruction::MOVE_OBJECT_FROM16:
1829 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1830 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1831 }
1832 break;
1833 case Instruction::MOVE_OBJECT_16:
1834 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1835 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1836 }
1837 break;
1838 default:
1839 break;
1840 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001841 }
1842 }
1843 }
1844
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 case Instruction::IF_LTZ:
1848 case Instruction::IF_GEZ:
1849 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001851 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1854 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 break;
jeffhaobdb76512011-09-07 11:43:16 -07001861 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 break;
jeffhaobdb76512011-09-07 11:43:16 -07001864 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 break;
jeffhaobdb76512011-09-07 11:43:16 -07001867 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001869 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001871 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 break;
jeffhaobdb76512011-09-07 11:43:16 -07001873 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001874 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 break;
1876 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001877 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001878 break;
1879
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001881 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 break;
1883 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001884 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 break;
1886 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 break;
1889 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
1895 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
1898 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
1901
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 break;
jeffhaobdb76512011-09-07 11:43:16 -07001905 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001906 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 break;
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 break;
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 break;
1914 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
1917 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
1920 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 break;
jeffhaobdb76512011-09-07 11:43:16 -07001923
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 break;
1927 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 break;
1930 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 break;
1933 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001935 break;
1936 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
1939 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 break;
jeffhaobdb76512011-09-07 11:43:16 -07001942 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945
jeffhaobdb76512011-09-07 11:43:16 -07001946 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001947 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 break;
jeffhaobdb76512011-09-07 11:43:16 -07001949 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 break;
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 break;
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 break;
1958 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001959 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001960 break;
1961 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001962 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001963 break;
1964 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 break;
1967
1968 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 break;
1971 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 break;
1974 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 break;
1977 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
1980 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
1983 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
1986 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989
1990 case Instruction::INVOKE_VIRTUAL:
1991 case Instruction::INVOKE_VIRTUAL_RANGE:
1992 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
1995 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1996 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
1997 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
1998 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001999 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002000 const char* descriptor;
2001 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002002 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002003 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2004 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2005 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2006 } else {
2007 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002008 }
Ian Rogersb4903572012-10-11 11:52:56 -07002009 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002010 if (!return_type.IsLowHalf()) {
2011 work_line_->SetResultRegisterType(return_type);
2012 } else {
2013 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2014 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002015 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002016 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 }
jeffhaobdb76512011-09-07 11:43:16 -07002018 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2021 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002022 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002023 const char* return_type_descriptor;
2024 bool is_constructor;
2025 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002027 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2028 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2029 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2030 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2031 } else {
2032 is_constructor = called_method->IsConstructor();
2033 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2034 }
2035 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002036 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 * Some additional checks when calling a constructor. We know from the invocation arg check
2038 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2039 * that to require that called_method->klass is the same as this->klass or this->super,
2040 * allowing the latter only if the "this" argument is the same as the "this" argument to
2041 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002042 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002044 if (this_type.IsConflict()) // failure.
2045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046
jeffhaob57e9522012-04-26 18:08:21 -07002047 /* no null refs allowed (?) */
2048 if (this_type.IsZero()) {
2049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2050 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002051 }
jeffhaob57e9522012-04-26 18:08:21 -07002052
2053 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002054 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2055 // TODO: re-enable constructor type verification
2056 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002057 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002058 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2059 // break;
2060 // }
jeffhaob57e9522012-04-26 18:08:21 -07002061
2062 /* arg must be an uninitialized reference */
2063 if (!this_type.IsUninitializedTypes()) {
2064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2065 << this_type;
2066 break;
2067 }
2068
2069 /*
2070 * Replace the uninitialized reference with an initialized one. We need to do this for all
2071 * registers that have the same object instance in them, not just the "this" register.
2072 */
2073 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002074 }
Ian Rogersb4903572012-10-11 11:52:56 -07002075 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2076 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 if (!return_type.IsLowHalf()) {
2078 work_line_->SetResultRegisterType(return_type);
2079 } else {
2080 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2081 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002082 just_set_result = true;
2083 break;
2084 }
2085 case Instruction::INVOKE_STATIC:
2086 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2088 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002089 const char* descriptor;
2090 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002092 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2093 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002094 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002095 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002096 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002097 }
Ian Rogersb4903572012-10-11 11:52:56 -07002098 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002099 if (!return_type.IsLowHalf()) {
2100 work_line_->SetResultRegisterType(return_type);
2101 } else {
2102 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2103 }
jeffhaobdb76512011-09-07 11:43:16 -07002104 just_set_result = true;
2105 }
2106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2110 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002111 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002112 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002113 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2114 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2115 << PrettyMethod(abs_method) << "'";
2116 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002117 }
Ian Rogers0d604842012-04-16 14:50:24 -07002118 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002119 /* Get the type of the "this" arg, which should either be a sub-interface of called
2120 * interface or Object (see comments in RegType::JoinClass).
2121 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002123 if (this_type.IsZero()) {
2124 /* null pointer always passes (and always fails at runtime) */
2125 } else {
2126 if (this_type.IsUninitializedTypes()) {
2127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2128 << this_type;
2129 break;
2130 }
2131 // In the past we have tried to assert that "called_interface" is assignable
2132 // from "this_type.GetClass()", however, as we do an imprecise Join
2133 // (RegType::JoinClass) we don't have full information on what interfaces are
2134 // implemented by "this_type". For example, two classes may implement the same
2135 // interfaces and have a common parent that doesn't implement the interface. The
2136 // join will set "this_type" to the parent class and a test that this implements
2137 // the interface will incorrectly fail.
2138 }
2139 /*
2140 * We don't have an object instance, so we can't find the concrete method. However, all of
2141 * the type information is in the abstract method, so we're good.
2142 */
2143 const char* descriptor;
2144 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002146 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2147 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2148 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2149 } else {
2150 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2151 }
Ian Rogersb4903572012-10-11 11:52:56 -07002152 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 if (!return_type.IsLowHalf()) {
2154 work_line_->SetResultRegisterType(return_type);
2155 } else {
2156 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002158 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 }
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::NEG_INT:
2162 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::NEG_LONG:
2166 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
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::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002186 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
2188 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002191 break;
2192 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002194 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002195 break;
2196 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002198 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002199 break;
2200 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002205 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002206 break;
2207 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002208 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
2211 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002213 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002217 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002221 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002228 break;
2229 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
2232
2233 case Instruction::ADD_INT:
2234 case Instruction::SUB_INT:
2235 case Instruction::MUL_INT:
2236 case Instruction::REM_INT:
2237 case Instruction::DIV_INT:
2238 case Instruction::SHL_INT:
2239 case Instruction::SHR_INT:
2240 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002241 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244 case Instruction::AND_INT:
2245 case Instruction::OR_INT:
2246 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::ADD_LONG:
2251 case Instruction::SUB_LONG:
2252 case Instruction::MUL_LONG:
2253 case Instruction::DIV_LONG:
2254 case Instruction::REM_LONG:
2255 case Instruction::AND_LONG:
2256 case Instruction::OR_LONG:
2257 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 reg_types_.LongLo(), reg_types_.LongHi(),
2260 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
2262 case Instruction::SHL_LONG:
2263 case Instruction::SHR_LONG:
2264 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002267 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::ADD_FLOAT:
2270 case Instruction::SUB_FLOAT:
2271 case Instruction::MUL_FLOAT:
2272 case Instruction::DIV_FLOAT:
2273 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::ADD_DOUBLE:
2277 case Instruction::SUB_DOUBLE:
2278 case Instruction::MUL_DOUBLE:
2279 case Instruction::DIV_DOUBLE:
2280 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2283 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::ADD_INT_2ADDR:
2286 case Instruction::SUB_INT_2ADDR:
2287 case Instruction::MUL_INT_2ADDR:
2288 case Instruction::REM_INT_2ADDR:
2289 case Instruction::SHL_INT_2ADDR:
2290 case Instruction::SHR_INT_2ADDR:
2291 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002292 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::AND_INT_2ADDR:
2295 case Instruction::OR_INT_2ADDR:
2296 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::ADD_LONG_2ADDR:
2303 case Instruction::SUB_LONG_2ADDR:
2304 case Instruction::MUL_LONG_2ADDR:
2305 case Instruction::DIV_LONG_2ADDR:
2306 case Instruction::REM_LONG_2ADDR:
2307 case Instruction::AND_LONG_2ADDR:
2308 case Instruction::OR_LONG_2ADDR:
2309 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 reg_types_.LongLo(), reg_types_.LongHi(),
2312 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::SHL_LONG_2ADDR:
2315 case Instruction::SHR_LONG_2ADDR:
2316 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::ADD_FLOAT_2ADDR:
2321 case Instruction::SUB_FLOAT_2ADDR:
2322 case Instruction::MUL_FLOAT_2ADDR:
2323 case Instruction::DIV_FLOAT_2ADDR:
2324 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::ADD_DOUBLE_2ADDR:
2328 case Instruction::SUB_DOUBLE_2ADDR:
2329 case Instruction::MUL_DOUBLE_2ADDR:
2330 case Instruction::DIV_DOUBLE_2ADDR:
2331 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2334 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::ADD_INT_LIT16:
2337 case Instruction::RSUB_INT:
2338 case Instruction::MUL_INT_LIT16:
2339 case Instruction::DIV_INT_LIT16:
2340 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::AND_INT_LIT16:
2344 case Instruction::OR_INT_LIT16:
2345 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::ADD_INT_LIT8:
2349 case Instruction::RSUB_INT_LIT8:
2350 case Instruction::MUL_INT_LIT8:
2351 case Instruction::DIV_INT_LIT8:
2352 case Instruction::REM_INT_LIT8:
2353 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002354 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002355 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::AND_INT_LIT8:
2359 case Instruction::OR_INT_LIT8:
2360 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002365 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::UNUSED_EE:
2367 case Instruction::UNUSED_EF:
2368 case Instruction::UNUSED_F2:
2369 case Instruction::UNUSED_F3:
2370 case Instruction::UNUSED_F4:
2371 case Instruction::UNUSED_F5:
2372 case Instruction::UNUSED_F6:
2373 case Instruction::UNUSED_F7:
2374 case Instruction::UNUSED_F8:
2375 case Instruction::UNUSED_F9:
2376 case Instruction::UNUSED_FA:
2377 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002378 case Instruction::UNUSED_F0:
2379 case Instruction::UNUSED_F1:
2380 case Instruction::UNUSED_E3:
2381 case Instruction::UNUSED_E8:
2382 case Instruction::UNUSED_E7:
2383 case Instruction::UNUSED_E4:
2384 case Instruction::UNUSED_E9:
2385 case Instruction::UNUSED_FC:
2386 case Instruction::UNUSED_E5:
2387 case Instruction::UNUSED_EA:
2388 case Instruction::UNUSED_FD:
2389 case Instruction::UNUSED_E6:
2390 case Instruction::UNUSED_EB:
2391 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002392 case Instruction::UNUSED_3E:
2393 case Instruction::UNUSED_3F:
2394 case Instruction::UNUSED_40:
2395 case Instruction::UNUSED_41:
2396 case Instruction::UNUSED_42:
2397 case Instruction::UNUSED_43:
2398 case Instruction::UNUSED_73:
2399 case Instruction::UNUSED_79:
2400 case Instruction::UNUSED_7A:
2401 case Instruction::UNUSED_EC:
2402 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405
2406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002407 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002408 * complain if an instruction is missing (which is desirable).
2409 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002410 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002411
Ian Rogersad0b3a32012-04-16 14:50:24 -07002412 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002413 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002414 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002415 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002416 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002417 /* immediate failure, reject class */
2418 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2419 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002420 } else if (have_pending_runtime_throw_failure_) {
2421 /* slow path will throw, mark following code as unreachable */
2422 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002423 }
jeffhaobdb76512011-09-07 11:43:16 -07002424 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 * If we didn't just set the result register, clear it out. This ensures that you can only use
2426 * "move-result" immediately after the result is set. (We could check this statically, but it's
2427 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002428 */
2429 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002431 }
2432
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002433
jeffhaobdb76512011-09-07 11:43:16 -07002434
2435 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002436 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002437 *
2438 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002439 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002440 * somebody could get a reference field, check it for zero, and if the
2441 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002442 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002443 * that, and will reject the code.
2444 *
2445 * TODO: avoid re-fetching the branch target
2446 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002447 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002448 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002450 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002452 return false;
2453 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002454 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002455 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002456 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 }
jeffhaobdb76512011-09-07 11:43:16 -07002458 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002459 if (NULL != branch_line.get()) {
2460 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2461 return false;
2462 }
2463 } else {
2464 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2465 return false;
2466 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 }
jeffhaobdb76512011-09-07 11:43:16 -07002468 }
2469
2470 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002471 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002472 *
2473 * We've already verified that the table is structurally sound, so we
2474 * just need to walk through and tag the targets.
2475 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002476 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002477 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2478 const uint16_t* switch_insns = insns + offset_to_switch;
2479 int switch_count = switch_insns[1];
2480 int offset_to_targets, targ;
2481
2482 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2483 /* 0 = sig, 1 = count, 2/3 = first key */
2484 offset_to_targets = 4;
2485 } else {
2486 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002487 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002488 offset_to_targets = 2 + 2 * switch_count;
2489 }
2490
2491 /* verify each switch target */
2492 for (targ = 0; targ < switch_count; targ++) {
2493 int offset;
2494 uint32_t abs_offset;
2495
2496 /* offsets are 32-bit, and only partly endian-swapped */
2497 offset = switch_insns[offset_to_targets + targ * 2] |
2498 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 abs_offset = work_insn_idx_ + offset;
2500 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002501 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002502 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 }
2504 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002505 return false;
2506 }
2507 }
2508
2509 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2511 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002512 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002513 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002515 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002516
Ian Rogers0571d352011-11-03 19:51:38 -07002517 for (; iterator.HasNext(); iterator.Next()) {
2518 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 within_catch_all = true;
2520 }
jeffhaobdb76512011-09-07 11:43:16 -07002521 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2523 * "work_regs", because at runtime the exception will be thrown before the instruction
2524 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002525 */
Ian Rogers0571d352011-11-03 19:51:38 -07002526 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002527 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 }
jeffhaobdb76512011-09-07 11:43:16 -07002529 }
2530
2531 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2533 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002534 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002536 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 * The state in work_line reflects the post-execution state. If the current instruction is a
2538 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002539 * it will do so before grabbing the lock).
2540 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002541 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002542 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002544 return false;
2545 }
2546 }
2547 }
2548
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002549 /* Handle "continue". Tag the next consecutive instruction.
2550 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2551 * because it changes work_line_ when performing peephole optimization
2552 * and this change should not be used in those cases.
2553 */
2554 if ((opcode_flags & Instruction::kContinue) != 0) {
2555 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2556 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2558 return false;
2559 }
2560 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2561 // next instruction isn't one.
2562 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2563 return false;
2564 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07002565 if (NULL != fallthrough_line.get()) {
2566 // Make workline consistent with fallthrough computed from peephole optimization.
2567 work_line_->CopyFromLine(fallthrough_line.get());
2568 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002569 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2570 if (next_line != NULL) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002571 // Merge registers into what we have for the next instruction,
2572 // and set the "changed" flag if needed.
2573 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2574 return false;
2575 }
2576 } else {
2577 /*
2578 * We're not recording register data for the next instruction, so we don't know what the
2579 * prior state was. We have to assume that something has changed and re-evaluate it.
2580 */
2581 insn_flags_[next_insn_idx].SetChanged();
2582 }
2583 }
2584
jeffhaod1f0fde2011-09-08 17:25:33 -07002585 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002586 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002587 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 return false;
2589 }
jeffhaobdb76512011-09-07 11:43:16 -07002590 }
2591
2592 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002593 * Update start_guess. Advance to the next instruction of that's
2594 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002595 * neither of those exists we're in a return or throw; leave start_guess
2596 * alone and let the caller sort it out.
2597 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002598 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002600 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002601 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002603 }
2604
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2606 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002607
2608 return true;
2609}
2610
Ian Rogers776ac1f2012-04-13 23:36:36 -07002611const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002612 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002613 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002614 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002615 const RegType& result =
Ian Rogers637c65b2013-05-31 11:46:00 -07002616 klass != NULL ? reg_types_.FromClass(descriptor, klass, klass->IsFinal())
Ian Rogersb4903572012-10-11 11:52:56 -07002617 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002618 if (result.IsConflict()) {
2619 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2620 << "' in " << referrer;
2621 return result;
2622 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002623 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002627 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002628 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002629 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002630 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002631 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002632 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002633}
2634
Ian Rogers776ac1f2012-04-13 23:36:36 -07002635const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002636 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002638 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2640 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002641 CatchHandlerIterator iterator(handlers_ptr);
2642 for (; iterator.HasNext(); iterator.Next()) {
2643 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2644 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002645 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002647 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002648 if (common_super == NULL) {
2649 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2650 // as that is caught at runtime
2651 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002652 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002653 // We don't know enough about the type and the common path merge will result in
2654 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002655 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002656 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002657 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002658 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002660 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002661 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 }
2663 }
2664 }
2665 }
Ian Rogers0571d352011-11-03 19:51:38 -07002666 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 }
2668 }
2669 if (common_super == NULL) {
2670 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002671 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002672 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002674 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002675}
2676
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002677mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2678 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002679 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002680 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002681 if (klass_type.IsConflict()) {
2682 std::string append(" in attempt to access method ");
2683 append += dex_file_->GetMethodName(method_id);
2684 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002685 return NULL;
2686 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002687 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002688 return NULL; // Can't resolve Class so no more to do here
2689 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002690 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002691 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002692 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002694 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002695 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002696
2697 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002699 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 res_method = klass->FindInterfaceMethod(name, signature);
2701 } else {
2702 res_method = klass->FindVirtualMethod(name, signature);
2703 }
2704 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002707 // If a virtual or interface method wasn't found with the expected type, look in
2708 // the direct methods. This can happen when the wrong invoke type is used or when
2709 // a class has changed, and will be flagged as an error in later checks.
2710 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2711 res_method = klass->FindDirectMethod(name, signature);
2712 }
2713 if (res_method == NULL) {
2714 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2715 << PrettyDescriptor(klass) << "." << name
2716 << " " << signature;
2717 return NULL;
2718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 }
2720 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2722 // enforce them here.
2723 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2725 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 return NULL;
2727 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002728 // Disallow any calls to class initializers.
2729 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2731 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002732 return NULL;
2733 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002734 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002735 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002736 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002737 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002738 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002739 }
jeffhaode0d9c92012-02-27 13:58:13 -08002740 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2741 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2743 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002744 return NULL;
2745 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002746 // Check that interface methods match interface classes.
2747 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2748 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2749 << " is in an interface class " << PrettyClass(klass);
2750 return NULL;
2751 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2752 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2753 << " is in a non-interface class " << PrettyClass(klass);
2754 return NULL;
2755 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 // See if the method type implied by the invoke instruction matches the access flags for the
2757 // target method.
2758 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2759 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2760 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2761 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002762 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2763 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 return NULL;
2765 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002766 return res_method;
2767}
2768
Sebastien Hertz5243e912013-05-21 10:55:07 +02002769mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2770 MethodType method_type,
2771 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002772 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002773 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2774 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002775 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2776 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002777 if (res_method == NULL) { // error or class is unresolved
2778 return NULL;
2779 }
2780
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2782 // has a vtable entry for the target method.
2783 if (is_super) {
2784 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002785 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002786 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002787 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002788 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002789 << " to super " << PrettyMethod(res_method);
2790 return NULL;
2791 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002792 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002793 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002794 MethodHelper mh(res_method);
2795 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002797 << " to super " << super
2798 << "." << mh.GetName()
2799 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 return NULL;
2801 }
2802 }
2803 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2804 // match the call to the signature. Also, we might might be calling through an abstract method
2805 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 /* caught by static verifier */
2808 DCHECK(is_range || expected_args <= 5);
2809 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2812 return NULL;
2813 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002814
jeffhaobdb76512011-09-07 11:43:16 -07002815 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002816 * Check the "this" argument, which must be an instance of the class that declared the method.
2817 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2818 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002819 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002820 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 return NULL;
2825 }
2826 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 return NULL;
2829 }
2830 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002831 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07002832 const RegType& res_method_class = reg_types_.FromClass(ClassHelper(klass).GetDescriptor(),
2833 klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002834 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002835 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002836 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 return NULL;
2838 }
2839 }
2840 actual_args++;
2841 }
2842 /*
2843 * Process the target method's signature. This signature may or may not
2844 * have been verified, so we can't assume it's properly formed.
2845 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002846 MethodHelper mh(res_method);
2847 const DexFile::TypeList* params = mh.GetParameterTypeList();
2848 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002849 uint32_t arg[5];
2850 if (!is_range) {
2851 inst->GetArgs(arg);
2852 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002853 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002856 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2857 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 return NULL;
2859 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002860 const char* descriptor =
2861 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2862 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002864 << " missing signature component";
2865 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 }
Ian Rogersb4903572012-10-11 11:52:56 -07002867 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002868 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002869 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002870 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 }
2872 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2873 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002876 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 return NULL;
2878 } else {
2879 return res_method;
2880 }
2881}
2882
Sebastien Hertz5243e912013-05-21 10:55:07 +02002883void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled,
2884 bool is_range) {
2885 uint32_t type_idx;
2886 if (!is_filled) {
2887 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
2888 type_idx = inst->VRegC_22c();
2889 } else if (!is_range) {
2890 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
2891 type_idx = inst->VRegB_35c();
2892 } else {
2893 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
2894 type_idx = inst->VRegB_3rc();
2895 }
2896 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 if (res_type.IsConflict()) { // bad class
2898 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002899 } else {
2900 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2901 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002903 } else if (!is_filled) {
2904 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002905 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002906 /* set register type to array class */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002907 work_line_->SetRegisterType(inst->VRegA_22c(), res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002908 } else {
2909 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2910 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002911 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002912 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
2913 uint32_t arg[5];
2914 if (!is_range) {
2915 inst->GetArgs(arg);
2916 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002917 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002918 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002919 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002920 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002921 return;
2922 }
2923 }
2924 // filled-array result goes into "result" register
2925 work_line_->SetResultRegisterType(res_type);
2926 }
2927 }
2928}
2929
Sebastien Hertz5243e912013-05-21 10:55:07 +02002930void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002931 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002932 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002936 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002937 if (array_type.IsZero()) {
2938 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2939 // instruction type. TODO: have a proper notion of bottom here.
2940 if (!is_primitive || insn_type.IsCategory1Types()) {
2941 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02002942 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002944 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02002945 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002946 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002947 }
jeffhaofc3144e2012-02-01 17:21:15 -08002948 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002950 } else {
2951 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002952 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002953 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002955 << " source for aget-object";
2956 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002958 << " source for category 1 aget";
2959 } else if (is_primitive && !insn_type.Equals(component_type) &&
2960 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002961 (insn_type.IsLong() && component_type.IsDouble()))) {
2962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2963 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002964 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 // Use knowledge of the field type which is stronger than the type inferred from the
2966 // instruction, which can't differentiate object types and ints from floats, longs from
2967 // doubles.
2968 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002969 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002970 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002971 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002972 component_type.HighHalf(&reg_types_));
2973 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 }
2975 }
2976 }
2977}
2978
Sebastien Hertz5243e912013-05-21 10:55:07 +02002979void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002981 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002985 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08002986 if (array_type.IsZero()) {
2987 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2988 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002989 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002991 } else {
2992 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002993 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002994 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002996 << " source for aput-object";
2997 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002999 << " source for category 1 aput";
3000 } else if (is_primitive && !insn_type.Equals(component_type) &&
3001 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3002 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003004 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003006 // The instruction agrees with the type of array, confirm the value to be stored does too
3007 // Note: we use the instruction type (rather than the component type) for aput-object as
3008 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003009 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 }
3011 }
3012 }
3013}
3014
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003015mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003016 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3017 // Check access to class
3018 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003019 if (klass_type.IsConflict()) { // bad class
3020 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3021 field_idx, dex_file_->GetFieldName(field_id),
3022 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003023 return NULL;
3024 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003025 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003026 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003027 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003028 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003029 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003031 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003032 << dex_file_->GetFieldName(field_id) << ") in "
3033 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 DCHECK(Thread::Current()->IsExceptionPending());
3035 Thread::Current()->ClearException();
3036 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3038 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003040 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 return NULL;
3042 } else if (!field->IsStatic()) {
3043 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3044 return NULL;
3045 } else {
3046 return field;
3047 }
3048}
3049
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003050mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003051 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3052 // Check access to class
3053 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 if (klass_type.IsConflict()) {
3055 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3056 field_idx, dex_file_->GetFieldName(field_id),
3057 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003058 return NULL;
3059 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003060 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003061 return NULL; // Can't resolve Class so no more to do here
3062 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003063 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003064 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003066 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003067 << dex_file_->GetFieldName(field_id) << ") in "
3068 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 DCHECK(Thread::Current()->IsExceptionPending());
3070 Thread::Current()->ClearException();
3071 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003072 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3073 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 return NULL;
3077 } else if (field->IsStatic()) {
3078 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3079 << " to not be static";
3080 return NULL;
3081 } else if (obj_type.IsZero()) {
3082 // Cannot infer and check type, however, access will cause null pointer exception
3083 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003084 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003085 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003086 const RegType& field_klass =
3087 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
3088 klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003089 if (obj_type.IsUninitializedTypes() &&
3090 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3091 !field_klass.Equals(GetDeclaringClass()))) {
3092 // Field accesses through uninitialized references are only allowable for constructors where
3093 // the field is declared in this class
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3095 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003096 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003097 return NULL;
3098 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3099 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3100 // of C1. For resolution to occur the declared class of the field must be compatible with
3101 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3102 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3103 << " from object of type " << obj_type;
3104 return NULL;
3105 } else {
3106 return field;
3107 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
3109}
3110
Sebastien Hertz5243e912013-05-21 10:55:07 +02003111void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3112 bool is_primitive, bool is_static) {
3113 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003114 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003115 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003116 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003117 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003118 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003119 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003120 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003121 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003122 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003123 if (field != NULL) {
3124 descriptor = FieldHelper(field).GetTypeDescriptor();
3125 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003126 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3128 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3129 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003130 }
Ian Rogersb4903572012-10-11 11:52:56 -07003131 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003132 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003133 if (is_primitive) {
3134 if (field_type.Equals(insn_type) ||
3135 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3136 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3137 // expected that read is of the correct primitive type or that int reads are reading
3138 // floats or long reads are reading doubles
3139 } else {
3140 // This is a global failure rather than a class change failure as the instructions and
3141 // the descriptors for the type should have been consistent within the same file at
3142 // compile time
3143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3144 << " to be of type '" << insn_type
3145 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003146 return;
3147 }
3148 } else {
3149 if (!insn_type.IsAssignableFrom(field_type)) {
3150 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3151 << " to be compatible with type '" << insn_type
3152 << "' but found type '" << field_type
3153 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003154 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003155 return;
3156 }
3157 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003158 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003159 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003160 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003161 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003162 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003163}
3164
Sebastien Hertz5243e912013-05-21 10:55:07 +02003165void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3166 bool is_primitive, bool is_static) {
3167 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003168 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003169 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003170 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003171 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003172 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003173 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003174 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003175 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003176 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003177 if (field != NULL) {
3178 descriptor = FieldHelper(field).GetTypeDescriptor();
3179 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003180 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003181 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3182 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3183 loader = class_loader_;
3184 }
Ian Rogersb4903572012-10-11 11:52:56 -07003185 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003186 if (field != NULL) {
3187 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3188 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3189 << " from other class " << GetDeclaringClass();
3190 return;
3191 }
3192 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003193 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003194 if (is_primitive) {
3195 // Primitive field assignability rules are weaker than regular assignability rules
3196 bool instruction_compatible;
3197 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003198 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003199 if (field_type.IsIntegralTypes()) {
3200 instruction_compatible = insn_type.IsIntegralTypes();
3201 value_compatible = value_type.IsIntegralTypes();
3202 } else if (field_type.IsFloat()) {
3203 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3204 value_compatible = value_type.IsFloatTypes();
3205 } else if (field_type.IsLong()) {
3206 instruction_compatible = insn_type.IsLong();
3207 value_compatible = value_type.IsLongTypes();
3208 } else if (field_type.IsDouble()) {
3209 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3210 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003211 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003212 instruction_compatible = false; // reference field with primitive store
3213 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003215 if (!instruction_compatible) {
3216 // This is a global failure rather than a class change failure as the instructions and
3217 // the descriptors for the type should have been consistent within the same file at
3218 // compile time
3219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3220 << " to be of type '" << insn_type
3221 << "' but found type '" << field_type
3222 << "' in put";
3223 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003224 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003225 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003227 << " of type " << value_type
3228 << " but expected " << field_type
3229 << " for store to " << PrettyField(field) << " in put";
3230 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003232 } else {
3233 if (!insn_type.IsAssignableFrom(field_type)) {
3234 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3235 << " to be compatible with type '" << insn_type
3236 << "' but found type '" << field_type
3237 << "' in put-object";
3238 return;
3239 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003240 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 }
3242}
3243
Ian Rogers776ac1f2012-04-13 23:36:36 -07003244bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 return false;
3248 }
3249 return true;
3250}
3251
Ian Rogers776ac1f2012-04-13 23:36:36 -07003252bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 bool changed = true;
3254 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3255 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003256 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 * We haven't processed this instruction before, and we haven't touched the registers here, so
3258 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3259 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003260 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003262 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003263 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3264 if (gDebugVerify) {
3265 copy->CopyFromLine(target_line);
3266 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003267 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003268 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003270 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003271 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003272 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003273 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3274 << *copy.get() << " MERGE\n"
3275 << *merge_line << " ==\n"
3276 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003277 }
3278 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 if (changed) {
3280 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003281 }
3282 return true;
3283}
3284
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003285InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003286 return &insn_flags_[work_insn_idx_];
3287}
3288
Ian Rogersad0b3a32012-04-16 14:50:24 -07003289const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003290 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003291 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3292 uint16_t return_type_idx = proto_id.return_type_idx_;
3293 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003294 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003295}
3296
3297const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003298 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003299 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003300 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003301 if (mirror_method_ != NULL) {
3302 mirror::Class* klass = mirror_method_->GetDeclaringClass();
3303 declaring_class_ = &reg_types_.FromClass(descriptor, klass, klass->IsFinal());
3304 } else {
3305 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3306 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003307 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003308 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003309}
3310
Ian Rogers776ac1f2012-04-13 23:36:36 -07003311void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 size_t* log2_max_gc_pc) {
3313 size_t local_gc_points = 0;
3314 size_t max_insn = 0;
3315 size_t max_ref_reg = -1;
3316 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003317 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 local_gc_points++;
3319 max_insn = i;
3320 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003321 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003322 }
3323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 *gc_points = local_gc_points;
3325 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3326 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003327 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 i++;
3329 }
3330 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003331}
3332
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003333MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3334 /*
3335 * Walks over the method code and adds any cast instructions in which
3336 * the type cast is implicit to a set, which is used in the code generation
3337 * to elide these casts.
3338 */
3339 if (!failure_messages_.empty()) {
3340 return NULL;
3341 }
3342 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003343 const Instruction* inst = Instruction::At(code_item_->insns_);
3344 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003345 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003346
3347 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003348 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003349 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003350 }
3351 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003352 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003353 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3354 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003355 if (cast_type.IsAssignableFrom(reg_type)) {
3356 if (mscs.get() == NULL) {
3357 mscs.reset(new MethodSafeCastSet());
3358 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003359 mscs->insert(dex_pc);
3360 }
3361 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003362 return mscs.release();
3363}
3364
Ian Rogersd0583802013-06-01 10:51:46 -07003365MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003366
3367 // It is risky to rely on reg_types for sharpening in cases of soft
3368 // verification, we might end up sharpening to a wrong implementation. Just abort.
3369 if (!failure_messages_.empty()) {
3370 return NULL;
3371 }
3372
Ian Rogersd0583802013-06-01 10:51:46 -07003373 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003374 const uint16_t* insns = code_item_->insns_ ;
3375 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003376 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003377
Ian Rogersd0583802013-06-01 10:51:46 -07003378 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003379 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3380 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3381 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3382 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3383
Ian Rogersd0583802013-06-01 10:51:46 -07003384 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003385 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003386 }
Ian Rogersd0583802013-06-01 10:51:46 -07003387 // Get reg type for register holding the reference to the object that will be dispatched upon.
3388 uint32_t dex_pc = inst->GetDexPc(insns);
3389 RegisterLine* line = reg_table_.GetLine(dex_pc);
3390 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3391 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3392 const RegType&
3393 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003394
Ian Rogersd0583802013-06-01 10:51:46 -07003395 if (!reg_type.HasClass()) {
3396 // We will compute devirtualization information only when we know the Class of the reg type.
3397 continue;
3398 }
3399 mirror::Class* reg_class = reg_type.GetClass();
3400 if (reg_class->IsInterface()) {
3401 // We can't devirtualize when the known type of the register is an interface.
3402 continue;
3403 }
3404 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3405 // We can't devirtualize abstract classes except on arrays of abstract classes.
3406 continue;
3407 }
3408 mirror::AbstractMethod* abstract_method =
3409 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003410 if(abstract_method == NULL) {
3411 // If the method is not found in the cache this means that it was never found
3412 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3413 continue;
3414 }
3415 // Find the concrete method.
3416 mirror::AbstractMethod* concrete_method = NULL;
3417 if (is_interface) {
3418 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3419 }
3420 if (is_virtual) {
3421 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3422 }
Ian Rogersd0583802013-06-01 10:51:46 -07003423 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3424 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003425 continue;
3426 }
Ian Rogersd0583802013-06-01 10:51:46 -07003427 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3428 concrete_method->GetDeclaringClass()->IsFinal()) {
3429 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3430 // overridden record the target to be used in the compiler driver.
3431 if (pc_to_concrete_method_map.get() == NULL) {
3432 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3433 }
3434 CompilerDriver::MethodReference concrete_ref(
3435 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3436 concrete_method->GetDexMethodIndex());
3437 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3438 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003439 }
Ian Rogersd0583802013-06-01 10:51:46 -07003440 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003441}
3442
Ian Rogers776ac1f2012-04-13 23:36:36 -07003443const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 size_t num_entries, ref_bitmap_bits, pc_bits;
3445 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3446 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003447 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003451 return NULL;
3452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3454 // There are 2 bytes to encode the number of entries
3455 if (num_entries >= 65536) {
3456 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003459 return NULL;
3460 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003462 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003463 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003464 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003466 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003467 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003469 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3473 return NULL;
3474 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003475 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003476 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003477 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 return NULL;
3480 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003481 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003483 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3484 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003485 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003486 table->push_back(num_entries & 0xFF);
3487 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003490 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003491 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003493 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003494 }
3495 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003496 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 }
3498 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003499 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 return table;
3501}
jeffhaoa0a764a2011-09-16 10:43:38 -07003502
Ian Rogers776ac1f2012-04-13 23:36:36 -07003503void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3505 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003506 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003507 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003508 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003510 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003512 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3514 map_index++;
3515 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003516 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003517 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003518 CHECK_LT(j / 8, map.RegWidth());
3519 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3520 } else if ((j / 8) < map.RegWidth()) {
3521 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3522 } else {
3523 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3524 }
3525 }
3526 } else {
3527 CHECK(reg_bitmap == NULL);
3528 }
3529 }
3530}
jeffhaoa0a764a2011-09-16 10:43:38 -07003531
Ian Rogers637c65b2013-05-31 11:46:00 -07003532void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3533 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003534 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003535 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003536 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3537 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003538 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003539 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003540 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003541 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003542 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003543 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003544}
3545
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003546
Ian Rogersfae370a2013-06-05 08:33:27 -07003547void MethodVerifier::SetSafeCastMap(CompilerDriver::MethodReference ref,
3548 const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003549 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3550 SafeCastMap::iterator it = safecast_map_->find(ref);
3551 if (it != safecast_map_->end()) {
3552 delete it->second;
3553 safecast_map_->erase(it);
3554 }
3555
3556 safecast_map_->Put(ref, cast_set);
3557 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3558}
3559
3560bool MethodVerifier::IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) {
3561 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3562 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3563 if (it == safecast_map_->end()) {
3564 return false;
3565 }
3566
3567 // Look up the cast address in the set of safe casts
3568 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3569 return cast_it != it->second->end();
3570}
3571
Ian Rogers637c65b2013-05-31 11:46:00 -07003572const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3573 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3574 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3575 if (it == dex_gc_maps_->end()) {
3576 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3577 return NULL;
3578 }
3579 CHECK(it->second != NULL);
3580 return it->second;
3581}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003582
Ian Rogers637c65b2013-05-31 11:46:00 -07003583void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003584 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003585 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003586 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3587 if (it != devirt_maps_->end()) {
3588 delete it->second;
3589 devirt_maps_->erase(it);
3590 }
3591
3592 devirt_maps_->Put(ref, devirt_map);
3593 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3594}
3595
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003596const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3597 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003598 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003599 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3600 if (it == devirt_maps_->end()) {
3601 return NULL;
3602 }
3603
3604 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003605 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003606 if(pc_to_concrete_method != it->second->end()) {
3607 return &(pc_to_concrete_method->second);
3608 } else {
3609 return NULL;
3610 }
3611}
3612
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003613std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3614 RegisterLine* line = reg_table_.GetLine(dex_pc);
3615 std::vector<int32_t> result;
3616 for (size_t i = 0; i < line->NumRegs(); ++i) {
3617 const RegType& type = line->GetRegisterType(i);
3618 if (type.IsConstant()) {
3619 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3620 result.push_back(type.ConstantValue());
3621 } else if (type.IsConstantLo()) {
3622 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3623 result.push_back(type.ConstantValueLo());
3624 } else if (type.IsConstantHi()) {
3625 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3626 result.push_back(type.ConstantValueHi());
3627 } else if (type.IsIntegralTypes()) {
3628 result.push_back(kIntVReg);
3629 result.push_back(0);
3630 } else if (type.IsFloat()) {
3631 result.push_back(kFloatVReg);
3632 result.push_back(0);
3633 } else if (type.IsLong()) {
3634 result.push_back(kLongLoVReg);
3635 result.push_back(0);
3636 result.push_back(kLongHiVReg);
3637 result.push_back(0);
3638 ++i;
3639 } else if (type.IsDouble()) {
3640 result.push_back(kDoubleLoVReg);
3641 result.push_back(0);
3642 result.push_back(kDoubleHiVReg);
3643 result.push_back(0);
3644 ++i;
3645 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3646 result.push_back(kUndefined);
3647 result.push_back(0);
3648 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003649 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003650 result.push_back(kReferenceVReg);
3651 result.push_back(0);
3652 }
3653 }
3654 return result;
3655}
3656
Ian Rogers637c65b2013-05-31 11:46:00 -07003657ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003658MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003659
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003660Mutex* MethodVerifier::safecast_map_lock_ = NULL;
3661MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
3662
Ian Rogers637c65b2013-05-31 11:46:00 -07003663ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003664MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3665
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003666Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3667MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3668
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003669void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003670 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003671 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003672 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003673 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003674 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003675 }
3676
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003677 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
3678 {
3679 MutexLock mu(self, *safecast_map_lock_);
3680 safecast_map_ = new MethodVerifier::SafeCastMap();
3681 }
3682
Ian Rogers637c65b2013-05-31 11:46:00 -07003683 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003684
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003685 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003686 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003687 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3688 }
3689
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003690 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3691 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003692 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003693 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3694 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003695 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003696}
3697
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003698void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003699 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003700 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003701 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003702 STLDeleteValues(dex_gc_maps_);
3703 delete dex_gc_maps_;
3704 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003705 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003706 delete dex_gc_maps_lock_;
3707 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003708
3709 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003710 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003711 STLDeleteValues(devirt_maps_);
3712 delete devirt_maps_;
3713 devirt_maps_ = NULL;
3714 }
3715 delete devirt_maps_lock_;
3716 devirt_maps_lock_ = NULL;
3717
3718 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003719 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003720 delete rejected_classes_;
3721 rejected_classes_ = NULL;
3722 }
3723 delete rejected_classes_lock_;
3724 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003725 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003726}
jeffhaod1224c72012-02-29 13:43:08 -08003727
Ian Rogers1212a022013-03-04 10:48:41 -08003728void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003729 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003730 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003731 rejected_classes_->insert(ref);
3732 }
jeffhaod1224c72012-02-29 13:43:08 -08003733 CHECK(IsClassRejected(ref));
3734}
3735
Ian Rogers1212a022013-03-04 10:48:41 -08003736bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003737 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003738 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003739}
3740
Ian Rogersd81871c2011-10-03 13:57:23 -07003741} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003742} // namespace art