blob: ce658299e4c0c1528cdfe3ad519610c7f1534215 [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"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Ian Rogers1212a022013-03-04 10:48:41 -080024#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "dex_instruction.h"
27#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070035#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/field-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080042#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070045namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Ian Rogers2c8a8572011-10-24 17:11:36 -070047static const bool gDebugVerify = false;
48
Ian Rogers7b3ddd22013-02-21 15:19:52 -080049void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070050 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070051 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070052 DCHECK_GT(insns_size, 0U);
53
54 for (uint32_t i = 0; i < insns_size; i++) {
55 bool interesting = false;
56 switch (mode) {
57 case kTrackRegsAll:
58 interesting = flags[i].IsOpcode();
59 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070060 case kTrackCompilerInterestPoints:
61 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 break;
63 case kTrackRegsBranches:
64 interesting = flags[i].IsBranchTarget();
65 break;
66 default:
67 break;
68 }
69 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070070 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070071 }
72 }
73}
74
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070076 std::string& error,
77 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070078 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070079 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070080 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080082 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080083 error = "Verifier rejected class ";
84 error += PrettyDescriptor(klass);
85 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070086 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070087 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080088 if (super != NULL && super->IsFinal()) {
89 error = "Verifier rejected class ";
90 error += PrettyDescriptor(klass);
91 error += " that attempts to sub-class final class ";
92 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070093 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070094 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070095 ClassHelper kh(klass);
96 const DexFile& dex_file = kh.GetDexFile();
97 uint32_t class_def_idx;
98 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
99 error = "Verifier rejected class ";
100 error += PrettyDescriptor(klass);
101 error += " that isn't present in dex file ";
102 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700103 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700104 }
Jeff Haoee988952013-04-16 14:23:47 -0700105 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700106}
107
Ian Rogers365c1022012-06-22 15:05:28 -0700108MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 mirror::DexCache* dex_cache,
110 mirror::ClassLoader* class_loader,
111 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700112 std::string& error,
113 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800114 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
115 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700116 if (class_data == NULL) {
117 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700118 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700119 }
jeffhaof56197c2012-03-05 18:01:54 -0800120 ClassDataItemIterator it(*dex_file, class_data);
121 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
122 it.Next();
123 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700124 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700125 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700126 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700127 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800128 while (it.HasNextDirectMethod()) {
129 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700130 if (method_idx == previous_direct_method_idx) {
131 // smali can create dex files with two encoded_methods sharing the same method_idx
132 // http://code.google.com/p/smali/issues/detail?id=119
133 it.Next();
134 continue;
135 }
136 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700137 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 mirror::AbstractMethod* method =
139 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 if (method == NULL) {
141 DCHECK(Thread::Current()->IsExceptionPending());
142 // We couldn't resolve the method, but continue regardless.
143 Thread::Current()->ClearException();
144 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700145 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700146 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700147 if (result != kNoFailure) {
148 if (result == kHardFailure) {
149 hard_fail = true;
150 if (error_count > 0) {
151 error += "\n";
152 }
153 error = "Verifier rejected class ";
154 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
155 error += " due to bad method ";
156 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800159 }
160 it.Next();
161 }
jeffhao9b0b1882012-10-01 16:51:22 -0700162 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800163 while (it.HasNextVirtualMethod()) {
164 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700165 if (method_idx == previous_virtual_method_idx) {
166 // smali can create dex files with two encoded_methods sharing the same method_idx
167 // http://code.google.com/p/smali/issues/detail?id=119
168 it.Next();
169 continue;
170 }
171 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700172 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 mirror::AbstractMethod* method =
174 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700175 if (method == NULL) {
176 DCHECK(Thread::Current()->IsExceptionPending());
177 // We couldn't resolve the method, but continue regardless.
178 Thread::Current()->ClearException();
179 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700180 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700181 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700182 if (result != kNoFailure) {
183 if (result == kHardFailure) {
184 hard_fail = true;
185 if (error_count > 0) {
186 error += "\n";
187 }
188 error = "Verifier rejected class ";
189 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
190 error += " due to bad method ";
191 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800194 }
195 it.Next();
196 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 if (error_count == 0) {
198 return kNoFailure;
199 } else {
200 return hard_fail ? kHardFailure : kSoftFailure;
201 }
jeffhaof56197c2012-03-05 18:01:54 -0800202}
203
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
205 const DexFile* dex_file,
206 mirror::DexCache* dex_cache,
207 mirror::ClassLoader* class_loader,
208 uint32_t class_def_idx,
209 const DexFile::CodeItem* code_item,
210 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700211 uint32_t method_access_flags,
212 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700213 MethodVerifier::FailureKind result = kNoFailure;
214 uint64_t start_ns = NanoTime();
215
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700217 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700218 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 // Verification completed, however failures may be pending that didn't cause the verification
220 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700221 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 if (verifier.failures_.size() != 0) {
223 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700224 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700225 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800226 }
227 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 // Bad method data.
229 CHECK_NE(verifier.failures_.size(), 0U);
230 CHECK(verifier.have_pending_hard_failure_);
231 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700232 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800233 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700234 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800235 verifier.Dump(std::cout);
236 }
Ian Rogersc8982582012-09-07 16:53:25 -0700237 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
Ian Rogersc8982582012-09-07 16:53:25 -0700239 uint64_t duration_ns = NanoTime() - start_ns;
240 if (duration_ns > MsToNs(100)) {
241 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
242 << " took " << PrettyDuration(duration_ns);
243 }
244 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800245}
246
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800247void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 const DexFile* dex_file, mirror::DexCache* dex_cache,
249 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
250 const DexFile::CodeItem* code_item,
251 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800252 uint32_t method_access_flags) {
253 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700254 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800256 verifier.DumpFailures(os);
257 os << verifier.info_messages_.str();
258 verifier.Dump(os);
259}
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
262 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
263 const DexFile::CodeItem* code_item,
264 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700265 uint32_t method_access_flags, bool can_load_classes,
266 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800267 : reg_types_(can_load_classes),
268 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800269 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700270 foo_method_(method),
271 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800272 dex_file_(dex_file),
273 dex_cache_(dex_cache),
274 class_loader_(class_loader),
275 class_def_idx_(class_def_idx),
276 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700277 interesting_dex_pc_(-1),
278 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700280 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800281 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800282 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700283 can_load_classes_(can_load_classes),
284 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800285}
286
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800288 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700289 MethodHelper mh(m);
290 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
291 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700292 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700293 verifier.interesting_dex_pc_ = dex_pc;
294 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
295 verifier.FindLocksAtDexPc();
296}
297
298void MethodVerifier::FindLocksAtDexPc() {
299 CHECK(monitor_enter_dex_pcs_ != NULL);
300 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
301
302 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
303 // verification. In practice, the phase we want relies on data structures set up by all the
304 // earlier passes, so we just run the full method verification and bail out early when we've
305 // got what we wanted.
306 Verify();
307}
308
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 // If there aren't any instructions, make sure that's expected, then exit successfully.
311 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700312 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700314 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 } else {
316 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700317 }
jeffhaobdb76512011-09-07 11:43:16 -0700318 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
320 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
322 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700323 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700324 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800326 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 // Run through the instructions and see if the width checks out.
328 bool result = ComputeWidthsAndCountOps();
329 // Flag instructions guarded by a "try" block and check exception handlers.
330 result = result && ScanTryCatchBlocks();
331 // Perform static instruction verification.
332 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Perform code-flow analysis and return.
334 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700335}
336
Ian Rogers776ac1f2012-04-13 23:36:36 -0700337std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700338 switch (error) {
339 case VERIFY_ERROR_NO_CLASS:
340 case VERIFY_ERROR_NO_FIELD:
341 case VERIFY_ERROR_NO_METHOD:
342 case VERIFY_ERROR_ACCESS_CLASS:
343 case VERIFY_ERROR_ACCESS_FIELD:
344 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700345 case VERIFY_ERROR_INSTANTIATION:
346 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800347 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700348 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
349 // class change and instantiation errors into soft verification errors so that we re-verify
350 // at runtime. We may fail to find or to agree on access because of not yet available class
351 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
352 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
353 // paths" that dynamically perform the verification and cause the behavior to be that akin
354 // to an interpreter.
355 error = VERIFY_ERROR_BAD_CLASS_SOFT;
356 } else {
357 have_pending_runtime_throw_failure_ = true;
358 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700359 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700360 // Indication that verification should be retried at runtime.
361 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700362 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700363 have_pending_hard_failure_ = true;
364 }
365 break;
jeffhaod5347e02012-03-22 17:25:05 -0700366 // Hard verification failures at compile time will still fail at runtime, so the class is
367 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700368 case VERIFY_ERROR_BAD_CLASS_HARD: {
369 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800370 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800371 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800372 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373 have_pending_hard_failure_ = true;
374 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800375 }
376 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700377 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800378 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379 work_insn_idx_));
380 std::ostringstream* failure_message = new std::ostringstream(location);
381 failure_messages_.push_back(failure_message);
382 return *failure_message;
383}
384
385void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
386 size_t failure_num = failure_messages_.size();
387 DCHECK_NE(failure_num, 0U);
388 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
389 prepend += last_fail_message->str();
390 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
391 delete last_fail_message;
392}
393
394void MethodVerifier::AppendToLastFailMessage(std::string append) {
395 size_t failure_num = failure_messages_.size();
396 DCHECK_NE(failure_num, 0U);
397 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
398 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800399}
400
Ian Rogers776ac1f2012-04-13 23:36:36 -0700401bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700402 const uint16_t* insns = code_item_->insns_;
403 size_t insns_size = code_item_->insns_size_in_code_units_;
404 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700405 size_t new_instance_count = 0;
406 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700407 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700408
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700410 Instruction::Code opcode = inst->Opcode();
411 if (opcode == Instruction::NEW_INSTANCE) {
412 new_instance_count++;
413 } else if (opcode == Instruction::MONITOR_ENTER) {
414 monitor_enter_count++;
415 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700416 size_t inst_size = inst->SizeInCodeUnits();
417 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
418 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700419 inst = inst->Next();
420 }
421
Ian Rogersd81871c2011-10-03 13:57:23 -0700422 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
424 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700425 return false;
426 }
427
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 new_instance_count_ = new_instance_count;
429 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700430 return true;
431}
432
Ian Rogers776ac1f2012-04-13 23:36:36 -0700433bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700435 if (tries_size == 0) {
436 return true;
437 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700439 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700440
441 for (uint32_t idx = 0; idx < tries_size; idx++) {
442 const DexFile::TryItem* try_item = &tries[idx];
443 uint32_t start = try_item->start_addr_;
444 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700445 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
447 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700448 return false;
449 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700450 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700452 return false;
453 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700454 for (uint32_t dex_pc = start; dex_pc < end;
455 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
456 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700457 }
458 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800459 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700460 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700461 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700462 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700463 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700464 CatchHandlerIterator iterator(handlers_ptr);
465 for (; iterator.HasNext(); iterator.Next()) {
466 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700467 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700469 return false;
470 }
jeffhao60f83e32012-02-13 17:16:30 -0800471 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
472 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700474 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800475 return false;
476 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700477 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700478 // Ensure exception types are resolved so that they don't need resolution to be delivered,
479 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700480 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
482 iterator.GetHandlerTypeIndex(),
483 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700484 if (exception_type == NULL) {
485 DCHECK(Thread::Current()->IsExceptionPending());
486 Thread::Current()->ClearException();
487 }
488 }
jeffhaobdb76512011-09-07 11:43:16 -0700489 }
Ian Rogers0571d352011-11-03 19:51:38 -0700490 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700491 }
jeffhaobdb76512011-09-07 11:43:16 -0700492 return true;
493}
494
Ian Rogers776ac1f2012-04-13 23:36:36 -0700495bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700497
Ian Rogers0c7abda2012-09-19 13:33:42 -0700498 /* 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 -0700499 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700500 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700501
502 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700503 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700504 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 return false;
507 }
508 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700509 // All invoke points are marked as "Throw" points already.
510 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700511 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700512 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 }
514 dex_pc += inst->SizeInCodeUnits();
515 inst = inst->Next();
516 }
517 return true;
518}
519
Ian Rogers776ac1f2012-04-13 23:36:36 -0700520bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800521 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 bool result = true;
523 switch (inst->GetVerifyTypeArgumentA()) {
524 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800525 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 break;
527 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800528 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 break;
530 }
531 switch (inst->GetVerifyTypeArgumentB()) {
532 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800533 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 break;
535 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800536 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 break;
538 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800539 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700540 break;
541 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800542 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 break;
544 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 break;
547 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800548 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 break;
550 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800551 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700552 break;
553 }
554 switch (inst->GetVerifyTypeArgumentC()) {
555 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800556 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 break;
558 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800559 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 break;
561 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800562 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 break;
564 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800565 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 break;
567 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800568 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700569 break;
570 }
571 switch (inst->GetVerifyExtraFlags()) {
572 case Instruction::kVerifyArrayData:
573 result = result && CheckArrayData(code_offset);
574 break;
575 case Instruction::kVerifyBranchTarget:
576 result = result && CheckBranchTarget(code_offset);
577 break;
578 case Instruction::kVerifySwitchTargets:
579 result = result && CheckSwitchTargets(code_offset);
580 break;
581 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800582 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 break;
584 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 result = false;
590 break;
591 }
592 return result;
593}
594
Ian Rogers776ac1f2012-04-13 23:36:36 -0700595bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
598 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 return false;
600 }
601 return true;
602}
603
Ian Rogers776ac1f2012-04-13 23:36:36 -0700604bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
607 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 return false;
609 }
610 return true;
611}
612
Ian Rogers776ac1f2012-04-13 23:36:36 -0700613bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
616 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 return false;
618 }
619 return true;
620}
621
Ian Rogers776ac1f2012-04-13 23:36:36 -0700622bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
625 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 return false;
627 }
628 return true;
629}
630
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
634 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700638 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 return false;
642 }
643 return true;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
649 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 return false;
651 }
652 return true;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
658 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 return false;
660 }
661 return true;
662}
663
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
667 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 return false;
669 }
670 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700671 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 const char* cp = descriptor;
673 while (*cp++ == '[') {
674 bracket_count++;
675 }
676 if (bracket_count == 0) {
677 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 return false;
680 } else if (bracket_count > 255) {
681 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
684 }
685 return true;
686}
687
Ian Rogers776ac1f2012-04-13 23:36:36 -0700688bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
690 const uint16_t* insns = code_item_->insns_ + cur_offset;
691 const uint16_t* array_data;
692 int32_t array_data_offset;
693
694 DCHECK_LT(cur_offset, insn_count);
695 /* make sure the start of the array data table is in range */
696 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
697 if ((int32_t) cur_offset + array_data_offset < 0 ||
698 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
700 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
702 }
703 /* offset to array data table is a relative branch-style offset */
704 array_data = insns + array_data_offset;
705 /* make sure the table is 32-bit aligned */
706 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
708 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700712 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
714 /* make sure the end of the switch is in range */
715 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
717 << ", data offset " << array_data_offset << ", end "
718 << cur_offset + array_data_offset + table_size
719 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 return true;
723}
724
Ian Rogers776ac1f2012-04-13 23:36:36 -0700725bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 int32_t offset;
727 bool isConditional, selfOkay;
728 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
729 return false;
730 }
731 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700732 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 -0700733 return false;
734 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700735 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
736 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
742 int32_t abs_offset = cur_offset + offset;
743 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700745 << reinterpret_cast<void*>(abs_offset) << ") at "
746 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return false;
748 }
749 insn_flags_[abs_offset].SetBranchTarget();
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 bool* selfOkay) {
755 const uint16_t* insns = code_item_->insns_ + cur_offset;
756 *pConditional = false;
757 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700758 switch (*insns & 0xff) {
759 case Instruction::GOTO:
760 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700761 break;
762 case Instruction::GOTO_32:
763 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700764 *selfOkay = true;
765 break;
766 case Instruction::GOTO_16:
767 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700768 break;
769 case Instruction::IF_EQ:
770 case Instruction::IF_NE:
771 case Instruction::IF_LT:
772 case Instruction::IF_GE:
773 case Instruction::IF_GT:
774 case Instruction::IF_LE:
775 case Instruction::IF_EQZ:
776 case Instruction::IF_NEZ:
777 case Instruction::IF_LTZ:
778 case Instruction::IF_GEZ:
779 case Instruction::IF_GTZ:
780 case Instruction::IF_LEZ:
781 *pOffset = (int16_t) insns[1];
782 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700783 break;
784 default:
785 return false;
786 break;
787 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700788 return true;
789}
790
Ian Rogers776ac1f2012-04-13 23:36:36 -0700791bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700793 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700795 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
797 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
799 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700800 return false;
801 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700802 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700804 /* make sure the table is 32-bit aligned */
805 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
807 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700808 return false;
809 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 uint32_t switch_count = switch_insns[1];
811 int32_t keys_offset, targets_offset;
812 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
814 /* 0=sig, 1=count, 2/3=firstKey */
815 targets_offset = 4;
816 keys_offset = -1;
817 expected_signature = Instruction::kPackedSwitchSignature;
818 } else {
819 /* 0=sig, 1=count, 2..count*2 = keys */
820 keys_offset = 2;
821 targets_offset = 2 + 2 * switch_count;
822 expected_signature = Instruction::kSparseSwitchSignature;
823 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700825 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
827 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700828 return false;
829 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 /* make sure the end of the switch is in range */
831 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
833 << switch_offset << ", end "
834 << (cur_offset + switch_offset + table_size)
835 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700836 return false;
837 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700838 /* for a sparse switch, verify the keys are in ascending order */
839 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
841 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700842 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
843 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
844 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
846 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 return false;
848 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700849 last_key = key;
850 }
851 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 for (uint32_t targ = 0; targ < switch_count; targ++) {
854 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
855 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
856 int32_t abs_offset = cur_offset + offset;
857 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700859 << reinterpret_cast<void*>(abs_offset) << ") at "
860 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 return false;
862 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 insn_flags_[abs_offset].SetBranchTarget();
864 }
865 return true;
866}
867
Ian Rogers776ac1f2012-04-13 23:36:36 -0700868bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 return false;
872 }
873 uint16_t registers_size = code_item_->registers_size_;
874 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800875 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
877 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 return false;
879 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 }
881
882 return true;
883}
884
Ian Rogers776ac1f2012-04-13 23:36:36 -0700885bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 uint16_t registers_size = code_item_->registers_size_;
887 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
888 // integer overflow when adding them here.
889 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
891 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700892 return false;
893 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return true;
895}
896
Ian Rogers0c7abda2012-09-19 13:33:42 -0700897static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800898 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
899 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
900 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
901 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
902 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
903 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
904 gc_map.begin(),
905 gc_map.end());
906 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
907 DCHECK_EQ(gc_map.size(),
908 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
909 (length_prefixed_gc_map->at(1) << 16) |
910 (length_prefixed_gc_map->at(2) << 8) |
911 (length_prefixed_gc_map->at(3) << 0)));
912 return length_prefixed_gc_map;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 uint16_t registers_size = code_item_->registers_size_;
917 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700918
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800920 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
921 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 }
923 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700924 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
925
jeffhaobdb76512011-09-07 11:43:16 -0700926
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 work_line_.reset(new RegisterLine(registers_size, this));
928 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700929
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 /* Initialize register types of method arguments. */
931 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700932 DCHECK_NE(failures_.size(), 0U);
933 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800934 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
938 /* Perform code flow verification. */
939 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700940 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700942 }
943
Ian Rogers1212a022013-03-04 10:48:41 -0800944 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700945
TDYa127b2eb5c12012-05-24 15:52:10 -0700946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800948 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
949 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700950 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 return false; // Not a real failure, but a failure to encode
952 }
Ian Rogers39ebcb82013-05-30 16:57:23 -0700953 if (kIsDebugBuild) {
954 VerifyGcMap(*map);
955 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700956 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
957 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800958
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700959 MethodVerifier::PcToConcreteMethod* pc_to_concrete_method = GenerateDevirtMap();
960 if(pc_to_concrete_method != NULL ) {
961 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700962 }
jeffhaobdb76512011-09-07 11:43:16 -0700963 return true;
964}
965
Ian Rogersad0b3a32012-04-16 14:50:24 -0700966std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
967 DCHECK_EQ(failures_.size(), failure_messages_.size());
968 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700969 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700970 }
971 return os;
972}
973
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700975 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700976 v->Dump(std::cerr);
977}
978
Ian Rogers776ac1f2012-04-13 23:36:36 -0700979void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800980 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700981 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return;
jeffhaobdb76512011-09-07 11:43:16 -0700983 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800984 {
985 os << "Register Types:\n";
986 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
987 std::ostream indent_os(&indent_filter);
988 reg_types_.Dump(indent_os);
989 }
Ian Rogersb4903572012-10-11 11:52:56 -0700990 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800991 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
992 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 const Instruction* inst = Instruction::At(code_item_->insns_);
994 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
995 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
997 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800998 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -0700999 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001000 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 const bool kDumpHexOfInstruction = false;
1002 if (kDumpHexOfInstruction) {
1003 indent_os << inst->DumpHex(5) << " ";
1004 }
1005 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001006 inst = inst->Next();
1007 }
jeffhaobdb76512011-09-07 11:43:16 -07001008}
1009
Ian Rogersd81871c2011-10-03 13:57:23 -07001010static bool IsPrimitiveDescriptor(char descriptor) {
1011 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001012 case 'I':
1013 case 'C':
1014 case 'S':
1015 case 'B':
1016 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001017 case 'F':
1018 case 'D':
1019 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001021 default:
1022 return false;
1023 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 RegisterLine* reg_line = reg_table_.GetLine(0);
1028 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1029 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1032 //Include the "this" pointer.
1033 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1036 // argument as uninitialized. This restricts field access until the superclass constructor is
1037 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001038 const RegType& declaring_class = GetDeclaringClass();
1039 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 reg_line->SetRegisterType(arg_start + cur_arg,
1041 reg_types_.UninitializedThisArgument(declaring_class));
1042 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001043 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001044 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001046 }
1047
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001048 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001049 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001050 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051
1052 for (; iterator.HasNext(); iterator.Next()) {
1053 const char* descriptor = iterator.GetDescriptor();
1054 if (descriptor == NULL) {
1055 LOG(FATAL) << "Null descriptor";
1056 }
1057 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1059 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
1062 switch (descriptor[0]) {
1063 case 'L':
1064 case '[':
1065 // We assume that reference arguments are initialized. The only way it could be otherwise
1066 // (assuming the caller was verified) is if the current method is <init>, but in that case
1067 // it's effectively considered initialized the instant we reach here (in the sense that we
1068 // can return without doing anything or call virtual methods).
1069 {
Ian Rogersb4903572012-10-11 11:52:56 -07001070 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001071 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 }
1073 break;
1074 case 'Z':
1075 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1076 break;
1077 case 'C':
1078 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1079 break;
1080 case 'B':
1081 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1082 break;
1083 case 'I':
1084 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1085 break;
1086 case 'S':
1087 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1088 break;
1089 case 'F':
1090 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1091 break;
1092 case 'J':
1093 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001094 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1095 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1096 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 cur_arg++;
1098 break;
1099 }
1100 default:
jeffhaod5347e02012-03-22 17:25:05 -07001101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 cur_arg++;
1105 }
1106 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1111 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1112 // format. Only major difference from the method argument format is that 'V' is supported.
1113 bool result;
1114 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1115 result = descriptor[1] == '\0';
1116 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1117 size_t i = 0;
1118 do {
1119 i++;
1120 } while (descriptor[i] == '['); // process leading [
1121 if (descriptor[i] == 'L') { // object array
1122 do {
1123 i++; // find closing ;
1124 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1125 result = descriptor[i] == ';';
1126 } else { // primitive array
1127 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1128 }
1129 } else if (descriptor[0] == 'L') {
1130 // could be more thorough here, but shouldn't be required
1131 size_t i = 0;
1132 do {
1133 i++;
1134 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1135 result = descriptor[i] == ';';
1136 } else {
1137 result = false;
1138 }
1139 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001140 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1141 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 }
1143 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001144}
1145
Ian Rogers776ac1f2012-04-13 23:36:36 -07001146bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 const uint16_t* insns = code_item_->insns_;
1148 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001149
jeffhaobdb76512011-09-07 11:43:16 -07001150 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 insn_flags_[0].SetChanged();
1152 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001153
jeffhaobdb76512011-09-07 11:43:16 -07001154 /* Continue until no instructions are marked "changed". */
1155 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1157 uint32_t insn_idx = start_guess;
1158 for (; insn_idx < insns_size; insn_idx++) {
1159 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001160 break;
1161 }
jeffhaobdb76512011-09-07 11:43:16 -07001162 if (insn_idx == insns_size) {
1163 if (start_guess != 0) {
1164 /* try again, starting from the top */
1165 start_guess = 0;
1166 continue;
1167 } else {
1168 /* all flags are clear */
1169 break;
1170 }
1171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 // We carry the working set of registers from instruction to instruction. If this address can
1173 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1174 // "changed" flags, we need to load the set of registers from the table.
1175 // Because we always prefer to continue on to the next instruction, we should never have a
1176 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1177 // target.
1178 work_insn_idx_ = insn_idx;
1179 if (insn_flags_[insn_idx].IsBranchTarget()) {
1180 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001181 } else {
1182#ifndef NDEBUG
1183 /*
1184 * Sanity check: retrieve the stored register line (assuming
1185 * a full table) and make sure it actually matches.
1186 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1188 if (register_line != NULL) {
1189 if (work_line_->CompareLine(register_line) != 0) {
1190 Dump(std::cout);
1191 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001193 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1194 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001195 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 }
jeffhaobdb76512011-09-07 11:43:16 -07001197 }
1198#endif
1199 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001202 prepend += " failed to verify: ";
1203 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001204 return false;
1205 }
jeffhaobdb76512011-09-07 11:43:16 -07001206 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 insn_flags_[insn_idx].SetVisited();
1208 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001209 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001210
Ian Rogers1c849e52012-06-28 14:00:33 -07001211 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001212 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001213 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001214 * (besides the wasted space), but it indicates a flaw somewhere
1215 * down the line, possibly in the verifier.
1216 *
1217 * If we've substituted "always throw" instructions into the stream,
1218 * we are almost certainly going to have some dead code.
1219 */
1220 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 uint32_t insn_idx = 0;
1222 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001223 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001224 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001225 * may or may not be preceded by a padding NOP (for alignment).
1226 */
1227 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1228 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1229 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001230 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001231 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1232 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1233 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001235 }
1236
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001238 if (dead_start < 0)
1239 dead_start = insn_idx;
1240 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001241 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001242 dead_start = -1;
1243 }
1244 }
1245 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001246 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 }
1248 }
jeffhaobdb76512011-09-07 11:43:16 -07001249 return true;
1250}
1251
Ian Rogers776ac1f2012-04-13 23:36:36 -07001252bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001253 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1254 // We want the state _before_ the instruction, for the case where the dex pc we're
1255 // interested in is itself a monitor-enter instruction (which is a likely place
1256 // for a thread to be suspended).
1257 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001258 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001259 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1260 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1261 }
1262 }
1263
jeffhaobdb76512011-09-07 11:43:16 -07001264 /*
1265 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001266 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001267 * control to another statement:
1268 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001269 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001270 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001271 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001272 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001273 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001274 * throw an exception that is handled by an encompassing "try"
1275 * block.
1276 *
1277 * We can also return, in which case there is no successor instruction
1278 * from this point.
1279 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001280 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001281 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1283 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001284 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001285 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001286
jeffhaobdb76512011-09-07 11:43:16 -07001287 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001288 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001289 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001291 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1292 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 }
jeffhaobdb76512011-09-07 11:43:16 -07001294
1295 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001296 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001297 * can throw an exception, we will copy/merge this into the "catch"
1298 * address rather than work_line, because we don't want the result
1299 * from the "successful" code path (e.g. a check-cast that "improves"
1300 * a type) to be visible to the exception handler.
1301 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001302 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001304 } else {
1305#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001307#endif
1308 }
1309
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001310 // We need to ensure the work line is consistent while performing validation. When we spot a
1311 // peephole pattern we compute a new line for either the fallthrough instruction or the
1312 // branch target.
1313 UniquePtr<RegisterLine> branch_line;
1314 UniquePtr<RegisterLine> fallthrough_line;
1315
Elliott Hughesadb8c672012-03-06 16:49:32 -08001316 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001317 case Instruction::NOP:
1318 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001319 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001320 * a signature that looks like a NOP; if we see one of these in
1321 * the course of executing code then we have a problem.
1322 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001323 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001325 }
1326 break;
1327
1328 case Instruction::MOVE:
1329 case Instruction::MOVE_FROM16:
1330 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001331 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001332 break;
1333 case Instruction::MOVE_WIDE:
1334 case Instruction::MOVE_WIDE_FROM16:
1335 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001336 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001337 break;
1338 case Instruction::MOVE_OBJECT:
1339 case Instruction::MOVE_OBJECT_FROM16:
1340 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001341 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001342 break;
1343
1344 /*
1345 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001346 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001347 * might want to hold the result in an actual CPU register, so the
1348 * Dalvik spec requires that these only appear immediately after an
1349 * invoke or filled-new-array.
1350 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001351 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001352 * redundant with the reset done below, but it can make the debug info
1353 * easier to read in some cases.)
1354 */
1355 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001356 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001357 break;
1358 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001360 break;
1361 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001363 break;
1364
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001366 /*
jeffhao60f83e32012-02-13 17:16:30 -08001367 * This statement can only appear as the first instruction in an exception handler. We verify
1368 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001369 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001370 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001372 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 }
jeffhaobdb76512011-09-07 11:43:16 -07001374 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001375 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1376 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 }
jeffhaobdb76512011-09-07 11:43:16 -07001379 }
1380 break;
1381 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001382 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001383 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 const RegType& return_type = GetMethodReturnType();
1385 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 } else {
1388 // Compilers may generate synthetic functions that write byte values into boolean fields.
1389 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1392 ((return_type.IsBoolean() || return_type.IsByte() ||
1393 return_type.IsShort() || return_type.IsChar()) &&
1394 src_type.IsInteger()));
1395 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001396 bool success =
1397 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1398 if (!success) {
1399 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 }
jeffhaobdb76512011-09-07 11:43:16 -07001401 }
1402 }
1403 break;
1404 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001405 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001406 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 const RegType& return_type = GetMethodReturnType();
1408 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 } else {
1411 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001412 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1413 if (!success) {
1414 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 }
jeffhaobdb76512011-09-07 11:43:16 -07001416 }
1417 }
1418 break;
1419 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001420 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 const RegType& return_type = GetMethodReturnType();
1422 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 } else {
1425 /* return_type is the *expected* return type, not register value */
1426 DCHECK(!return_type.IsZero());
1427 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001428 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001429 // Disallow returning uninitialized values and verify that the reference in vAA is an
1430 // instance of the "return_type"
1431 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001432 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001433 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001434 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1435 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001436 }
1437 }
1438 }
1439 break;
1440
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001442 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001443 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001445 break;
jeffhaobdb76512011-09-07 11:43:16 -07001446 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001447 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001449 break;
jeffhaobdb76512011-09-07 11:43:16 -07001450 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001452 break;
1453 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001454 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001455 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001456 break;
jeffhaobdb76512011-09-07 11:43:16 -07001457 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 case Instruction::CONST_WIDE_16: {
1459 int64_t val = static_cast<int16_t>(dec_insn.vB);
1460 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1461 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1462 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001463 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 }
1465 case Instruction::CONST_WIDE_32: {
1466 int64_t val = static_cast<int32_t>(dec_insn.vB);
1467 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1468 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1469 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1470 break;
1471 }
1472 case Instruction::CONST_WIDE: {
1473 int64_t val = dec_insn.vB_wide;
1474 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1475 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1476 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1477 break;
1478 }
1479 case Instruction::CONST_WIDE_HIGH16: {
1480 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1481 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1482 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1483 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1484 break;
1485 }
jeffhaobdb76512011-09-07 11:43:16 -07001486 case Instruction::CONST_STRING:
1487 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001488 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001489 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001491 // Get type from instruction if unresolved then we need an access check
1492 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001493 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001494 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001495 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001496 res_type.IsConflict() ? res_type
1497 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 }
jeffhaobdb76512011-09-07 11:43:16 -07001500 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001501 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001502 break;
1503 case Instruction::MONITOR_EXIT:
1504 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001505 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001506 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001507 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001508 * to the need to handle asynchronous exceptions, a now-deprecated
1509 * feature that Dalvik doesn't support.)
1510 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001511 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001512 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001513 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001514 * structured locking checks are working, the former would have
1515 * failed on the -enter instruction, and the latter is impossible.
1516 *
1517 * This is fortunate, because issue 3221411 prevents us from
1518 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001520 * some catch blocks (which will show up as "dead" code when
1521 * we skip them here); if we can't, then the code path could be
1522 * "live" so we still need to check it.
1523 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001524 opcode_flags &= ~Instruction::kThrow;
1525 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001526 break;
1527
Ian Rogers28ad40d2011-10-27 15:19:26 -07001528 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001530 /*
1531 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1532 * could be a "upcast" -- not expected, so we don't try to address it.)
1533 *
1534 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001535 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001536 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001537 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001538 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001539 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 if (res_type.IsConflict()) {
1541 DCHECK_NE(failures_.size(), 0U);
1542 if (!is_checkcast) {
1543 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1544 }
1545 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001546 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001547 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1548 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001550 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001552 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001554 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001555 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001556 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001558 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
jeffhaobdb76512011-09-07 11:43:16 -07001560 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001561 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 }
1563 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001564 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001565 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001566 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 }
1571 }
1572 break;
1573 }
1574 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001575 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001576 if (res_type.IsConflict()) {
1577 DCHECK_NE(failures_.size(), 0U);
1578 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001579 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1581 // can't create an instance of an interface or abstract class */
1582 if (!res_type.IsInstantiableTypes()) {
1583 Fail(VERIFY_ERROR_INSTANTIATION)
1584 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001585 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001587 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1588 // Any registers holding previous allocations from this address that have not yet been
1589 // initialized must be marked invalid.
1590 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1591 // add the new uninitialized reference to the register state
1592 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 break;
1594 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001595 case Instruction::NEW_ARRAY:
1596 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001597 break;
1598 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001599 VerifyNewArray(dec_insn, true, false);
1600 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001601 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001602 case Instruction::FILLED_NEW_ARRAY_RANGE:
1603 VerifyNewArray(dec_insn, true, true);
1604 just_set_result = true; // Filled new array range sets result register
1605 break;
jeffhaobdb76512011-09-07 11:43:16 -07001606 case Instruction::CMPL_FLOAT:
1607 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001608 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001609 break;
1610 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001612 break;
1613 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
1616 case Instruction::CMPL_DOUBLE:
1617 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1619 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001620 break;
1621 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1623 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001624 break;
1625 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001626 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001627 break;
1628 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1630 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001631 break;
1632 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1634 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001635 break;
1636 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001637 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001641 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001642 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001643 }
1644 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::GOTO:
1647 case Instruction::GOTO_16:
1648 case Instruction::GOTO_32:
1649 /* no effect on or use of registers */
1650 break;
1651
1652 case Instruction::PACKED_SWITCH:
1653 case Instruction::SPARSE_SWITCH:
1654 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001655 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 case Instruction::FILL_ARRAY_DATA: {
1659 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001660 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001661 /* array_type can be null if the reg type is Zero */
1662 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001663 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001665 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1667 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001668 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1670 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 } else {
jeffhao457cc512012-02-02 16:55:13 -08001672 // Now verify if the element width in the table matches the element width declared in
1673 // the array
1674 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1675 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001677 } else {
1678 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1679 // Since we don't compress the data in Dex, expect to see equal width of data stored
1680 // in the table and expected from the array class.
1681 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1683 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 }
1686 }
jeffhaobdb76512011-09-07 11:43:16 -07001687 }
1688 }
1689 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001693 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1694 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 bool mismatch = false;
1696 if (reg_type1.IsZero()) { // zero then integral or reference expected
1697 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1698 } else if (reg_type1.IsReferenceTypes()) { // both references?
1699 mismatch = !reg_type2.IsReferenceTypes();
1700 } else { // both integral?
1701 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1702 }
1703 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1705 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001706 }
1707 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 }
jeffhaobdb76512011-09-07 11:43:16 -07001709 case Instruction::IF_LT:
1710 case Instruction::IF_GE:
1711 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001713 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1714 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1717 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001718 }
1719 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001723 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001727
1728 // Find previous instruction - its existence is a precondition to peephole optimization.
1729 uint32_t prev_idx = 0;
1730 if (0 != work_insn_idx_) {
1731 prev_idx = work_insn_idx_ - 1;
1732 while(0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1733 prev_idx--;
1734 }
1735 CHECK(insn_flags_[prev_idx].IsOpcode());
1736 } else {
1737 break;
1738 }
1739
1740 const Instruction* prev_inst = Instruction::At(code_item_->insns_+prev_idx);
1741
1742 /* Check for peep-hole pattern of:
1743 * ...;
1744 * instance-of vX, vO, T;
1745 * ifXXX vX, b ;
1746 * ...;
1747 * b: INST;
1748 * ...;
1749 * and sharpen the type for either the fall-through or the branch case.
1750 */
1751 if (!CurrentInsnFlags()->IsBranchTarget()) {
1752 DecodedInstruction prev_dec_insn(prev_inst);
1753 if ((Instruction::INSTANCE_OF == prev_inst->Opcode())
1754 && (dec_insn.vA == prev_dec_insn.vA)) {
1755 // Check that the we are not attempting conversion to interface types,
1756 // which is not done because of the multiple inheritance implications.
1757 const RegType& cast_type =
1758 ResolveClassAndCheckAccess(prev_dec_insn.vC);
1759
1760 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
1761 if (dec_insn.opcode == Instruction::IF_EQZ) {
1762 fallthrough_line.reset(new RegisterLine(code_item_->registers_size_, this));
1763 fallthrough_line->CopyFromLine(work_line_.get());
1764 fallthrough_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1765 } else {
1766 branch_line.reset(new RegisterLine(code_item_->registers_size_, this));
1767 branch_line->CopyFromLine(work_line_.get());
1768 branch_line->SetRegisterType(prev_dec_insn.vB , cast_type);
1769 }
1770 }
1771 }
1772 }
1773
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 }
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::IF_LTZ:
1777 case Instruction::IF_GEZ:
1778 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001780 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1783 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 }
jeffhaobdb76512011-09-07 11:43:16 -07001785 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 }
jeffhaobdb76512011-09-07 11:43:16 -07001787 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1789 break;
jeffhaobdb76512011-09-07 11:43:16 -07001790 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1792 break;
jeffhaobdb76512011-09-07 11:43:16 -07001793 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 VerifyAGet(dec_insn, reg_types_.Char(), true);
1795 break;
jeffhaobdb76512011-09-07 11:43:16 -07001796 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 case Instruction::AGET:
1800 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1801 break;
jeffhaobdb76512011-09-07 11:43:16 -07001802 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001806 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 case Instruction::APUT_BOOLEAN:
1810 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1811 break;
1812 case Instruction::APUT_BYTE:
1813 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1814 break;
1815 case Instruction::APUT_CHAR:
1816 VerifyAPut(dec_insn, reg_types_.Char(), true);
1817 break;
1818 case Instruction::APUT_SHORT:
1819 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
1821 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001823 break;
1824 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001825 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
1827 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001828 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001829 break;
1830
jeffhaobdb76512011-09-07 11:43:16 -07001831 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001832 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 break;
jeffhaobdb76512011-09-07 11:43:16 -07001834 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001835 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 break;
jeffhaobdb76512011-09-07 11:43:16 -07001837 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001838 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 break;
jeffhaobdb76512011-09-07 11:43:16 -07001840 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001841 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 break;
1843 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001844 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
1846 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001850 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 break;
jeffhaobdb76512011-09-07 11:43:16 -07001852
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001854 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 break;
1856 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001857 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 break;
1859 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001860 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 break;
1862 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001863 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001866 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001867 break;
1868 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001869 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 break;
jeffhaobdb76512011-09-07 11:43:16 -07001871 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001872 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874
jeffhaobdb76512011-09-07 11:43:16 -07001875 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001876 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001879 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001882 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 break;
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001885 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 break;
1887 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001888 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001891 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001894 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 break;
1896
1897 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001898 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 break;
1900 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001901 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 break;
1903 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001904 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 break;
1906 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001907 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
1909 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001910 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001914 break;
1915 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001916 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918
1919 case Instruction::INVOKE_VIRTUAL:
1920 case Instruction::INVOKE_VIRTUAL_RANGE:
1921 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001923 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1924 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1925 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1926 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001927 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1928 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001929 const char* descriptor;
1930 if (called_method == NULL) {
1931 uint32_t method_idx = dec_insn.vB;
1932 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1933 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1934 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1935 } else {
1936 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
Ian Rogersb4903572012-10-11 11:52:56 -07001938 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001939 if (!return_type.IsLowHalf()) {
1940 work_line_->SetResultRegisterType(return_type);
1941 } else {
1942 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1943 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001944 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001949 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001950 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1951 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001952 const char* return_type_descriptor;
1953 bool is_constructor;
1954 if (called_method == NULL) {
1955 uint32_t method_idx = dec_insn.vB;
1956 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1957 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1958 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1959 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1960 } else {
1961 is_constructor = called_method->IsConstructor();
1962 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1963 }
1964 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001965 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 * Some additional checks when calling a constructor. We know from the invocation arg check
1967 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1968 * that to require that called_method->klass is the same as this->klass or this->super,
1969 * allowing the latter only if the "this" argument is the same as the "this" argument to
1970 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001971 */
jeffhaob57e9522012-04-26 18:08:21 -07001972 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1973 if (this_type.IsConflict()) // failure.
1974 break;
jeffhaobdb76512011-09-07 11:43:16 -07001975
jeffhaob57e9522012-04-26 18:08:21 -07001976 /* no null refs allowed (?) */
1977 if (this_type.IsZero()) {
1978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1979 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001980 }
jeffhaob57e9522012-04-26 18:08:21 -07001981
1982 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001983 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1984 // TODO: re-enable constructor type verification
1985 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001986 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001987 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1988 // break;
1989 // }
jeffhaob57e9522012-04-26 18:08:21 -07001990
1991 /* arg must be an uninitialized reference */
1992 if (!this_type.IsUninitializedTypes()) {
1993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1994 << this_type;
1995 break;
1996 }
1997
1998 /*
1999 * Replace the uninitialized reference with an initialized one. We need to do this for all
2000 * registers that have the same object instance in them, not just the "this" register.
2001 */
2002 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002003 }
Ian Rogersb4903572012-10-11 11:52:56 -07002004 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2005 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002006 if (!return_type.IsLowHalf()) {
2007 work_line_->SetResultRegisterType(return_type);
2008 } else {
2009 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2010 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002011 just_set_result = true;
2012 break;
2013 }
2014 case Instruction::INVOKE_STATIC:
2015 case Instruction::INVOKE_STATIC_RANGE: {
2016 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002017 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002018 const char* descriptor;
2019 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002020 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002021 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2022 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002023 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002024 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002025 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002026 }
Ian Rogersb4903572012-10-11 11:52:56 -07002027 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002028 if (!return_type.IsLowHalf()) {
2029 work_line_->SetResultRegisterType(return_type);
2030 } else {
2031 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2032 }
jeffhaobdb76512011-09-07 11:43:16 -07002033 just_set_result = true;
2034 }
2035 break;
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002038 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002039 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002040 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002041 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002042 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2043 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2044 << PrettyMethod(abs_method) << "'";
2045 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002046 }
Ian Rogers0d604842012-04-16 14:50:24 -07002047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002048 /* Get the type of the "this" arg, which should either be a sub-interface of called
2049 * interface or Object (see comments in RegType::JoinClass).
2050 */
2051 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
2052 if (this_type.IsZero()) {
2053 /* null pointer always passes (and always fails at runtime) */
2054 } else {
2055 if (this_type.IsUninitializedTypes()) {
2056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2057 << this_type;
2058 break;
2059 }
2060 // In the past we have tried to assert that "called_interface" is assignable
2061 // from "this_type.GetClass()", however, as we do an imprecise Join
2062 // (RegType::JoinClass) we don't have full information on what interfaces are
2063 // implemented by "this_type". For example, two classes may implement the same
2064 // interfaces and have a common parent that doesn't implement the interface. The
2065 // join will set "this_type" to the parent class and a test that this implements
2066 // the interface will incorrectly fail.
2067 }
2068 /*
2069 * We don't have an object instance, so we can't find the concrete method. However, all of
2070 * the type information is in the abstract method, so we're good.
2071 */
2072 const char* descriptor;
2073 if (abs_method == NULL) {
2074 uint32_t method_idx = dec_insn.vB;
2075 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2076 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2077 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2078 } else {
2079 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2080 }
Ian Rogersb4903572012-10-11 11:52:56 -07002081 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002082 if (!return_type.IsLowHalf()) {
2083 work_line_->SetResultRegisterType(return_type);
2084 } else {
2085 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2086 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002087 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 }
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::NEG_INT:
2091 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::NEG_LONG:
2095 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2097 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002103 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2104 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002107 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2108 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002114 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2115 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002118 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2119 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002122 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2123 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002126 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2127 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002133 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2134 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002137 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2138 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002141 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2142 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002145 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2146 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2150 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161
2162 case Instruction::ADD_INT:
2163 case Instruction::SUB_INT:
2164 case Instruction::MUL_INT:
2165 case Instruction::REM_INT:
2166 case Instruction::DIV_INT:
2167 case Instruction::SHL_INT:
2168 case Instruction::SHR_INT:
2169 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2171 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::AND_INT:
2174 case Instruction::OR_INT:
2175 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2177 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::ADD_LONG:
2180 case Instruction::SUB_LONG:
2181 case Instruction::MUL_LONG:
2182 case Instruction::DIV_LONG:
2183 case Instruction::REM_LONG:
2184 case Instruction::AND_LONG:
2185 case Instruction::OR_LONG:
2186 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002187 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2188 reg_types_.LongLo(), reg_types_.LongHi(),
2189 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::SHL_LONG:
2192 case Instruction::SHR_LONG:
2193 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002195 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2196 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198 case Instruction::ADD_FLOAT:
2199 case Instruction::SUB_FLOAT:
2200 case Instruction::MUL_FLOAT:
2201 case Instruction::DIV_FLOAT:
2202 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
2205 case Instruction::ADD_DOUBLE:
2206 case Instruction::SUB_DOUBLE:
2207 case Instruction::MUL_DOUBLE:
2208 case Instruction::DIV_DOUBLE:
2209 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002210 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2211 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2212 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214 case Instruction::ADD_INT_2ADDR:
2215 case Instruction::SUB_INT_2ADDR:
2216 case Instruction::MUL_INT_2ADDR:
2217 case Instruction::REM_INT_2ADDR:
2218 case Instruction::SHL_INT_2ADDR:
2219 case Instruction::SHR_INT_2ADDR:
2220 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::AND_INT_2ADDR:
2224 case Instruction::OR_INT_2ADDR:
2225 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
2228 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::ADD_LONG_2ADDR:
2232 case Instruction::SUB_LONG_2ADDR:
2233 case Instruction::MUL_LONG_2ADDR:
2234 case Instruction::DIV_LONG_2ADDR:
2235 case Instruction::REM_LONG_2ADDR:
2236 case Instruction::AND_LONG_2ADDR:
2237 case Instruction::OR_LONG_2ADDR:
2238 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002239 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2240 reg_types_.LongLo(), reg_types_.LongHi(),
2241 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002242 break;
2243 case Instruction::SHL_LONG_2ADDR:
2244 case Instruction::SHR_LONG_2ADDR:
2245 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2247 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
2249 case Instruction::ADD_FLOAT_2ADDR:
2250 case Instruction::SUB_FLOAT_2ADDR:
2251 case Instruction::MUL_FLOAT_2ADDR:
2252 case Instruction::DIV_FLOAT_2ADDR:
2253 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::ADD_DOUBLE_2ADDR:
2257 case Instruction::SUB_DOUBLE_2ADDR:
2258 case Instruction::MUL_DOUBLE_2ADDR:
2259 case Instruction::DIV_DOUBLE_2ADDR:
2260 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2262 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2263 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::ADD_INT_LIT16:
2266 case Instruction::RSUB_INT:
2267 case Instruction::MUL_INT_LIT16:
2268 case Instruction::DIV_INT_LIT16:
2269 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272 case Instruction::AND_INT_LIT16:
2273 case Instruction::OR_INT_LIT16:
2274 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::ADD_INT_LIT8:
2278 case Instruction::RSUB_INT_LIT8:
2279 case Instruction::MUL_INT_LIT8:
2280 case Instruction::DIV_INT_LIT8:
2281 case Instruction::REM_INT_LIT8:
2282 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002286 break;
2287 case Instruction::AND_INT_LIT8:
2288 case Instruction::OR_INT_LIT8:
2289 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
2292
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002294 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002295 case Instruction::UNUSED_EE:
2296 case Instruction::UNUSED_EF:
2297 case Instruction::UNUSED_F2:
2298 case Instruction::UNUSED_F3:
2299 case Instruction::UNUSED_F4:
2300 case Instruction::UNUSED_F5:
2301 case Instruction::UNUSED_F6:
2302 case Instruction::UNUSED_F7:
2303 case Instruction::UNUSED_F8:
2304 case Instruction::UNUSED_F9:
2305 case Instruction::UNUSED_FA:
2306 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002307 case Instruction::UNUSED_F0:
2308 case Instruction::UNUSED_F1:
2309 case Instruction::UNUSED_E3:
2310 case Instruction::UNUSED_E8:
2311 case Instruction::UNUSED_E7:
2312 case Instruction::UNUSED_E4:
2313 case Instruction::UNUSED_E9:
2314 case Instruction::UNUSED_FC:
2315 case Instruction::UNUSED_E5:
2316 case Instruction::UNUSED_EA:
2317 case Instruction::UNUSED_FD:
2318 case Instruction::UNUSED_E6:
2319 case Instruction::UNUSED_EB:
2320 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002321 case Instruction::UNUSED_3E:
2322 case Instruction::UNUSED_3F:
2323 case Instruction::UNUSED_40:
2324 case Instruction::UNUSED_41:
2325 case Instruction::UNUSED_42:
2326 case Instruction::UNUSED_43:
2327 case Instruction::UNUSED_73:
2328 case Instruction::UNUSED_79:
2329 case Instruction::UNUSED_7A:
2330 case Instruction::UNUSED_EC:
2331 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334
2335 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002336 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002337 * complain if an instruction is missing (which is desirable).
2338 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002340
Ian Rogersad0b3a32012-04-16 14:50:24 -07002341 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002343 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002344 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002345 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002346 /* immediate failure, reject class */
2347 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2348 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002349 } else if (have_pending_runtime_throw_failure_) {
2350 /* slow path will throw, mark following code as unreachable */
2351 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002352 }
jeffhaobdb76512011-09-07 11:43:16 -07002353 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 * If we didn't just set the result register, clear it out. This ensures that you can only use
2355 * "move-result" immediately after the result is set. (We could check this statically, but it's
2356 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002357 */
2358 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002360 }
2361
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002362
jeffhaobdb76512011-09-07 11:43:16 -07002363
2364 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002365 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002366 *
2367 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002368 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002369 * somebody could get a reference field, check it for zero, and if the
2370 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002371 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002372 * that, and will reject the code.
2373 *
2374 * TODO: avoid re-fetching the branch target
2375 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002376 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002377 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002379 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002381 return false;
2382 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002383 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002384 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002385 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 }
jeffhaobdb76512011-09-07 11:43:16 -07002387 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002388 if (NULL != branch_line.get()) {
2389 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2390 return false;
2391 }
2392 } else {
2393 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2394 return false;
2395 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 }
2398
2399 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002401 *
2402 * We've already verified that the table is structurally sound, so we
2403 * just need to walk through and tag the targets.
2404 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002406 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2407 const uint16_t* switch_insns = insns + offset_to_switch;
2408 int switch_count = switch_insns[1];
2409 int offset_to_targets, targ;
2410
2411 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2412 /* 0 = sig, 1 = count, 2/3 = first key */
2413 offset_to_targets = 4;
2414 } else {
2415 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002416 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002417 offset_to_targets = 2 + 2 * switch_count;
2418 }
2419
2420 /* verify each switch target */
2421 for (targ = 0; targ < switch_count; targ++) {
2422 int offset;
2423 uint32_t abs_offset;
2424
2425 /* offsets are 32-bit, and only partly endian-swapped */
2426 offset = switch_insns[offset_to_targets + targ * 2] |
2427 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 abs_offset = work_insn_idx_ + offset;
2429 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002430 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002431 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 }
2433 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002434 return false;
2435 }
2436 }
2437
2438 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2440 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002441 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002442 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002444 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002445
Ian Rogers0571d352011-11-03 19:51:38 -07002446 for (; iterator.HasNext(); iterator.Next()) {
2447 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 within_catch_all = true;
2449 }
jeffhaobdb76512011-09-07 11:43:16 -07002450 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2452 * "work_regs", because at runtime the exception will be thrown before the instruction
2453 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002454 */
Ian Rogers0571d352011-11-03 19:51:38 -07002455 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002456 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 }
jeffhaobdb76512011-09-07 11:43:16 -07002458 }
2459
2460 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2462 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002463 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002465 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 * The state in work_line reflects the post-execution state. If the current instruction is a
2467 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002468 * it will do so before grabbing the lock).
2469 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002470 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002471 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002473 return false;
2474 }
2475 }
2476 }
2477
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002478 /* Handle "continue". Tag the next consecutive instruction.
2479 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2480 * because it changes work_line_ when performing peephole optimization
2481 * and this change should not be used in those cases.
2482 */
2483 if ((opcode_flags & Instruction::kContinue) != 0) {
2484 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2485 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2487 return false;
2488 }
2489 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2490 // next instruction isn't one.
2491 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2492 return false;
2493 }
2494 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2495 if (next_line != NULL) {
2496 if (NULL != fallthrough_line.get()) {
2497 // Make workline consistent with fallthrough computed from peephole optimization.
2498 work_line_->CopyFromLine(fallthrough_line.get());
2499 }
2500 // Merge registers into what we have for the next instruction,
2501 // and set the "changed" flag if needed.
2502 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2503 return false;
2504 }
2505 } else {
2506 /*
2507 * We're not recording register data for the next instruction, so we don't know what the
2508 * prior state was. We have to assume that something has changed and re-evaluate it.
2509 */
2510 insn_flags_[next_insn_idx].SetChanged();
2511 }
2512 }
2513
jeffhaod1f0fde2011-09-08 17:25:33 -07002514 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002515 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002516 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 return false;
2518 }
jeffhaobdb76512011-09-07 11:43:16 -07002519 }
2520
2521 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002522 * Update start_guess. Advance to the next instruction of that's
2523 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002524 * neither of those exists we're in a return or throw; leave start_guess
2525 * alone and let the caller sort it out.
2526 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002527 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002529 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002530 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002532 }
2533
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2535 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002536
2537 return true;
2538}
2539
Ian Rogers776ac1f2012-04-13 23:36:36 -07002540const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002541 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002543 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002544 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002545 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2546 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002547 if (result.IsConflict()) {
2548 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2549 << "' in " << referrer;
2550 return result;
2551 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002552 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002553 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002554 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002555 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002556 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002557 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002559 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002560 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002561 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002562}
2563
Ian Rogers776ac1f2012-04-13 23:36:36 -07002564const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002565 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002567 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2569 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002570 CatchHandlerIterator iterator(handlers_ptr);
2571 for (; iterator.HasNext(); iterator.Next()) {
2572 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2573 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002574 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002576 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002577 if (common_super == NULL) {
2578 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2579 // as that is caught at runtime
2580 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002581 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002582 // We don't know enough about the type and the common path merge will result in
2583 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002584 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002585 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002586 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002587 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002589 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002590 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 }
2592 }
2593 }
2594 }
Ian Rogers0571d352011-11-03 19:51:38 -07002595 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 }
2597 }
2598 if (common_super == NULL) {
2599 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002600 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002601 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002603 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002604}
2605
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002606mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2607 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002608 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002609 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002610 if (klass_type.IsConflict()) {
2611 std::string append(" in attempt to access method ");
2612 append += dex_file_->GetMethodName(method_id);
2613 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002614 return NULL;
2615 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002616 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002617 return NULL; // Can't resolve Class so no more to do here
2618 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002619 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002620 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002621 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002623 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002624 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002625
2626 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002628 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 res_method = klass->FindInterfaceMethod(name, signature);
2630 } else {
2631 res_method = klass->FindVirtualMethod(name, signature);
2632 }
2633 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002636 // If a virtual or interface method wasn't found with the expected type, look in
2637 // the direct methods. This can happen when the wrong invoke type is used or when
2638 // a class has changed, and will be flagged as an error in later checks.
2639 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2640 res_method = klass->FindDirectMethod(name, signature);
2641 }
2642 if (res_method == NULL) {
2643 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2644 << PrettyDescriptor(klass) << "." << name
2645 << " " << signature;
2646 return NULL;
2647 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 }
2649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2651 // enforce them here.
2652 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2654 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 return NULL;
2656 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002657 // Disallow any calls to class initializers.
2658 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2660 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002661 return NULL;
2662 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002663 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002664 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002665 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002666 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002667 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002668 }
jeffhaode0d9c92012-02-27 13:58:13 -08002669 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2670 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2672 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002673 return NULL;
2674 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002675 // Check that interface methods match interface classes.
2676 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2677 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2678 << " is in an interface class " << PrettyClass(klass);
2679 return NULL;
2680 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2681 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2682 << " is in a non-interface class " << PrettyClass(klass);
2683 return NULL;
2684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 // See if the method type implied by the invoke instruction matches the access flags for the
2686 // target method.
2687 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2688 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2689 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2690 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002691 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2692 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 return NULL;
2694 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002695 return res_method;
2696}
2697
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002698mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2699 MethodType method_type, bool is_range,
2700 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002701 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2702 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002703 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002704 if (res_method == NULL) { // error or class is unresolved
2705 return NULL;
2706 }
2707
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2709 // has a vtable entry for the target method.
2710 if (is_super) {
2711 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002712 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002713 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002714 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002715 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002716 << " to super " << PrettyMethod(res_method);
2717 return NULL;
2718 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002719 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002720 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002721 MethodHelper mh(res_method);
2722 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002723 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002724 << " to super " << super
2725 << "." << mh.GetName()
2726 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 return NULL;
2728 }
2729 }
2730 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2731 // match the call to the signature. Also, we might might be calling through an abstract method
2732 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002733 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 /* caught by static verifier */
2735 DCHECK(is_range || expected_args <= 5);
2736 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2739 return NULL;
2740 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002741
jeffhaobdb76512011-09-07 11:43:16 -07002742 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002743 * Check the "this" argument, which must be an instance of the class that declared the method.
2744 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2745 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002746 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002747 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 if (!res_method->IsStatic()) {
2749 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002750 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 return NULL;
2752 }
2753 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 return NULL;
2756 }
2757 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002758 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002759 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002760 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002762 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 return NULL;
2764 }
2765 }
2766 actual_args++;
2767 }
2768 /*
2769 * Process the target method's signature. This signature may or may not
2770 * have been verified, so we can't assume it's properly formed.
2771 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002772 MethodHelper mh(res_method);
2773 const DexFile::TypeList* params = mh.GetParameterTypeList();
2774 size_t params_size = params == NULL ? 0 : params->Size();
2775 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002778 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2779 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 return NULL;
2781 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002782 const char* descriptor =
2783 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2784 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002786 << " missing signature component";
2787 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 }
Ian Rogersb4903572012-10-11 11:52:56 -07002789 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002790 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002791 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002792 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 }
2794 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2795 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002798 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 return NULL;
2800 } else {
2801 return res_method;
2802 }
2803}
2804
Ian Rogers776ac1f2012-04-13 23:36:36 -07002805void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002806 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002808 if (res_type.IsConflict()) { // bad class
2809 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002810 } else {
2811 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2812 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002814 } else if (!is_filled) {
2815 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002816 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002817 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002819 } else {
2820 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2821 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002822 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002823 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002824 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002825 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002826 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002828 return;
2829 }
2830 }
2831 // filled-array result goes into "result" register
2832 work_line_->SetResultRegisterType(res_type);
2833 }
2834 }
2835}
2836
Ian Rogers776ac1f2012-04-13 23:36:36 -07002837void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002838 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002843 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002844 if (array_type.IsZero()) {
2845 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2846 // instruction type. TODO: have a proper notion of bottom here.
2847 if (!is_primitive || insn_type.IsCategory1Types()) {
2848 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002849 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002851 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002852 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2853 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002854 }
jeffhaofc3144e2012-02-01 17:21:15 -08002855 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002857 } else {
2858 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002860 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002862 << " source for aget-object";
2863 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002865 << " source for category 1 aget";
2866 } else if (is_primitive && !insn_type.Equals(component_type) &&
2867 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 (insn_type.IsLong() && component_type.IsDouble()))) {
2869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2870 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002871 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002872 // Use knowledge of the field type which is stronger than the type inferred from the
2873 // instruction, which can't differentiate object types and ints from floats, longs from
2874 // doubles.
2875 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002876 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002877 } else {
2878 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2879 component_type.HighHalf(&reg_types_));
2880 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 }
2882 }
2883 }
2884}
2885
Ian Rogers776ac1f2012-04-13 23:36:36 -07002886void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002888 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002892 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002893 if (array_type.IsZero()) {
2894 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2895 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002896 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002898 } else {
2899 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002901 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002903 << " source for aput-object";
2904 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002906 << " source for category 1 aput";
2907 } else if (is_primitive && !insn_type.Equals(component_type) &&
2908 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2909 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002911 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002913 // The instruction agrees with the type of array, confirm the value to be stored does too
2914 // Note: we use the instruction type (rather than the component type) for aput-object as
2915 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002916 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 }
2918 }
2919 }
2920}
2921
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002922mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002923 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2924 // Check access to class
2925 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002926 if (klass_type.IsConflict()) { // bad class
2927 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2928 field_idx, dex_file_->GetFieldName(field_id),
2929 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002930 return NULL;
2931 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002932 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002933 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002934 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002935 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002936 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002938 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2939 << dex_file_->GetFieldName(field_id) << ") in "
2940 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 DCHECK(Thread::Current()->IsExceptionPending());
2942 Thread::Current()->ClearException();
2943 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002944 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2945 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 return NULL;
2949 } else if (!field->IsStatic()) {
2950 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2951 return NULL;
2952 } else {
2953 return field;
2954 }
2955}
2956
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002957mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002958 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2959 // Check access to class
2960 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002961 if (klass_type.IsConflict()) {
2962 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2963 field_idx, dex_file_->GetFieldName(field_id),
2964 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002965 return NULL;
2966 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002967 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002968 return NULL; // Can't resolve Class so no more to do here
2969 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002970 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002973 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2974 << dex_file_->GetFieldName(field_id) << ") in "
2975 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 DCHECK(Thread::Current()->IsExceptionPending());
2977 Thread::Current()->ClearException();
2978 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2980 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002982 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 return NULL;
2984 } else if (field->IsStatic()) {
2985 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2986 << " to not be static";
2987 return NULL;
2988 } else if (obj_type.IsZero()) {
2989 // Cannot infer and check type, however, access will cause null pointer exception
2990 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002991 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002992 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002993 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 if (obj_type.IsUninitializedTypes() &&
2995 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2996 !field_klass.Equals(GetDeclaringClass()))) {
2997 // Field accesses through uninitialized references are only allowable for constructors where
2998 // the field is declared in this class
2999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3000 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003001 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 return NULL;
3003 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3004 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3005 // of C1. For resolution to occur the declared class of the field must be compatible with
3006 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3007 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3008 << " from object of type " << obj_type;
3009 return NULL;
3010 } else {
3011 return field;
3012 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 }
3014}
3015
Ian Rogers776ac1f2012-04-13 23:36:36 -07003016void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003017 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003018 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003019 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003020 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003021 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003022 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003023 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07003024 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003025 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003026 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003027 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 if (field != NULL) {
3029 descriptor = FieldHelper(field).GetTypeDescriptor();
3030 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003031 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3033 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3034 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003035 }
Ian Rogersb4903572012-10-11 11:52:56 -07003036 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 if (is_primitive) {
3038 if (field_type.Equals(insn_type) ||
3039 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3040 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3041 // expected that read is of the correct primitive type or that int reads are reading
3042 // floats or long reads are reading doubles
3043 } else {
3044 // This is a global failure rather than a class change failure as the instructions and
3045 // the descriptors for the type should have been consistent within the same file at
3046 // compile time
3047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3048 << " to be of type '" << insn_type
3049 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 return;
3051 }
3052 } else {
3053 if (!insn_type.IsAssignableFrom(field_type)) {
3054 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3055 << " to be compatible with type '" << insn_type
3056 << "' but found type '" << field_type
3057 << "' in get-object";
3058 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
3059 return;
3060 }
3061 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003062 if (!field_type.IsLowHalf()) {
3063 work_line_->SetRegisterType(dec_insn.vA, field_type);
3064 } else {
3065 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
3066 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003067}
3068
Ian Rogers776ac1f2012-04-13 23:36:36 -07003069void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003070 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003071 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003072 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003073 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003074 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003075 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003076 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003077 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003078 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003079 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003080 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 if (field != NULL) {
3082 descriptor = FieldHelper(field).GetTypeDescriptor();
3083 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003084 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3086 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3087 loader = class_loader_;
3088 }
Ian Rogersb4903572012-10-11 11:52:56 -07003089 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003090 if (field != NULL) {
3091 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3092 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3093 << " from other class " << GetDeclaringClass();
3094 return;
3095 }
3096 }
3097 if (is_primitive) {
3098 // Primitive field assignability rules are weaker than regular assignability rules
3099 bool instruction_compatible;
3100 bool value_compatible;
3101 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3102 if (field_type.IsIntegralTypes()) {
3103 instruction_compatible = insn_type.IsIntegralTypes();
3104 value_compatible = value_type.IsIntegralTypes();
3105 } else if (field_type.IsFloat()) {
3106 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3107 value_compatible = value_type.IsFloatTypes();
3108 } else if (field_type.IsLong()) {
3109 instruction_compatible = insn_type.IsLong();
3110 value_compatible = value_type.IsLongTypes();
3111 } else if (field_type.IsDouble()) {
3112 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3113 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003114 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003115 instruction_compatible = false; // reference field with primitive store
3116 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 if (!instruction_compatible) {
3119 // This is a global failure rather than a class change failure as the instructions and
3120 // the descriptors for the type should have been consistent within the same file at
3121 // compile time
3122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3123 << " to be of type '" << insn_type
3124 << "' but found type '" << field_type
3125 << "' in put";
3126 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003127 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003128 if (!value_compatible) {
3129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3130 << " of type " << value_type
3131 << " but expected " << field_type
3132 << " for store to " << PrettyField(field) << " in put";
3133 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003134 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003135 } else {
3136 if (!insn_type.IsAssignableFrom(field_type)) {
3137 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3138 << " to be compatible with type '" << insn_type
3139 << "' but found type '" << field_type
3140 << "' in put-object";
3141 return;
3142 }
3143 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 }
3145}
3146
Ian Rogers776ac1f2012-04-13 23:36:36 -07003147bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003148 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 return false;
3151 }
3152 return true;
3153}
3154
Ian Rogers776ac1f2012-04-13 23:36:36 -07003155bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003156 bool changed = true;
3157 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3158 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003159 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 * We haven't processed this instruction before, and we haven't touched the registers here, so
3161 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3162 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003163 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003165 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003166 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3167 if (gDebugVerify) {
3168 copy->CopyFromLine(target_line);
3169 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003170 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003171 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003173 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003174 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003175 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003176 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3177 << *copy.get() << " MERGE\n"
3178 << *merge_line << " ==\n"
3179 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003180 }
3181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 if (changed) {
3183 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003184 }
3185 return true;
3186}
3187
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003188InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003189 return &insn_flags_[work_insn_idx_];
3190}
3191
Ian Rogersad0b3a32012-04-16 14:50:24 -07003192const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003193 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003194 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3195 uint16_t return_type_idx = proto_id.return_type_idx_;
3196 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003197 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003198}
3199
3200const RegType& MethodVerifier::GetDeclaringClass() {
3201 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003202 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003203 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003204 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003205 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003206 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003207 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003208 }
3209}
3210
Ian Rogers776ac1f2012-04-13 23:36:36 -07003211void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 size_t* log2_max_gc_pc) {
3213 size_t local_gc_points = 0;
3214 size_t max_insn = 0;
3215 size_t max_ref_reg = -1;
3216 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003217 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 local_gc_points++;
3219 max_insn = i;
3220 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003221 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003222 }
3223 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003224 *gc_points = local_gc_points;
3225 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3226 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003227 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003228 i++;
3229 }
3230 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003231}
3232
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003233MethodVerifier::PcToConcreteMethod* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003234
3235 // It is risky to rely on reg_types for sharpening in cases of soft
3236 // verification, we might end up sharpening to a wrong implementation. Just abort.
3237 if (!failure_messages_.empty()) {
3238 return NULL;
3239 }
3240
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003241 UniquePtr<PcToConcreteMethod> pc_to_concrete_method(new PcToConcreteMethod());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003242 uint32_t dex_pc = 0;
3243 const uint16_t* insns = code_item_->insns_ ;
3244 const Instruction* inst = Instruction::At(insns);
3245
3246 for (; dex_pc < code_item_->insns_size_in_code_units_;
3247 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits(), inst = inst->Next()) {
3248
3249 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3250 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3251 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3252 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3253
3254 if(!(is_interface || is_virtual))
3255 continue;
3256
3257 // Check if vC ("this" pointer in the instruction) has a precise type.
3258 RegisterLine* line = reg_table_.GetLine(dex_pc);
3259 DecodedInstruction dec_insn(inst);
3260 const RegType& reg_type(line->GetRegisterType(dec_insn.vC));
3261
3262 if (!reg_type.IsPreciseReference()) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003263 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003264 }
3265
3266 CHECK(!(reg_type.GetClass()->IsInterface()));
3267 // If the class is an array class, it can be both Abstract and final and so
3268 // the reg_type will be created as precise.
3269 CHECK(!(reg_type.GetClass()->IsAbstract()) || reg_type.GetClass()->IsArrayClass());
3270 // Find the abstract method.
3271 // vB has the method index.
3272 mirror::AbstractMethod* abstract_method = NULL ;
3273 abstract_method = dex_cache_->GetResolvedMethod(dec_insn.vB);
3274 if(abstract_method == NULL) {
3275 // If the method is not found in the cache this means that it was never found
3276 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3277 continue;
3278 }
3279 // Find the concrete method.
3280 mirror::AbstractMethod* concrete_method = NULL;
3281 if (is_interface) {
3282 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3283 }
3284 if (is_virtual) {
3285 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3286 }
3287
3288 if(concrete_method == NULL) {
3289 // In cases where concrete_method is not found continue to the next invoke instead
3290 // of crashing.
3291 continue;
3292 }
3293
3294 CHECK(!concrete_method->IsAbstract()) << PrettyMethod(concrete_method);
3295 // Build method reference.
3296 CompilerDriver::MethodReference concrete_ref(
3297 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3298 concrete_method->GetDexMethodIndex());
3299 // Now Save the current PC and the concrete method reference to be used
3300 // in compiler driver.
3301 pc_to_concrete_method->Put(dex_pc, concrete_ref );
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003302 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003303
3304 if (pc_to_concrete_method->size() == 0) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003305 return NULL ;
3306 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003307 return pc_to_concrete_method.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003308}
3309
Ian Rogers776ac1f2012-04-13 23:36:36 -07003310const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 size_t num_entries, ref_bitmap_bits, pc_bits;
3312 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3313 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003314 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003318 return NULL;
3319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3321 // There are 2 bytes to encode the number of entries
3322 if (num_entries >= 65536) {
3323 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003326 return NULL;
3327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003329 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003330 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003331 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003333 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003334 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003336 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003339 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3340 return NULL;
3341 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003342 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003343 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003346 return NULL;
3347 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003348 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003350 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3351 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003352 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003353 table->push_back(num_entries & 0xFF);
3354 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003355 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003357 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003358 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003360 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 }
3362 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003363 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 }
3365 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003366 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003367 return table;
3368}
jeffhaoa0a764a2011-09-16 10:43:38 -07003369
Ian Rogers776ac1f2012-04-13 23:36:36 -07003370void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3372 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003373 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003375 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003377 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003379 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3381 map_index++;
3382 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003383 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003384 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003385 CHECK_LT(j / 8, map.RegWidth());
3386 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3387 } else if ((j / 8) < map.RegWidth()) {
3388 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3389 } else {
3390 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3391 }
3392 }
3393 } else {
3394 CHECK(reg_bitmap == NULL);
3395 }
3396 }
3397}
jeffhaoa0a764a2011-09-16 10:43:38 -07003398
Ian Rogers1212a022013-03-04 10:48:41 -08003399void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003400 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003401 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003402 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3403 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003404 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003405 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003406 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003407 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003408 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003409 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003410}
3411
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003412void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref, const PcToConcreteMethod* devirt_map) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003413
3414 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3415 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3416 if (it != devirt_maps_->end()) {
3417 delete it->second;
3418 devirt_maps_->erase(it);
3419 }
3420
3421 devirt_maps_->Put(ref, devirt_map);
3422 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3423}
3424
Ian Rogers1212a022013-03-04 10:48:41 -08003425const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003426 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003427 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3428 if (it == dex_gc_maps_->end()) {
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003429 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003430 return NULL;
3431 }
3432 CHECK(it->second != NULL);
3433 return it->second;
3434}
3435
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003436const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3437 uint32_t dex_pc) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003438 MutexLock mu(Thread::Current(), *devirt_maps_lock_);
3439 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3440 if (it == devirt_maps_->end()) {
3441 return NULL;
3442 }
3443
3444 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07003445 MethodVerifier::PcToConcreteMethod::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003446 if(pc_to_concrete_method != it->second->end()) {
3447 return &(pc_to_concrete_method->second);
3448 } else {
3449 return NULL;
3450 }
3451}
3452
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003453std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3454 RegisterLine* line = reg_table_.GetLine(dex_pc);
3455 std::vector<int32_t> result;
3456 for (size_t i = 0; i < line->NumRegs(); ++i) {
3457 const RegType& type = line->GetRegisterType(i);
3458 if (type.IsConstant()) {
3459 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3460 result.push_back(type.ConstantValue());
3461 } else if (type.IsConstantLo()) {
3462 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3463 result.push_back(type.ConstantValueLo());
3464 } else if (type.IsConstantHi()) {
3465 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3466 result.push_back(type.ConstantValueHi());
3467 } else if (type.IsIntegralTypes()) {
3468 result.push_back(kIntVReg);
3469 result.push_back(0);
3470 } else if (type.IsFloat()) {
3471 result.push_back(kFloatVReg);
3472 result.push_back(0);
3473 } else if (type.IsLong()) {
3474 result.push_back(kLongLoVReg);
3475 result.push_back(0);
3476 result.push_back(kLongHiVReg);
3477 result.push_back(0);
3478 ++i;
3479 } else if (type.IsDouble()) {
3480 result.push_back(kDoubleLoVReg);
3481 result.push_back(0);
3482 result.push_back(kDoubleHiVReg);
3483 result.push_back(0);
3484 ++i;
3485 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3486 result.push_back(kUndefined);
3487 result.push_back(0);
3488 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003489 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003490 result.push_back(kReferenceVReg);
3491 result.push_back(0);
3492 }
3493 }
3494 return result;
3495}
3496
Ian Rogers0c7abda2012-09-19 13:33:42 -07003497Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3498MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003499
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003500Mutex* MethodVerifier::devirt_maps_lock_ = NULL;
3501MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
3502
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003503Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3504MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3505
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003506void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003507 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003508 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003509 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003510 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003511 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003512 }
3513
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003514 devirt_maps_lock_ = new Mutex("verifier Devirtualization lock");
3515 {
3516 MutexLock mu(self, *devirt_maps_lock_);
3517 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
3518 }
3519
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003520 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3521 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003522 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003523 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3524 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003525 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003526}
3527
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003528void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003529 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003530 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003531 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003532 STLDeleteValues(dex_gc_maps_);
3533 delete dex_gc_maps_;
3534 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003535 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003536 delete dex_gc_maps_lock_;
3537 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003538
3539 {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003540 MutexLock mu(self, *devirt_maps_lock_);
3541 STLDeleteValues(devirt_maps_);
3542 delete devirt_maps_;
3543 devirt_maps_ = NULL;
3544 }
3545 delete devirt_maps_lock_;
3546 devirt_maps_lock_ = NULL;
3547
3548 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003549 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003550 delete rejected_classes_;
3551 rejected_classes_ = NULL;
3552 }
3553 delete rejected_classes_lock_;
3554 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003555 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003556}
jeffhaod1224c72012-02-29 13:43:08 -08003557
Ian Rogers1212a022013-03-04 10:48:41 -08003558void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003559 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003560 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003561 rejected_classes_->insert(ref);
3562 }
jeffhaod1224c72012-02-29 13:43:08 -08003563 CHECK(IsClassRejected(ref));
3564}
3565
Ian Rogers1212a022013-03-04 10:48:41 -08003566bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003567 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003568 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003569}
3570
Ian Rogersd81871c2011-10-03 13:57:23 -07003571} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003572} // namespace art