blob: fc285bcdc7919ed03683de30ab050e1dd82169a1 [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction.h"
28#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/abstract_method-inl.h"
34#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/field-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
49
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
62 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070077 std::string& error,
78 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080084 error = "Verifier rejected class ";
85 error += PrettyDescriptor(klass);
86 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
90 error = "Verifier rejected class ";
91 error += PrettyDescriptor(klass);
92 error += " that attempts to sub-class final class ";
93 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
98 uint32_t class_def_idx;
99 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
100 error = "Verifier rejected class ";
101 error += PrettyDescriptor(klass);
102 error += " that isn't present in dex file ";
103 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Jeff Haoee988952013-04-16 14:23:47 -0700106 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700107}
108
Ian Rogers365c1022012-06-22 15:05:28 -0700109MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::DexCache* dex_cache,
111 mirror::ClassLoader* class_loader,
112 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700113 std::string& error,
114 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800115 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
116 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700117 if (class_data == NULL) {
118 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700119 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700120 }
jeffhaof56197c2012-03-05 18:01:54 -0800121 ClassDataItemIterator it(*dex_file, class_data);
122 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
123 it.Next();
124 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700126 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700127 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700128 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800129 while (it.HasNextDirectMethod()) {
130 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700131 if (method_idx == previous_direct_method_idx) {
132 // smali can create dex files with two encoded_methods sharing the same method_idx
133 // http://code.google.com/p/smali/issues/detail?id=119
134 it.Next();
135 continue;
136 }
137 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::AbstractMethod* method =
140 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 if (method == NULL) {
142 DCHECK(Thread::Current()->IsExceptionPending());
143 // We couldn't resolve the method, but continue regardless.
144 Thread::Current()->ClearException();
145 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700147 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700148 if (result != kNoFailure) {
149 if (result == kHardFailure) {
150 hard_fail = true;
151 if (error_count > 0) {
152 error += "\n";
153 }
154 error = "Verifier rejected class ";
155 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
156 error += " due to bad method ";
157 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800160 }
161 it.Next();
162 }
jeffhao9b0b1882012-10-01 16:51:22 -0700163 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800164 while (it.HasNextVirtualMethod()) {
165 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700166 if (method_idx == previous_virtual_method_idx) {
167 // smali can create dex files with two encoded_methods sharing the same method_idx
168 // http://code.google.com/p/smali/issues/detail?id=119
169 it.Next();
170 continue;
171 }
172 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::AbstractMethod* method =
175 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 if (method == NULL) {
177 DCHECK(Thread::Current()->IsExceptionPending());
178 // We couldn't resolve the method, but continue regardless.
179 Thread::Current()->ClearException();
180 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700182 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700183 if (result != kNoFailure) {
184 if (result == kHardFailure) {
185 hard_fail = true;
186 if (error_count > 0) {
187 error += "\n";
188 }
189 error = "Verifier rejected class ";
190 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
191 error += " due to bad method ";
192 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800195 }
196 it.Next();
197 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 if (error_count == 0) {
199 return kNoFailure;
200 } else {
201 return hard_fail ? kHardFailure : kSoftFailure;
202 }
jeffhaof56197c2012-03-05 18:01:54 -0800203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
206 const DexFile* dex_file,
207 mirror::DexCache* dex_cache,
208 mirror::ClassLoader* class_loader,
209 uint32_t class_def_idx,
210 const DexFile::CodeItem* code_item,
211 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700212 uint32_t method_access_flags,
213 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700214 MethodVerifier::FailureKind result = kNoFailure;
215 uint64_t start_ns = NanoTime();
216
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700218 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Verification completed, however failures may be pending that didn't cause the verification
221 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700222 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 if (verifier.failures_.size() != 0) {
224 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700225 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700226 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // Bad method data.
230 CHECK_NE(verifier.failures_.size(), 0U);
231 CHECK(verifier.have_pending_hard_failure_);
232 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800234 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700235 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800236 verifier.Dump(std::cout);
237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800239 }
Ian Rogersc8982582012-09-07 16:53:25 -0700240 uint64_t duration_ns = NanoTime() - start_ns;
241 if (duration_ns > MsToNs(100)) {
242 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
243 << " took " << PrettyDuration(duration_ns);
244 }
245 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800246}
247
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile* dex_file, mirror::DexCache* dex_cache,
250 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
251 const DexFile::CodeItem* code_item,
252 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800253 uint32_t method_access_flags) {
254 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700255 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257 verifier.DumpFailures(os);
258 os << verifier.info_messages_.str();
259 verifier.Dump(os);
260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
263 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
264 const DexFile::CodeItem* code_item,
265 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700266 uint32_t method_access_flags, bool can_load_classes,
267 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800268 : reg_types_(can_load_classes),
269 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700271 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800273 dex_file_(dex_file),
274 dex_cache_(dex_cache),
275 class_loader_(class_loader),
276 class_def_idx_(class_def_idx),
277 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700278 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700279 interesting_dex_pc_(-1),
280 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700281 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700282 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800283 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800284 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700285 can_load_classes_(can_load_classes),
286 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800290 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700291 MethodHelper mh(m);
292 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
293 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700294 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700295 verifier.interesting_dex_pc_ = dex_pc;
296 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
297 verifier.FindLocksAtDexPc();
298}
299
300void MethodVerifier::FindLocksAtDexPc() {
301 CHECK(monitor_enter_dex_pcs_ != NULL);
302 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
303
304 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
305 // verification. In practice, the phase we want relies on data structures set up by all the
306 // earlier passes, so we just run the full method verification and bail out early when we've
307 // got what we wanted.
308 Verify();
309}
310
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700312 // If there aren't any instructions, make sure that's expected, then exit successfully.
313 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700316 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700317 } else {
318 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700319 }
jeffhaobdb76512011-09-07 11:43:16 -0700320 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700321 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
322 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
324 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700326 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800328 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 // Run through the instructions and see if the width checks out.
330 bool result = ComputeWidthsAndCountOps();
331 // Flag instructions guarded by a "try" block and check exception handlers.
332 result = result && ScanTryCatchBlocks();
333 // Perform static instruction verification.
334 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 // Perform code-flow analysis and return.
336 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700337}
338
Ian Rogers776ac1f2012-04-13 23:36:36 -0700339std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700340 switch (error) {
341 case VERIFY_ERROR_NO_CLASS:
342 case VERIFY_ERROR_NO_FIELD:
343 case VERIFY_ERROR_NO_METHOD:
344 case VERIFY_ERROR_ACCESS_CLASS:
345 case VERIFY_ERROR_ACCESS_FIELD:
346 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700347 case VERIFY_ERROR_INSTANTIATION:
348 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800349 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700350 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
351 // class change and instantiation errors into soft verification errors so that we re-verify
352 // at runtime. We may fail to find or to agree on access because of not yet available class
353 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
354 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
355 // paths" that dynamically perform the verification and cause the behavior to be that akin
356 // to an interpreter.
357 error = VERIFY_ERROR_BAD_CLASS_SOFT;
358 } else {
359 have_pending_runtime_throw_failure_ = true;
360 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700362 // Indication that verification should be retried at runtime.
363 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700364 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 have_pending_hard_failure_ = true;
366 }
367 break;
jeffhaod5347e02012-03-22 17:25:05 -0700368 // Hard verification failures at compile time will still fail at runtime, so the class is
369 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370 case VERIFY_ERROR_BAD_CLASS_HARD: {
371 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800372 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800373 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700375 have_pending_hard_failure_ = true;
376 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800377 }
378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800380 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700381 work_insn_idx_));
382 std::ostringstream* failure_message = new std::ostringstream(location);
383 failure_messages_.push_back(failure_message);
384 return *failure_message;
385}
386
387void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
388 size_t failure_num = failure_messages_.size();
389 DCHECK_NE(failure_num, 0U);
390 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
391 prepend += last_fail_message->str();
392 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
393 delete last_fail_message;
394}
395
396void MethodVerifier::AppendToLastFailMessage(std::string append) {
397 size_t failure_num = failure_messages_.size();
398 DCHECK_NE(failure_num, 0U);
399 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
400 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800401}
402
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 const uint16_t* insns = code_item_->insns_;
405 size_t insns_size = code_item_->insns_size_in_code_units_;
406 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700407 size_t new_instance_count = 0;
408 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700410
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700412 Instruction::Code opcode = inst->Opcode();
413 if (opcode == Instruction::NEW_INSTANCE) {
414 new_instance_count++;
415 } else if (opcode == Instruction::MONITOR_ENTER) {
416 monitor_enter_count++;
417 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 size_t inst_size = inst->SizeInCodeUnits();
419 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
420 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700421 inst = inst->Next();
422 }
423
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
426 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700427 return false;
428 }
429
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 new_instance_count_ = new_instance_count;
431 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700432 return true;
433}
434
Ian Rogers776ac1f2012-04-13 23:36:36 -0700435bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700437 if (tries_size == 0) {
438 return true;
439 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700441 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700442
443 for (uint32_t idx = 0; idx < tries_size; idx++) {
444 const DexFile::TryItem* try_item = &tries[idx];
445 uint32_t start = try_item->start_addr_;
446 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700447 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
449 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700450 return false;
451 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700454 return false;
455 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700456 for (uint32_t dex_pc = start; dex_pc < end;
457 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
458 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700459 }
460 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800461 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700462 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700463 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700464 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700465 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700466 CatchHandlerIterator iterator(handlers_ptr);
467 for (; iterator.HasNext(); iterator.Next()) {
468 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700469 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700471 return false;
472 }
jeffhao60f83e32012-02-13 17:16:30 -0800473 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
474 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800477 return false;
478 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700479 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700480 // Ensure exception types are resolved so that they don't need resolution to be delivered,
481 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700482 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
484 iterator.GetHandlerTypeIndex(),
485 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700486 if (exception_type == NULL) {
487 DCHECK(Thread::Current()->IsExceptionPending());
488 Thread::Current()->ClearException();
489 }
490 }
jeffhaobdb76512011-09-07 11:43:16 -0700491 }
Ian Rogers0571d352011-11-03 19:51:38 -0700492 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700493 }
jeffhaobdb76512011-09-07 11:43:16 -0700494 return true;
495}
496
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700498 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700499
Ian Rogers0c7abda2012-09-19 13:33:42 -0700500 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700502 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700503
504 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700505 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 return false;
509 }
510 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700511 // All invoke points are marked as "Throw" points already.
512 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700514 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700515 }
516 dex_pc += inst->SizeInCodeUnits();
517 inst = inst->Next();
518 }
519 return true;
520}
521
Ian Rogers776ac1f2012-04-13 23:36:36 -0700522bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800523 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 bool result = true;
525 switch (inst->GetVerifyTypeArgumentA()) {
526 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 break;
529 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800530 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 break;
532 }
533 switch (inst->GetVerifyTypeArgumentB()) {
534 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800535 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 break;
537 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800538 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 break;
540 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800541 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 break;
543 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800544 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 break;
546 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800547 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 break;
549 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800553 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 break;
555 }
556 switch (inst->GetVerifyTypeArgumentC()) {
557 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800558 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 break;
560 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800561 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 break;
563 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800564 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 break;
566 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800567 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 break;
572 }
573 switch (inst->GetVerifyExtraFlags()) {
574 case Instruction::kVerifyArrayData:
575 result = result && CheckArrayData(code_offset);
576 break;
577 case Instruction::kVerifyBranchTarget:
578 result = result && CheckBranchTarget(code_offset);
579 break;
580 case Instruction::kVerifySwitchTargets:
581 result = result && CheckSwitchTargets(code_offset);
582 break;
583 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800587 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 break;
589 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 result = false;
592 break;
593 }
594 return result;
595}
596
Ian Rogers776ac1f2012-04-13 23:36:36 -0700597bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
600 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 return false;
602 }
603 return true;
604}
605
Ian Rogers776ac1f2012-04-13 23:36:36 -0700606bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
609 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 return false;
611 }
612 return true;
613}
614
Ian Rogers776ac1f2012-04-13 23:36:36 -0700615bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
618 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 return false;
620 }
621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
627 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 return false;
629 }
630 return true;
631}
632
Ian Rogers776ac1f2012-04-13 23:36:36 -0700633bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
636 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 return false;
638 }
639 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700640 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 return false;
644 }
645 return true;
646}
647
Ian Rogers776ac1f2012-04-13 23:36:36 -0700648bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
651 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 return false;
653 }
654 return true;
655}
656
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
660 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700661 return false;
662 }
663 return true;
664}
665
Ian Rogers776ac1f2012-04-13 23:36:36 -0700666bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
669 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 return false;
671 }
672 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700673 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 const char* cp = descriptor;
675 while (*cp++ == '[') {
676 bracket_count++;
677 }
678 if (bracket_count == 0) {
679 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 return false;
682 } else if (bracket_count > 255) {
683 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 return false;
686 }
687 return true;
688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
692 const uint16_t* insns = code_item_->insns_ + cur_offset;
693 const uint16_t* array_data;
694 int32_t array_data_offset;
695
696 DCHECK_LT(cur_offset, insn_count);
697 /* make sure the start of the array data table is in range */
698 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
699 if ((int32_t) cur_offset + array_data_offset < 0 ||
700 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
702 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 /* offset to array data table is a relative branch-style offset */
706 array_data = insns + array_data_offset;
707 /* make sure the table is 32-bit aligned */
708 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
710 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700714 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
716 /* make sure the end of the switch is in range */
717 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
719 << ", data offset " << array_data_offset << ", end "
720 << cur_offset + array_data_offset + table_size
721 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 return false;
723 }
724 return true;
725}
726
Ian Rogers776ac1f2012-04-13 23:36:36 -0700727bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 int32_t offset;
729 bool isConditional, selfOkay;
730 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
731 return false;
732 }
733 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700737 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
738 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
744 int32_t abs_offset = cur_offset + offset;
745 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700747 << reinterpret_cast<void*>(abs_offset) << ") at "
748 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 insn_flags_[abs_offset].SetBranchTarget();
752 return true;
753}
754
Ian Rogers776ac1f2012-04-13 23:36:36 -0700755bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 bool* selfOkay) {
757 const uint16_t* insns = code_item_->insns_ + cur_offset;
758 *pConditional = false;
759 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700760 switch (*insns & 0xff) {
761 case Instruction::GOTO:
762 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700763 break;
764 case Instruction::GOTO_32:
765 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700766 *selfOkay = true;
767 break;
768 case Instruction::GOTO_16:
769 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700770 break;
771 case Instruction::IF_EQ:
772 case Instruction::IF_NE:
773 case Instruction::IF_LT:
774 case Instruction::IF_GE:
775 case Instruction::IF_GT:
776 case Instruction::IF_LE:
777 case Instruction::IF_EQZ:
778 case Instruction::IF_NEZ:
779 case Instruction::IF_LTZ:
780 case Instruction::IF_GEZ:
781 case Instruction::IF_GTZ:
782 case Instruction::IF_LEZ:
783 *pOffset = (int16_t) insns[1];
784 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700785 break;
786 default:
787 return false;
788 break;
789 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700795 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700797 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
799 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
801 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 return false;
803 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700806 /* make sure the table is 32-bit aligned */
807 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
809 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 return false;
811 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 uint32_t switch_count = switch_insns[1];
813 int32_t keys_offset, targets_offset;
814 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700815 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
816 /* 0=sig, 1=count, 2/3=firstKey */
817 targets_offset = 4;
818 keys_offset = -1;
819 expected_signature = Instruction::kPackedSwitchSignature;
820 } else {
821 /* 0=sig, 1=count, 2..count*2 = keys */
822 keys_offset = 2;
823 targets_offset = 2 + 2 * switch_count;
824 expected_signature = Instruction::kSparseSwitchSignature;
825 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
829 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 return false;
831 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700832 /* make sure the end of the switch is in range */
833 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
835 << switch_offset << ", end "
836 << (cur_offset + switch_offset + table_size)
837 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 return false;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 /* for a sparse switch, verify the keys are in ascending order */
841 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
843 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700844 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
845 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
846 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
848 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 return false;
850 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 last_key = key;
852 }
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 for (uint32_t targ = 0; targ < switch_count; targ++) {
856 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
857 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
858 int32_t abs_offset = cur_offset + offset;
859 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700861 << reinterpret_cast<void*>(abs_offset) << ") at "
862 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 insn_flags_[abs_offset].SetBranchTarget();
866 }
867 return true;
868}
869
Ian Rogers776ac1f2012-04-13 23:36:36 -0700870bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
874 }
875 uint16_t registers_size = code_item_->registers_size_;
876 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800877 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
879 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 }
883
884 return true;
885}
886
Ian Rogers776ac1f2012-04-13 23:36:36 -0700887bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 uint16_t registers_size = code_item_->registers_size_;
889 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
890 // integer overflow when adding them here.
891 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
893 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return true;
897}
898
Ian Rogers0c7abda2012-09-19 13:33:42 -0700899static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800900 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700901 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800902 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
903 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
904 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
905 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
906 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
907 gc_map.begin(),
908 gc_map.end());
909 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
910 DCHECK_EQ(gc_map.size(),
911 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
912 (length_prefixed_gc_map->at(1) << 16) |
913 (length_prefixed_gc_map->at(2) << 8) |
914 (length_prefixed_gc_map->at(3) << 0)));
915 return length_prefixed_gc_map;
916}
917
Ian Rogers776ac1f2012-04-13 23:36:36 -0700918bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 uint16_t registers_size = code_item_->registers_size_;
920 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700921
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800923 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
924 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 }
926 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700927 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
928
jeffhaobdb76512011-09-07 11:43:16 -0700929
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 work_line_.reset(new RegisterLine(registers_size, this));
931 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700932
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 /* Initialize register types of method arguments. */
934 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 DCHECK_NE(failures_.size(), 0U);
936 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800937 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 /* Perform code flow verification. */
942 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700943 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946
Ian Rogers1212a022013-03-04 10:48:41 -0800947 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700948
TDYa127b2eb5c12012-05-24 15:52:10 -0700949
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800951 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
952 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false; // Not a real failure, but a failure to encode
955 }
Ian Rogers39ebcb82013-05-30 16:57:23 -0700956 if (kIsDebugBuild) {
957 VerifyGcMap(*map);
958 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700959 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
960 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800961
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700962 MethodVerifier::PcToConcreteMethod* pc_to_concrete_method = GenerateDevirtMap();
963 if(pc_to_concrete_method != NULL ) {
964 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700965 }
jeffhaobdb76512011-09-07 11:43:16 -0700966 return true;
967}
968
Ian Rogersad0b3a32012-04-16 14:50:24 -0700969std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
970 DCHECK_EQ(failures_.size(), failure_messages_.size());
971 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700972 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700973 }
974 return os;
975}
976
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700978 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700979 v->Dump(std::cerr);
980}
981
Ian Rogers776ac1f2012-04-13 23:36:36 -0700982void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800983 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700984 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 return;
jeffhaobdb76512011-09-07 11:43:16 -0700986 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 {
988 os << "Register Types:\n";
989 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
990 std::ostream indent_os(&indent_filter);
991 reg_types_.Dump(indent_os);
992 }
Ian Rogersb4903572012-10-11 11:52:56 -0700993 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800994 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
995 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 const Instruction* inst = Instruction::At(code_item_->insns_);
997 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
998 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1000 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001002 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001003 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001004 const bool kDumpHexOfInstruction = false;
1005 if (kDumpHexOfInstruction) {
1006 indent_os << inst->DumpHex(5) << " ";
1007 }
1008 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 inst = inst->Next();
1010 }
jeffhaobdb76512011-09-07 11:43:16 -07001011}
1012
Ian Rogersd81871c2011-10-03 13:57:23 -07001013static bool IsPrimitiveDescriptor(char descriptor) {
1014 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001015 case 'I':
1016 case 'C':
1017 case 'S':
1018 case 'B':
1019 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001020 case 'F':
1021 case 'D':
1022 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001024 default:
1025 return false;
1026 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001027}
1028
Ian Rogers776ac1f2012-04-13 23:36:36 -07001029bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 RegisterLine* reg_line = reg_table_.GetLine(0);
1031 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1032 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1035 //Include the "this" pointer.
1036 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001037 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1039 // argument as uninitialized. This restricts field access until the superclass constructor is
1040 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001041 const RegType& declaring_class = GetDeclaringClass();
1042 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 reg_line->SetRegisterType(arg_start + cur_arg,
1044 reg_types_.UninitializedThisArgument(declaring_class));
1045 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001046 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001047 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001049 }
1050
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001051 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001052 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001053 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001054
1055 for (; iterator.HasNext(); iterator.Next()) {
1056 const char* descriptor = iterator.GetDescriptor();
1057 if (descriptor == NULL) {
1058 LOG(FATAL) << "Null descriptor";
1059 }
1060 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1062 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 return false;
1064 }
1065 switch (descriptor[0]) {
1066 case 'L':
1067 case '[':
1068 // We assume that reference arguments are initialized. The only way it could be otherwise
1069 // (assuming the caller was verified) is if the current method is <init>, but in that case
1070 // it's effectively considered initialized the instant we reach here (in the sense that we
1071 // can return without doing anything or call virtual methods).
1072 {
Ian Rogersb4903572012-10-11 11:52:56 -07001073 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001074 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 }
1076 break;
1077 case 'Z':
1078 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1079 break;
1080 case 'C':
1081 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1082 break;
1083 case 'B':
1084 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1085 break;
1086 case 'I':
1087 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1088 break;
1089 case 'S':
1090 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1091 break;
1092 case 'F':
1093 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1094 break;
1095 case 'J':
1096 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001097 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1098 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1099 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 cur_arg++;
1101 break;
1102 }
1103 default:
jeffhaod5347e02012-03-22 17:25:05 -07001104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 cur_arg++;
1108 }
1109 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1114 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1115 // format. Only major difference from the method argument format is that 'V' is supported.
1116 bool result;
1117 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1118 result = descriptor[1] == '\0';
1119 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1120 size_t i = 0;
1121 do {
1122 i++;
1123 } while (descriptor[i] == '['); // process leading [
1124 if (descriptor[i] == 'L') { // object array
1125 do {
1126 i++; // find closing ;
1127 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1128 result = descriptor[i] == ';';
1129 } else { // primitive array
1130 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1131 }
1132 } else if (descriptor[0] == 'L') {
1133 // could be more thorough here, but shouldn't be required
1134 size_t i = 0;
1135 do {
1136 i++;
1137 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1138 result = descriptor[i] == ';';
1139 } else {
1140 result = false;
1141 }
1142 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1144 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 }
1146 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001147}
1148
Ian Rogers776ac1f2012-04-13 23:36:36 -07001149bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 const uint16_t* insns = code_item_->insns_;
1151 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001152
jeffhaobdb76512011-09-07 11:43:16 -07001153 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 insn_flags_[0].SetChanged();
1155 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001156
jeffhaobdb76512011-09-07 11:43:16 -07001157 /* Continue until no instructions are marked "changed". */
1158 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1160 uint32_t insn_idx = start_guess;
1161 for (; insn_idx < insns_size; insn_idx++) {
1162 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001163 break;
1164 }
jeffhaobdb76512011-09-07 11:43:16 -07001165 if (insn_idx == insns_size) {
1166 if (start_guess != 0) {
1167 /* try again, starting from the top */
1168 start_guess = 0;
1169 continue;
1170 } else {
1171 /* all flags are clear */
1172 break;
1173 }
1174 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 // We carry the working set of registers from instruction to instruction. If this address can
1176 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1177 // "changed" flags, we need to load the set of registers from the table.
1178 // Because we always prefer to continue on to the next instruction, we should never have a
1179 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1180 // target.
1181 work_insn_idx_ = insn_idx;
1182 if (insn_flags_[insn_idx].IsBranchTarget()) {
1183 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001184 } else {
1185#ifndef NDEBUG
1186 /*
1187 * Sanity check: retrieve the stored register line (assuming
1188 * a full table) and make sure it actually matches.
1189 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1191 if (register_line != NULL) {
1192 if (work_line_->CompareLine(register_line) != 0) {
1193 Dump(std::cout);
1194 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001196 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1197 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001198 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 }
jeffhaobdb76512011-09-07 11:43:16 -07001200 }
1201#endif
1202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001205 prepend += " failed to verify: ";
1206 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 return false;
1208 }
jeffhaobdb76512011-09-07 11:43:16 -07001209 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 insn_flags_[insn_idx].SetVisited();
1211 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213
Ian Rogers1c849e52012-06-28 14:00:33 -07001214 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001215 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001216 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001217 * (besides the wasted space), but it indicates a flaw somewhere
1218 * down the line, possibly in the verifier.
1219 *
1220 * If we've substituted "always throw" instructions into the stream,
1221 * we are almost certainly going to have some dead code.
1222 */
1223 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 uint32_t insn_idx = 0;
1225 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001226 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001227 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001228 * may or may not be preceded by a padding NOP (for alignment).
1229 */
1230 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1231 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1232 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001233 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001234 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1235 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1236 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001238 }
1239
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001241 if (dead_start < 0)
1242 dead_start = insn_idx;
1243 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001244 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001245 dead_start = -1;
1246 }
1247 }
1248 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001249 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001250 }
1251 }
jeffhaobdb76512011-09-07 11:43:16 -07001252 return true;
1253}
1254
Ian Rogers776ac1f2012-04-13 23:36:36 -07001255bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001256 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1257 // We want the state _before_ the instruction, for the case where the dex pc we're
1258 // interested in is itself a monitor-enter instruction (which is a likely place
1259 // for a thread to be suspended).
1260 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001261 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001262 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1263 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1264 }
1265 }
1266
jeffhaobdb76512011-09-07 11:43:16 -07001267 /*
1268 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001269 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001270 * control to another statement:
1271 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001272 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001273 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001274 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001275 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001276 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001277 * throw an exception that is handled by an encompassing "try"
1278 * block.
1279 *
1280 * We can also return, in which case there is no successor instruction
1281 * from this point.
1282 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001283 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001284 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1286 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001287 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001288 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001289
jeffhaobdb76512011-09-07 11:43:16 -07001290 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001291 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001292 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001294 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1295 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 }
jeffhaobdb76512011-09-07 11:43:16 -07001297
1298 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001299 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001300 * can throw an exception, we will copy/merge this into the "catch"
1301 * address rather than work_line, because we don't want the result
1302 * from the "successful" code path (e.g. a check-cast that "improves"
1303 * a type) to be visible to the exception handler.
1304 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001305 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001307 } else {
1308#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001310#endif
1311 }
1312
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001313 // We need to ensure the work line is consistent while performing validation. When we spot a
1314 // peephole pattern we compute a new line for either the fallthrough instruction or the
1315 // branch target.
1316 UniquePtr<RegisterLine> branch_line;
1317 UniquePtr<RegisterLine> fallthrough_line;
1318
Elliott Hughesadb8c672012-03-06 16:49:32 -08001319 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001320 case Instruction::NOP:
1321 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001322 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001323 * a signature that looks like a NOP; if we see one of these in
1324 * the course of executing code then we have a problem.
1325 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001328 }
1329 break;
1330
1331 case Instruction::MOVE:
1332 case Instruction::MOVE_FROM16:
1333 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001335 break;
1336 case Instruction::MOVE_WIDE:
1337 case Instruction::MOVE_WIDE_FROM16:
1338 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001339 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001340 break;
1341 case Instruction::MOVE_OBJECT:
1342 case Instruction::MOVE_OBJECT_FROM16:
1343 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001344 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001345 break;
1346
1347 /*
1348 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001349 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001350 * might want to hold the result in an actual CPU register, so the
1351 * Dalvik spec requires that these only appear immediately after an
1352 * invoke or filled-new-array.
1353 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001354 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001355 * redundant with the reset done below, but it can make the debug info
1356 * easier to read in some cases.)
1357 */
1358 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001360 break;
1361 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001363 break;
1364 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001365 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001366 break;
1367
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001369 /*
jeffhao60f83e32012-02-13 17:16:30 -08001370 * This statement can only appear as the first instruction in an exception handler. We verify
1371 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001372 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001373 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001375 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 }
jeffhaobdb76512011-09-07 11:43:16 -07001377 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001378 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1379 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 }
jeffhaobdb76512011-09-07 11:43:16 -07001382 }
1383 break;
1384 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001385 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001386 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 const RegType& return_type = GetMethodReturnType();
1388 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 } else {
1391 // Compilers may generate synthetic functions that write byte values into boolean fields.
1392 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001393 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1395 ((return_type.IsBoolean() || return_type.IsByte() ||
1396 return_type.IsShort() || return_type.IsChar()) &&
1397 src_type.IsInteger()));
1398 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001399 bool success =
1400 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1401 if (!success) {
1402 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 }
jeffhaobdb76512011-09-07 11:43:16 -07001404 }
1405 }
1406 break;
1407 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001408 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001409 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 const RegType& return_type = GetMethodReturnType();
1411 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 } else {
1414 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001415 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1416 if (!success) {
1417 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 }
jeffhaobdb76512011-09-07 11:43:16 -07001419 }
1420 }
1421 break;
1422 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001423 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 const RegType& return_type = GetMethodReturnType();
1425 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 } else {
1428 /* return_type is the *expected* return type, not register value */
1429 DCHECK(!return_type.IsZero());
1430 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001431 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001432 // Disallow returning uninitialized values and verify that the reference in vAA is an
1433 // instance of the "return_type"
1434 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001435 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001436 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001437 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1438 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001439 }
1440 }
1441 }
1442 break;
1443
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001444 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001445 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001446 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001448 break;
jeffhaobdb76512011-09-07 11:43:16 -07001449 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001450 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001452 break;
jeffhaobdb76512011-09-07 11:43:16 -07001453 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001454 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001455 break;
1456 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001457 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001459 break;
jeffhaobdb76512011-09-07 11:43:16 -07001460 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 case Instruction::CONST_WIDE_16: {
1462 int64_t val = static_cast<int16_t>(dec_insn.vB);
1463 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1464 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1465 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001466 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001467 }
1468 case Instruction::CONST_WIDE_32: {
1469 int64_t val = static_cast<int32_t>(dec_insn.vB);
1470 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1471 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1472 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1473 break;
1474 }
1475 case Instruction::CONST_WIDE: {
1476 int64_t val = dec_insn.vB_wide;
1477 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1478 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1479 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1480 break;
1481 }
1482 case Instruction::CONST_WIDE_HIGH16: {
1483 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1484 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1485 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1486 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1487 break;
1488 }
jeffhaobdb76512011-09-07 11:43:16 -07001489 case Instruction::CONST_STRING:
1490 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001491 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001492 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001494 // Get type from instruction if unresolved then we need an access check
1495 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001496 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001498 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001499 res_type.IsConflict() ? res_type
1500 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 }
jeffhaobdb76512011-09-07 11:43:16 -07001503 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001504 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001505 break;
1506 case Instruction::MONITOR_EXIT:
1507 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001508 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001509 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001510 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001511 * to the need to handle asynchronous exceptions, a now-deprecated
1512 * feature that Dalvik doesn't support.)
1513 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001514 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001515 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001516 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001517 * structured locking checks are working, the former would have
1518 * failed on the -enter instruction, and the latter is impossible.
1519 *
1520 * This is fortunate, because issue 3221411 prevents us from
1521 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001522 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001523 * some catch blocks (which will show up as "dead" code when
1524 * we skip them here); if we can't, then the code path could be
1525 * "live" so we still need to check it.
1526 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001527 opcode_flags &= ~Instruction::kThrow;
1528 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001529 break;
1530
Ian Rogers28ad40d2011-10-27 15:19:26 -07001531 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001533 /*
1534 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1535 * could be a "upcast" -- not expected, so we don't try to address it.)
1536 *
1537 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001539 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001540 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001541 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001542 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001543 if (res_type.IsConflict()) {
1544 DCHECK_NE(failures_.size(), 0U);
1545 if (!is_checkcast) {
1546 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1547 }
1548 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001549 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001550 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1551 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001553 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001555 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001557 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001558 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001559 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001561 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001562 }
jeffhaobdb76512011-09-07 11:43:16 -07001563 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001564 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 }
1566 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001568 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001569 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001572 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 }
1574 }
1575 break;
1576 }
1577 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001578 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001579 if (res_type.IsConflict()) {
1580 DCHECK_NE(failures_.size(), 0U);
1581 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001582 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001583 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1584 // can't create an instance of an interface or abstract class */
1585 if (!res_type.IsInstantiableTypes()) {
1586 Fail(VERIFY_ERROR_INSTANTIATION)
1587 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001588 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001590 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1591 // Any registers holding previous allocations from this address that have not yet been
1592 // initialized must be marked invalid.
1593 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1594 // add the new uninitialized reference to the register state
1595 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 break;
1597 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001598 case Instruction::NEW_ARRAY:
1599 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001600 break;
1601 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001602 VerifyNewArray(dec_insn, true, false);
1603 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001604 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001605 case Instruction::FILLED_NEW_ARRAY_RANGE:
1606 VerifyNewArray(dec_insn, true, true);
1607 just_set_result = true; // Filled new array range sets result register
1608 break;
jeffhaobdb76512011-09-07 11:43:16 -07001609 case Instruction::CMPL_FLOAT:
1610 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001612 break;
1613 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001615 break;
1616 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001617 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001618 break;
1619 case Instruction::CMPL_DOUBLE:
1620 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1622 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001623 break;
1624 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1626 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001627 break;
1628 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001629 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1633 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001634 break;
1635 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1637 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001638 break;
1639 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001641 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001644 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001645 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001646 }
1647 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 }
jeffhaobdb76512011-09-07 11:43:16 -07001649 case Instruction::GOTO:
1650 case Instruction::GOTO_16:
1651 case Instruction::GOTO_32:
1652 /* no effect on or use of registers */
1653 break;
1654
1655 case Instruction::PACKED_SWITCH:
1656 case Instruction::SPARSE_SWITCH:
1657 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001658 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
1660
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 case Instruction::FILL_ARRAY_DATA: {
1662 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001664 /* array_type can be null if the reg type is Zero */
1665 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001666 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001668 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001669 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1670 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001671 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1673 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 } else {
jeffhao457cc512012-02-02 16:55:13 -08001675 // Now verify if the element width in the table matches the element width declared in
1676 // the array
1677 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1678 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001680 } else {
1681 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1682 // Since we don't compress the data in Dex, expect to see equal width of data stored
1683 // in the table and expected from the array class.
1684 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1686 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001687 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 }
1689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 }
1691 }
1692 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1697 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 bool mismatch = false;
1699 if (reg_type1.IsZero()) { // zero then integral or reference expected
1700 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1701 } else if (reg_type1.IsReferenceTypes()) { // both references?
1702 mismatch = !reg_type2.IsReferenceTypes();
1703 } else { // both integral?
1704 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1705 }
1706 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1708 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001709 }
1710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::IF_LT:
1713 case Instruction::IF_GE:
1714 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1717 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1720 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001721 }
1722 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 }
jeffhaobdb76512011-09-07 11:43:16 -07001724 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001730
1731 // Find previous instruction - its existence is a precondition to peephole optimization.
1732 uint32_t prev_idx = 0;
1733 if (0 != work_insn_idx_) {
1734 prev_idx = work_insn_idx_ - 1;
1735 while(0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1736 prev_idx--;
1737 }
1738 CHECK(insn_flags_[prev_idx].IsOpcode());
1739 } else {
1740 break;
1741 }
1742
1743 const Instruction* prev_inst = Instruction::At(code_item_->insns_+prev_idx);
1744
1745 /* Check for peep-hole pattern of:
1746 * ...;
1747 * instance-of vX, vO, T;
1748 * ifXXX vX, b ;
1749 * ...;
1750 * b: INST;
1751 * ...;
1752 * and sharpen the type for either the fall-through or the branch case.
1753 */
1754 if (!CurrentInsnFlags()->IsBranchTarget()) {
1755 DecodedInstruction prev_dec_insn(prev_inst);
1756 if ((Instruction::INSTANCE_OF == prev_inst->Opcode())
1757 && (dec_insn.vA == prev_dec_insn.vA)) {
1758 // Check that the we are not attempting conversion to interface types,
1759 // which is not done because of the multiple inheritance implications.
1760 const RegType& cast_type =
1761 ResolveClassAndCheckAccess(prev_dec_insn.vC);
1762
1763 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
1764 if (dec_insn.opcode == Instruction::IF_EQZ) {
1765 fallthrough_line.reset(new RegisterLine(code_item_->registers_size_, this));
1766 fallthrough_line->CopyFromLine(work_line_.get());
1767 fallthrough_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1768 } else {
1769 branch_line.reset(new RegisterLine(code_item_->registers_size_, this));
1770 branch_line->CopyFromLine(work_line_.get());
1771 branch_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1772 }
1773 }
1774 }
1775 }
1776
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 case Instruction::IF_LTZ:
1780 case Instruction::IF_GEZ:
1781 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001783 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1786 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 }
jeffhaobdb76512011-09-07 11:43:16 -07001790 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1792 break;
jeffhaobdb76512011-09-07 11:43:16 -07001793 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1795 break;
jeffhaobdb76512011-09-07 11:43:16 -07001796 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 VerifyAGet(dec_insn, reg_types_.Char(), true);
1798 break;
jeffhaobdb76512011-09-07 11:43:16 -07001799 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001801 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 case Instruction::AGET:
1803 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1804 break;
jeffhaobdb76512011-09-07 11:43:16 -07001805 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001806 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
1808 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001809 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
1811
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 case Instruction::APUT_BOOLEAN:
1813 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1814 break;
1815 case Instruction::APUT_BYTE:
1816 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1817 break;
1818 case Instruction::APUT_CHAR:
1819 VerifyAPut(dec_insn, reg_types_.Char(), true);
1820 break;
1821 case Instruction::APUT_SHORT:
1822 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001823 break;
1824 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
1827 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001829 break;
1830 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001831 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001832 break;
1833
jeffhaobdb76512011-09-07 11:43:16 -07001834 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001835 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 break;
jeffhaobdb76512011-09-07 11:43:16 -07001837 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001838 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 break;
jeffhaobdb76512011-09-07 11:43:16 -07001840 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001841 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 break;
jeffhaobdb76512011-09-07 11:43:16 -07001843 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001844 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 break;
1846 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001847 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001851 break;
1852 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001853 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 break;
jeffhaobdb76512011-09-07 11:43:16 -07001855
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001857 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 break;
1859 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001860 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 break;
1862 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001863 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 break;
1865 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001866 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001867 break;
1868 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001869 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
1871 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001872 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 break;
jeffhaobdb76512011-09-07 11:43:16 -07001874 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001875 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001876 break;
1877
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001879 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001882 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001885 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 break;
jeffhaobdb76512011-09-07 11:43:16 -07001887 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001888 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 break;
1890 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001891 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
1896 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001897 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 break;
1899
1900 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001901 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 break;
1903 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001904 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 break;
1906 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001907 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 break;
1909 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001910 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001913 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001914 break;
1915 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001919 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
1921
1922 case Instruction::INVOKE_VIRTUAL:
1923 case Instruction::INVOKE_VIRTUAL_RANGE:
1924 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001926 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1927 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1928 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1929 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001930 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1931 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001932 const char* descriptor;
1933 if (called_method == NULL) {
1934 uint32_t method_idx = dec_insn.vB;
1935 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1936 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1937 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1938 } else {
1939 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
Ian Rogersb4903572012-10-11 11:52:56 -07001941 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001942 if (!return_type.IsLowHalf()) {
1943 work_line_->SetResultRegisterType(return_type);
1944 } else {
1945 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001947 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001952 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001953 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1954 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001955 const char* return_type_descriptor;
1956 bool is_constructor;
1957 if (called_method == NULL) {
1958 uint32_t method_idx = dec_insn.vB;
1959 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1960 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1961 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1962 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1963 } else {
1964 is_constructor = called_method->IsConstructor();
1965 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1966 }
1967 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001968 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 * Some additional checks when calling a constructor. We know from the invocation arg check
1970 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1971 * that to require that called_method->klass is the same as this->klass or this->super,
1972 * allowing the latter only if the "this" argument is the same as the "this" argument to
1973 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001974 */
jeffhaob57e9522012-04-26 18:08:21 -07001975 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1976 if (this_type.IsConflict()) // failure.
1977 break;
jeffhaobdb76512011-09-07 11:43:16 -07001978
jeffhaob57e9522012-04-26 18:08:21 -07001979 /* no null refs allowed (?) */
1980 if (this_type.IsZero()) {
1981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1982 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001983 }
jeffhaob57e9522012-04-26 18:08:21 -07001984
1985 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001986 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1987 // TODO: re-enable constructor type verification
1988 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001989 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001990 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1991 // break;
1992 // }
jeffhaob57e9522012-04-26 18:08:21 -07001993
1994 /* arg must be an uninitialized reference */
1995 if (!this_type.IsUninitializedTypes()) {
1996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1997 << this_type;
1998 break;
1999 }
2000
2001 /*
2002 * Replace the uninitialized reference with an initialized one. We need to do this for all
2003 * registers that have the same object instance in them, not just the "this" register.
2004 */
2005 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002006 }
Ian Rogersb4903572012-10-11 11:52:56 -07002007 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2008 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002009 if (!return_type.IsLowHalf()) {
2010 work_line_->SetResultRegisterType(return_type);
2011 } else {
2012 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2013 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002014 just_set_result = true;
2015 break;
2016 }
2017 case Instruction::INVOKE_STATIC:
2018 case Instruction::INVOKE_STATIC_RANGE: {
2019 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002020 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002021 const char* descriptor;
2022 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002023 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002024 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2025 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002026 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002027 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002028 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002029 }
Ian Rogersb4903572012-10-11 11:52:56 -07002030 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002031 if (!return_type.IsLowHalf()) {
2032 work_line_->SetResultRegisterType(return_type);
2033 } else {
2034 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2035 }
jeffhaobdb76512011-09-07 11:43:16 -07002036 just_set_result = true;
2037 }
2038 break;
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002041 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002042 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002043 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002044 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002045 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2046 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2047 << PrettyMethod(abs_method) << "'";
2048 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002049 }
Ian Rogers0d604842012-04-16 14:50:24 -07002050 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002051 /* Get the type of the "this" arg, which should either be a sub-interface of called
2052 * interface or Object (see comments in RegType::JoinClass).
2053 */
2054 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2055 if (this_type.IsZero()) {
2056 /* null pointer always passes (and always fails at runtime) */
2057 } else {
2058 if (this_type.IsUninitializedTypes()) {
2059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2060 << this_type;
2061 break;
2062 }
2063 // In the past we have tried to assert that "called_interface" is assignable
2064 // from "this_type.GetClass()", however, as we do an imprecise Join
2065 // (RegType::JoinClass) we don't have full information on what interfaces are
2066 // implemented by "this_type". For example, two classes may implement the same
2067 // interfaces and have a common parent that doesn't implement the interface. The
2068 // join will set "this_type" to the parent class and a test that this implements
2069 // the interface will incorrectly fail.
2070 }
2071 /*
2072 * We don't have an object instance, so we can't find the concrete method. However, all of
2073 * the type information is in the abstract method, so we're good.
2074 */
2075 const char* descriptor;
2076 if (abs_method == NULL) {
2077 uint32_t method_idx = dec_insn.vB;
2078 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2079 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2080 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2081 } else {
2082 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2083 }
Ian Rogersb4903572012-10-11 11:52:56 -07002084 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 if (!return_type.IsLowHalf()) {
2086 work_line_->SetResultRegisterType(return_type);
2087 } else {
2088 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2089 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002090 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::NEG_INT:
2094 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
2097 case Instruction::NEG_LONG:
2098 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002099 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2100 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2107 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002110 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2111 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002117 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2118 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2122 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2126 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002129 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2130 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002136 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2137 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002140 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2141 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2145 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002148 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2149 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2153 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164
2165 case Instruction::ADD_INT:
2166 case Instruction::SUB_INT:
2167 case Instruction::MUL_INT:
2168 case Instruction::REM_INT:
2169 case Instruction::DIV_INT:
2170 case Instruction::SHL_INT:
2171 case Instruction::SHR_INT:
2172 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002173 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2174 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::AND_INT:
2177 case Instruction::OR_INT:
2178 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2180 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182 case Instruction::ADD_LONG:
2183 case Instruction::SUB_LONG:
2184 case Instruction::MUL_LONG:
2185 case Instruction::DIV_LONG:
2186 case Instruction::REM_LONG:
2187 case Instruction::AND_LONG:
2188 case Instruction::OR_LONG:
2189 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2191 reg_types_.LongLo(), reg_types_.LongHi(),
2192 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::SHL_LONG:
2195 case Instruction::SHR_LONG:
2196 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002198 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2199 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002200 break;
2201 case Instruction::ADD_FLOAT:
2202 case Instruction::SUB_FLOAT:
2203 case Instruction::MUL_FLOAT:
2204 case Instruction::DIV_FLOAT:
2205 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208 case Instruction::ADD_DOUBLE:
2209 case Instruction::SUB_DOUBLE:
2210 case Instruction::MUL_DOUBLE:
2211 case Instruction::DIV_DOUBLE:
2212 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002213 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2214 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2215 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::ADD_INT_2ADDR:
2218 case Instruction::SUB_INT_2ADDR:
2219 case Instruction::MUL_INT_2ADDR:
2220 case Instruction::REM_INT_2ADDR:
2221 case Instruction::SHL_INT_2ADDR:
2222 case Instruction::SHR_INT_2ADDR:
2223 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::AND_INT_2ADDR:
2227 case Instruction::OR_INT_2ADDR:
2228 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::ADD_LONG_2ADDR:
2235 case Instruction::SUB_LONG_2ADDR:
2236 case Instruction::MUL_LONG_2ADDR:
2237 case Instruction::DIV_LONG_2ADDR:
2238 case Instruction::REM_LONG_2ADDR:
2239 case Instruction::AND_LONG_2ADDR:
2240 case Instruction::OR_LONG_2ADDR:
2241 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2243 reg_types_.LongLo(), reg_types_.LongHi(),
2244 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002245 break;
2246 case Instruction::SHL_LONG_2ADDR:
2247 case Instruction::SHR_LONG_2ADDR:
2248 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2250 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::ADD_FLOAT_2ADDR:
2253 case Instruction::SUB_FLOAT_2ADDR:
2254 case Instruction::MUL_FLOAT_2ADDR:
2255 case Instruction::DIV_FLOAT_2ADDR:
2256 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259 case Instruction::ADD_DOUBLE_2ADDR:
2260 case Instruction::SUB_DOUBLE_2ADDR:
2261 case Instruction::MUL_DOUBLE_2ADDR:
2262 case Instruction::DIV_DOUBLE_2ADDR:
2263 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002264 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2265 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2266 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
2268 case Instruction::ADD_INT_LIT16:
2269 case Instruction::RSUB_INT:
2270 case Instruction::MUL_INT_LIT16:
2271 case Instruction::DIV_INT_LIT16:
2272 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
2275 case Instruction::AND_INT_LIT16:
2276 case Instruction::OR_INT_LIT16:
2277 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::ADD_INT_LIT8:
2281 case Instruction::RSUB_INT_LIT8:
2282 case Instruction::MUL_INT_LIT8:
2283 case Instruction::DIV_INT_LIT8:
2284 case Instruction::REM_INT_LIT8:
2285 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002286 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::AND_INT_LIT8:
2291 case Instruction::OR_INT_LIT8:
2292 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002297 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002298 case Instruction::UNUSED_EE:
2299 case Instruction::UNUSED_EF:
2300 case Instruction::UNUSED_F2:
2301 case Instruction::UNUSED_F3:
2302 case Instruction::UNUSED_F4:
2303 case Instruction::UNUSED_F5:
2304 case Instruction::UNUSED_F6:
2305 case Instruction::UNUSED_F7:
2306 case Instruction::UNUSED_F8:
2307 case Instruction::UNUSED_F9:
2308 case Instruction::UNUSED_FA:
2309 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002310 case Instruction::UNUSED_F0:
2311 case Instruction::UNUSED_F1:
2312 case Instruction::UNUSED_E3:
2313 case Instruction::UNUSED_E8:
2314 case Instruction::UNUSED_E7:
2315 case Instruction::UNUSED_E4:
2316 case Instruction::UNUSED_E9:
2317 case Instruction::UNUSED_FC:
2318 case Instruction::UNUSED_E5:
2319 case Instruction::UNUSED_EA:
2320 case Instruction::UNUSED_FD:
2321 case Instruction::UNUSED_E6:
2322 case Instruction::UNUSED_EB:
2323 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002324 case Instruction::UNUSED_3E:
2325 case Instruction::UNUSED_3F:
2326 case Instruction::UNUSED_40:
2327 case Instruction::UNUSED_41:
2328 case Instruction::UNUSED_42:
2329 case Instruction::UNUSED_43:
2330 case Instruction::UNUSED_73:
2331 case Instruction::UNUSED_79:
2332 case Instruction::UNUSED_7A:
2333 case Instruction::UNUSED_EC:
2334 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337
2338 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002339 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002340 * complain if an instruction is missing (which is desirable).
2341 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002343
Ian Rogersad0b3a32012-04-16 14:50:24 -07002344 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002346 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002347 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002348 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002349 /* immediate failure, reject class */
2350 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2351 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002352 } else if (have_pending_runtime_throw_failure_) {
2353 /* slow path will throw, mark following code as unreachable */
2354 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002355 }
jeffhaobdb76512011-09-07 11:43:16 -07002356 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 * If we didn't just set the result register, clear it out. This ensures that you can only use
2358 * "move-result" immediately after the result is set. (We could check this statically, but it's
2359 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002360 */
2361 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002363 }
2364
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002365
jeffhaobdb76512011-09-07 11:43:16 -07002366
2367 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002368 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002369 *
2370 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002371 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002372 * somebody could get a reference field, check it for zero, and if the
2373 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002374 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002375 * that, and will reject the code.
2376 *
2377 * TODO: avoid re-fetching the branch target
2378 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002379 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002380 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002382 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002384 return false;
2385 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002386 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002387 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002388 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 }
jeffhaobdb76512011-09-07 11:43:16 -07002390 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002391 if (NULL != branch_line.get()) {
2392 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2393 return false;
2394 }
2395 } else {
2396 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2397 return false;
2398 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 }
jeffhaobdb76512011-09-07 11:43:16 -07002400 }
2401
2402 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002403 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002404 *
2405 * We've already verified that the table is structurally sound, so we
2406 * just need to walk through and tag the targets.
2407 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002408 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002409 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2410 const uint16_t* switch_insns = insns + offset_to_switch;
2411 int switch_count = switch_insns[1];
2412 int offset_to_targets, targ;
2413
2414 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2415 /* 0 = sig, 1 = count, 2/3 = first key */
2416 offset_to_targets = 4;
2417 } else {
2418 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002419 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002420 offset_to_targets = 2 + 2 * switch_count;
2421 }
2422
2423 /* verify each switch target */
2424 for (targ = 0; targ < switch_count; targ++) {
2425 int offset;
2426 uint32_t abs_offset;
2427
2428 /* offsets are 32-bit, and only partly endian-swapped */
2429 offset = switch_insns[offset_to_targets + targ * 2] |
2430 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 abs_offset = work_insn_idx_ + offset;
2432 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002433 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002434 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 }
2436 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002437 return false;
2438 }
2439 }
2440
2441 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2443 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002444 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002445 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002447 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002448
Ian Rogers0571d352011-11-03 19:51:38 -07002449 for (; iterator.HasNext(); iterator.Next()) {
2450 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 within_catch_all = true;
2452 }
jeffhaobdb76512011-09-07 11:43:16 -07002453 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2455 * "work_regs", because at runtime the exception will be thrown before the instruction
2456 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002457 */
Ian Rogers0571d352011-11-03 19:51:38 -07002458 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002459 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 }
jeffhaobdb76512011-09-07 11:43:16 -07002461 }
2462
2463 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2465 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002466 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002468 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 * The state in work_line reflects the post-execution state. If the current instruction is a
2470 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002471 * it will do so before grabbing the lock).
2472 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002473 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002474 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002476 return false;
2477 }
2478 }
2479 }
2480
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002481 /* Handle "continue". Tag the next consecutive instruction.
2482 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2483 * because it changes work_line_ when performing peephole optimization
2484 * and this change should not be used in those cases.
2485 */
2486 if ((opcode_flags & Instruction::kContinue) != 0) {
2487 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2488 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2490 return false;
2491 }
2492 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2493 // next instruction isn't one.
2494 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2495 return false;
2496 }
2497 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2498 if (next_line != NULL) {
2499 if (NULL != fallthrough_line.get()) {
2500 // Make workline consistent with fallthrough computed from peephole optimization.
2501 work_line_->CopyFromLine(fallthrough_line.get());
2502 }
2503 // Merge registers into what we have for the next instruction,
2504 // and set the "changed" flag if needed.
2505 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2506 return false;
2507 }
2508 } else {
2509 /*
2510 * We're not recording register data for the next instruction, so we don't know what the
2511 * prior state was. We have to assume that something has changed and re-evaluate it.
2512 */
2513 insn_flags_[next_insn_idx].SetChanged();
2514 }
2515 }
2516
jeffhaod1f0fde2011-09-08 17:25:33 -07002517 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002518 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002519 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 return false;
2521 }
jeffhaobdb76512011-09-07 11:43:16 -07002522 }
2523
2524 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002525 * Update start_guess. Advance to the next instruction of that's
2526 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002527 * neither of those exists we're in a return or throw; leave start_guess
2528 * alone and let the caller sort it out.
2529 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002530 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002532 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002533 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002535 }
2536
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2538 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002539
2540 return true;
2541}
2542
Ian Rogers776ac1f2012-04-13 23:36:36 -07002543const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002544 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002545 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002546 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002547 const RegType& result =
Ian Rogers637c65b2013-05-31 11:46:00 -07002548 klass != NULL ? reg_types_.FromClass(descriptor, klass, klass->IsFinal())
Ian Rogersb4903572012-10-11 11:52:56 -07002549 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 if (result.IsConflict()) {
2551 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2552 << "' in " << referrer;
2553 return result;
2554 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002555 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002556 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002557 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002558 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002559 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002560 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002561 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002562 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002563 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002564 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002565}
2566
Ian Rogers776ac1f2012-04-13 23:36:36 -07002567const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002568 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002570 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2572 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002573 CatchHandlerIterator iterator(handlers_ptr);
2574 for (; iterator.HasNext(); iterator.Next()) {
2575 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2576 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002577 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002579 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002580 if (common_super == NULL) {
2581 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2582 // as that is caught at runtime
2583 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002584 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002585 // We don't know enough about the type and the common path merge will result in
2586 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002587 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002588 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002589 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002590 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002592 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002593 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 }
2595 }
2596 }
2597 }
Ian Rogers0571d352011-11-03 19:51:38 -07002598 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 }
2600 }
2601 if (common_super == NULL) {
2602 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002603 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002604 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002606 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002607}
2608
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002609mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2610 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002611 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002612 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002613 if (klass_type.IsConflict()) {
2614 std::string append(" in attempt to access method ");
2615 append += dex_file_->GetMethodName(method_id);
2616 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002617 return NULL;
2618 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002619 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002620 return NULL; // Can't resolve Class so no more to do here
2621 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002622 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002623 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002624 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002626 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002627 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002628
2629 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002631 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 res_method = klass->FindInterfaceMethod(name, signature);
2633 } else {
2634 res_method = klass->FindVirtualMethod(name, signature);
2635 }
2636 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002637 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002639 // If a virtual or interface method wasn't found with the expected type, look in
2640 // the direct methods. This can happen when the wrong invoke type is used or when
2641 // a class has changed, and will be flagged as an error in later checks.
2642 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2643 res_method = klass->FindDirectMethod(name, signature);
2644 }
2645 if (res_method == NULL) {
2646 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2647 << PrettyDescriptor(klass) << "." << name
2648 << " " << signature;
2649 return NULL;
2650 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 }
2652 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2654 // enforce them here.
2655 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2657 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 return NULL;
2659 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002660 // Disallow any calls to class initializers.
2661 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2663 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002664 return NULL;
2665 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002666 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002667 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002668 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002669 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002670 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002671 }
jeffhaode0d9c92012-02-27 13:58:13 -08002672 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2673 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2675 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002676 return NULL;
2677 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002678 // Check that interface methods match interface classes.
2679 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2680 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2681 << " is in an interface class " << PrettyClass(klass);
2682 return NULL;
2683 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2684 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2685 << " is in a non-interface class " << PrettyClass(klass);
2686 return NULL;
2687 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 // See if the method type implied by the invoke instruction matches the access flags for the
2689 // target method.
2690 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2691 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2692 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2693 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002694 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2695 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 return NULL;
2697 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002698 return res_method;
2699}
2700
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002701mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2702 MethodType method_type, bool is_range,
2703 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002704 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2705 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002706 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002707 if (res_method == NULL) { // error or class is unresolved
2708 return NULL;
2709 }
2710
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2712 // has a vtable entry for the target method.
2713 if (is_super) {
2714 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002715 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002716 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002717 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002718 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002719 << " to super " << PrettyMethod(res_method);
2720 return NULL;
2721 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002722 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002723 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002724 MethodHelper mh(res_method);
2725 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002727 << " to super " << super
2728 << "." << mh.GetName()
2729 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 return NULL;
2731 }
2732 }
2733 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2734 // match the call to the signature. Also, we might might be calling through an abstract method
2735 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002736 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 /* caught by static verifier */
2738 DCHECK(is_range || expected_args <= 5);
2739 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2742 return NULL;
2743 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002744
jeffhaobdb76512011-09-07 11:43:16 -07002745 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002746 * Check the "this" argument, which must be an instance of the class that declared the method.
2747 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2748 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002749 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002750 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 if (!res_method->IsStatic()) {
2752 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002753 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 return NULL;
2755 }
2756 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 return NULL;
2759 }
2760 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002761 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07002762 const RegType& res_method_class = reg_types_.FromClass(ClassHelper(klass).GetDescriptor(),
2763 klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002764 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002765 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002766 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 return NULL;
2768 }
2769 }
2770 actual_args++;
2771 }
2772 /*
2773 * Process the target method's signature. This signature may or may not
2774 * have been verified, so we can't assume it's properly formed.
2775 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002776 MethodHelper mh(res_method);
2777 const DexFile::TypeList* params = mh.GetParameterTypeList();
2778 size_t params_size = params == NULL ? 0 : params->Size();
2779 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002782 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2783 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 return NULL;
2785 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002786 const char* descriptor =
2787 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2788 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002790 << " missing signature component";
2791 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
Ian Rogersb4903572012-10-11 11:52:56 -07002793 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002794 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002795 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002796 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 }
2798 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2799 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002802 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 return NULL;
2804 } else {
2805 return res_method;
2806 }
2807}
2808
Ian Rogers776ac1f2012-04-13 23:36:36 -07002809void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002810 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002811 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 if (res_type.IsConflict()) { // bad class
2813 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002814 } else {
2815 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2816 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002818 } else if (!is_filled) {
2819 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002820 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002821 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002822 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002823 } else {
2824 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2825 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002827 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002828 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002829 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002830 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002832 return;
2833 }
2834 }
2835 // filled-array result goes into "result" register
2836 work_line_->SetResultRegisterType(res_type);
2837 }
2838 }
2839}
2840
Ian Rogers776ac1f2012-04-13 23:36:36 -07002841void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002842 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002843 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002847 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002848 if (array_type.IsZero()) {
2849 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2850 // instruction type. TODO: have a proper notion of bottom here.
2851 if (!is_primitive || insn_type.IsCategory1Types()) {
2852 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002853 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002855 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002856 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2857 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002858 }
jeffhaofc3144e2012-02-01 17:21:15 -08002859 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002861 } else {
2862 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002864 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002866 << " source for aget-object";
2867 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002869 << " source for category 1 aget";
2870 } else if (is_primitive && !insn_type.Equals(component_type) &&
2871 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002872 (insn_type.IsLong() && component_type.IsDouble()))) {
2873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2874 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002875 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 // Use knowledge of the field type which is stronger than the type inferred from the
2877 // instruction, which can't differentiate object types and ints from floats, longs from
2878 // doubles.
2879 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002880 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002881 } else {
2882 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2883 component_type.HighHalf(&reg_types_));
2884 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 }
2886 }
2887 }
2888}
2889
Ian Rogers776ac1f2012-04-13 23:36:36 -07002890void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002892 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002896 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002897 if (array_type.IsZero()) {
2898 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2899 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002900 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002902 } else {
2903 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002905 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002907 << " source for aput-object";
2908 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002910 << " source for category 1 aput";
2911 } else if (is_primitive && !insn_type.Equals(component_type) &&
2912 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2913 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002915 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002917 // The instruction agrees with the type of array, confirm the value to be stored does too
2918 // Note: we use the instruction type (rather than the component type) for aput-object as
2919 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002920 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 }
2922 }
2923 }
2924}
2925
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002926mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002927 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2928 // Check access to class
2929 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 if (klass_type.IsConflict()) { // bad class
2931 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2932 field_idx, dex_file_->GetFieldName(field_id),
2933 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002934 return NULL;
2935 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002936 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002937 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002938 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002939 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07002942 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07002943 << dex_file_->GetFieldName(field_id) << ") in "
2944 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 DCHECK(Thread::Current()->IsExceptionPending());
2946 Thread::Current()->ClearException();
2947 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002948 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2949 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 return NULL;
2953 } else if (!field->IsStatic()) {
2954 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2955 return NULL;
2956 } else {
2957 return field;
2958 }
2959}
2960
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002961mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002962 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2963 // Check access to class
2964 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 if (klass_type.IsConflict()) {
2966 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2967 field_idx, dex_file_->GetFieldName(field_id),
2968 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002969 return NULL;
2970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002972 return NULL; // Can't resolve Class so no more to do here
2973 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002974 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002975 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07002977 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07002978 << dex_file_->GetFieldName(field_id) << ") in "
2979 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 DCHECK(Thread::Current()->IsExceptionPending());
2981 Thread::Current()->ClearException();
2982 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002983 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2984 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 return NULL;
2988 } else if (field->IsStatic()) {
2989 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2990 << " to not be static";
2991 return NULL;
2992 } else if (obj_type.IsZero()) {
2993 // Cannot infer and check type, however, access will cause null pointer exception
2994 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002995 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002996 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07002997 const RegType& field_klass =
2998 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
2999 klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 if (obj_type.IsUninitializedTypes() &&
3001 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3002 !field_klass.Equals(GetDeclaringClass()))) {
3003 // Field accesses through uninitialized references are only allowable for constructors where
3004 // the field is declared in this class
3005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3006 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003007 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003008 return NULL;
3009 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3010 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3011 // of C1. For resolution to occur the declared class of the field must be compatible with
3012 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3013 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3014 << " from object of type " << obj_type;
3015 return NULL;
3016 } else {
3017 return field;
3018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
3020}
3021
Ian Rogers776ac1f2012-04-13 23:36:36 -07003022void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003023 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003024 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003025 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003026 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003027 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003028 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003029 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003030 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003031 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003033 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003034 if (field != NULL) {
3035 descriptor = FieldHelper(field).GetTypeDescriptor();
3036 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003037 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3039 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3040 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003041 }
Ian Rogersb4903572012-10-11 11:52:56 -07003042 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 if (is_primitive) {
3044 if (field_type.Equals(insn_type) ||
3045 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3046 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3047 // expected that read is of the correct primitive type or that int reads are reading
3048 // floats or long reads are reading doubles
3049 } else {
3050 // This is a global failure rather than a class change failure as the instructions and
3051 // the descriptors for the type should have been consistent within the same file at
3052 // compile time
3053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3054 << " to be of type '" << insn_type
3055 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003056 return;
3057 }
3058 } else {
3059 if (!insn_type.IsAssignableFrom(field_type)) {
3060 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3061 << " to be compatible with type '" << insn_type
3062 << "' but found type '" << field_type
3063 << "' in get-object";
3064 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3065 return;
3066 }
3067 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 if (!field_type.IsLowHalf()) {
3069 work_line_->SetRegisterType(dec_insn.vA, field_type);
3070 } else {
3071 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3072 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003073}
3074
Ian Rogers776ac1f2012-04-13 23:36:36 -07003075void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003076 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003077 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003078 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003079 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003080 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003081 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003082 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003083 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003084 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003086 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003087 if (field != NULL) {
3088 descriptor = FieldHelper(field).GetTypeDescriptor();
3089 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003090 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3092 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3093 loader = class_loader_;
3094 }
Ian Rogersb4903572012-10-11 11:52:56 -07003095 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003096 if (field != NULL) {
3097 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3098 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3099 << " from other class " << GetDeclaringClass();
3100 return;
3101 }
3102 }
3103 if (is_primitive) {
3104 // Primitive field assignability rules are weaker than regular assignability rules
3105 bool instruction_compatible;
3106 bool value_compatible;
3107 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3108 if (field_type.IsIntegralTypes()) {
3109 instruction_compatible = insn_type.IsIntegralTypes();
3110 value_compatible = value_type.IsIntegralTypes();
3111 } else if (field_type.IsFloat()) {
3112 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3113 value_compatible = value_type.IsFloatTypes();
3114 } else if (field_type.IsLong()) {
3115 instruction_compatible = insn_type.IsLong();
3116 value_compatible = value_type.IsLongTypes();
3117 } else if (field_type.IsDouble()) {
3118 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3119 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003120 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003121 instruction_compatible = false; // reference field with primitive store
3122 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 if (!instruction_compatible) {
3125 // This is a global failure rather than a class change failure as the instructions and
3126 // the descriptors for the type should have been consistent within the same file at
3127 // compile time
3128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3129 << " to be of type '" << insn_type
3130 << "' but found type '" << field_type
3131 << "' in put";
3132 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003133 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003134 if (!value_compatible) {
3135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3136 << " of type " << value_type
3137 << " but expected " << field_type
3138 << " for store to " << PrettyField(field) << " in put";
3139 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003141 } else {
3142 if (!insn_type.IsAssignableFrom(field_type)) {
3143 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3144 << " to be compatible with type '" << insn_type
3145 << "' but found type '" << field_type
3146 << "' in put-object";
3147 return;
3148 }
3149 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 }
3151}
3152
Ian Rogers776ac1f2012-04-13 23:36:36 -07003153bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003156 return false;
3157 }
3158 return true;
3159}
3160
Ian Rogers776ac1f2012-04-13 23:36:36 -07003161bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 bool changed = true;
3163 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3164 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003165 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 * We haven't processed this instruction before, and we haven't touched the registers here, so
3167 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3168 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003169 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003171 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003172 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3173 if (gDebugVerify) {
3174 copy->CopyFromLine(target_line);
3175 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003177 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003179 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003180 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003181 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003182 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3183 << *copy.get() << " MERGE\n"
3184 << *merge_line << " ==\n"
3185 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003186 }
3187 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 if (changed) {
3189 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003190 }
3191 return true;
3192}
3193
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003194InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003195 return &insn_flags_[work_insn_idx_];
3196}
3197
Ian Rogersad0b3a32012-04-16 14:50:24 -07003198const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003199 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003200 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3201 uint16_t return_type_idx = proto_id.return_type_idx_;
3202 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003203 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003204}
3205
3206const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003207 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003208 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003209 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003210 if (mirror_method_ != NULL) {
3211 mirror::Class* klass = mirror_method_->GetDeclaringClass();
3212 declaring_class_ = &reg_types_.FromClass(descriptor, klass, klass->IsFinal());
3213 } else {
3214 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3215 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003216 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003217 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218}
3219
Ian Rogers776ac1f2012-04-13 23:36:36 -07003220void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 size_t* log2_max_gc_pc) {
3222 size_t local_gc_points = 0;
3223 size_t max_insn = 0;
3224 size_t max_ref_reg = -1;
3225 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003226 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 local_gc_points++;
3228 max_insn = i;
3229 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003230 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003231 }
3232 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 *gc_points = local_gc_points;
3234 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3235 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003236 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 i++;
3238 }
3239 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003240}
3241
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003242MethodVerifier::PcToConcreteMethod* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003243
3244 // It is risky to rely on reg_types for sharpening in cases of soft
3245 // verification, we might end up sharpening to a wrong implementation. Just abort.
3246 if (!failure_messages_.empty()) {
3247 return NULL;
3248 }
3249
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003250 UniquePtr<PcToConcreteMethod> pc_to_concrete_method(new PcToConcreteMethod());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003251 uint32_t dex_pc = 0;
3252 const uint16_t* insns = code_item_->insns_ ;
3253 const Instruction* inst = Instruction::At(insns);
3254
3255 for (; dex_pc < code_item_->insns_size_in_code_units_;
3256 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits(), inst = inst->Next()) {
3257
3258 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3259 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3260 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3261 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3262
3263 if(!(is_interface || is_virtual))
3264 continue;
3265
3266 // Check if vC ("this" pointer in the instruction) has a precise type.
3267 RegisterLine* line = reg_table_.GetLine(dex_pc);
3268 DecodedInstruction dec_insn(inst);
3269 const RegType& reg_type(line->GetRegisterType(dec_insn.vC));
3270
3271 if (!reg_type.IsPreciseReference()) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003272 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003273 }
3274
3275 CHECK(!(reg_type.GetClass()->IsInterface()));
3276 // If the class is an array class, it can be both Abstract and final and so
3277 // the reg_type will be created as precise.
3278 CHECK(!(reg_type.GetClass()->IsAbstract()) || reg_type.GetClass()->IsArrayClass());
3279 // Find the abstract method.
3280 // vB has the method index.
3281 mirror::AbstractMethod* abstract_method = NULL ;
3282 abstract_method = dex_cache_->GetResolvedMethod(dec_insn.vB);
3283 if(abstract_method == NULL) {
3284 // If the method is not found in the cache this means that it was never found
3285 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3286 continue;
3287 }
3288 // Find the concrete method.
3289 mirror::AbstractMethod* concrete_method = NULL;
3290 if (is_interface) {
3291 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3292 }
3293 if (is_virtual) {
3294 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3295 }
3296
3297 if(concrete_method == NULL) {
3298 // In cases where concrete_method is not found continue to the next invoke instead
3299 // of crashing.
3300 continue;
3301 }
3302
3303 CHECK(!concrete_method->IsAbstract()) << PrettyMethod(concrete_method);
3304 // Build method reference.
3305 CompilerDriver::MethodReference concrete_ref(
3306 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3307 concrete_method->GetDexMethodIndex());
3308 // Now Save the current PC and the concrete method reference to be used
3309 // in compiler driver.
3310 pc_to_concrete_method->Put(dex_pc, concrete_ref );
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003311 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003312
3313 if (pc_to_concrete_method->size() == 0) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003314 return NULL ;
3315 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003316 return pc_to_concrete_method.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003317}
3318
Ian Rogers776ac1f2012-04-13 23:36:36 -07003319const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 size_t num_entries, ref_bitmap_bits, pc_bits;
3321 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3322 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003323 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003327 return NULL;
3328 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003329 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3330 // There are 2 bytes to encode the number of entries
3331 if (num_entries >= 65536) {
3332 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003335 return NULL;
3336 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003338 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003339 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003340 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003342 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003343 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003345 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003346 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3349 return NULL;
3350 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003351 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003352 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003355 return NULL;
3356 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003357 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003359 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3360 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003361 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003362 table->push_back(num_entries & 0xFF);
3363 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003366 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003367 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003369 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 }
3371 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003372 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 }
3374 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003375 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 return table;
3377}
jeffhaoa0a764a2011-09-16 10:43:38 -07003378
Ian Rogers776ac1f2012-04-13 23:36:36 -07003379void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3381 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003382 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003383 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003384 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003385 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003386 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003388 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003389 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3390 map_index++;
3391 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003392 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003393 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 CHECK_LT(j / 8, map.RegWidth());
3395 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3396 } else if ((j / 8) < map.RegWidth()) {
3397 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3398 } else {
3399 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3400 }
3401 }
3402 } else {
3403 CHECK(reg_bitmap == NULL);
3404 }
3405 }
3406}
jeffhaoa0a764a2011-09-16 10:43:38 -07003407
Ian Rogers637c65b2013-05-31 11:46:00 -07003408void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3409 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003410 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003411 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003412 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3413 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003414 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003415 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003416 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003417 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003418 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003419 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003420}
3421
Ian Rogers637c65b2013-05-31 11:46:00 -07003422const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3423 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3424 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3425 if (it == dex_gc_maps_->end()) {
3426 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3427 return NULL;
3428 }
3429 CHECK(it->second != NULL);
3430 return it->second;
3431}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003432
Ian Rogers637c65b2013-05-31 11:46:00 -07003433void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
3434 const PcToConcreteMethod* devirt_map) {
3435 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003436 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3437 if (it != devirt_maps_->end()) {
3438 delete it->second;
3439 devirt_maps_->erase(it);
3440 }
3441
3442 devirt_maps_->Put(ref, devirt_map);
3443 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3444}
3445
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003446const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3447 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003448 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003449 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3450 if (it == devirt_maps_->end()) {
3451 return NULL;
3452 }
3453
3454 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003455 MethodVerifier::PcToConcreteMethod::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003456 if(pc_to_concrete_method != it->second->end()) {
3457 return &(pc_to_concrete_method->second);
3458 } else {
3459 return NULL;
3460 }
3461}
3462
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003463std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3464 RegisterLine* line = reg_table_.GetLine(dex_pc);
3465 std::vector<int32_t> result;
3466 for (size_t i = 0; i < line->NumRegs(); ++i) {
3467 const RegType& type = line->GetRegisterType(i);
3468 if (type.IsConstant()) {
3469 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3470 result.push_back(type.ConstantValue());
3471 } else if (type.IsConstantLo()) {
3472 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3473 result.push_back(type.ConstantValueLo());
3474 } else if (type.IsConstantHi()) {
3475 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3476 result.push_back(type.ConstantValueHi());
3477 } else if (type.IsIntegralTypes()) {
3478 result.push_back(kIntVReg);
3479 result.push_back(0);
3480 } else if (type.IsFloat()) {
3481 result.push_back(kFloatVReg);
3482 result.push_back(0);
3483 } else if (type.IsLong()) {
3484 result.push_back(kLongLoVReg);
3485 result.push_back(0);
3486 result.push_back(kLongHiVReg);
3487 result.push_back(0);
3488 ++i;
3489 } else if (type.IsDouble()) {
3490 result.push_back(kDoubleLoVReg);
3491 result.push_back(0);
3492 result.push_back(kDoubleHiVReg);
3493 result.push_back(0);
3494 ++i;
3495 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3496 result.push_back(kUndefined);
3497 result.push_back(0);
3498 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003499 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003500 result.push_back(kReferenceVReg);
3501 result.push_back(0);
3502 }
3503 }
3504 return result;
3505}
3506
Ian Rogers637c65b2013-05-31 11:46:00 -07003507ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003508MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003509
Ian Rogers637c65b2013-05-31 11:46:00 -07003510ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003511MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3512
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003513Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3514MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3515
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003516void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003517 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003518 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003519 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003520 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003521 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003522 }
3523
Ian Rogers637c65b2013-05-31 11:46:00 -07003524 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003525 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003526 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003527 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3528 }
3529
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003530 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3531 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003532 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003533 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3534 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003535 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003536}
3537
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003538void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003539 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003540 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003541 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003542 STLDeleteValues(dex_gc_maps_);
3543 delete dex_gc_maps_;
3544 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003545 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003546 delete dex_gc_maps_lock_;
3547 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003548
3549 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003550 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003551 STLDeleteValues(devirt_maps_);
3552 delete devirt_maps_;
3553 devirt_maps_ = NULL;
3554 }
3555 delete devirt_maps_lock_;
3556 devirt_maps_lock_ = NULL;
3557
3558 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003559 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003560 delete rejected_classes_;
3561 rejected_classes_ = NULL;
3562 }
3563 delete rejected_classes_lock_;
3564 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003565 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003566}
jeffhaod1224c72012-02-29 13:43:08 -08003567
Ian Rogers1212a022013-03-04 10:48:41 -08003568void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003569 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003570 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003571 rejected_classes_->insert(ref);
3572 }
jeffhaod1224c72012-02-29 13:43:08 -08003573 CHECK(IsClassRejected(ref));
3574}
3575
Ian Rogers1212a022013-03-04 10:48:41 -08003576bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003577 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003578 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003579}
3580
Ian Rogersd81871c2011-10-03 13:57:23 -07003581} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003582} // namespace art